#!/bin/bash
#
# Welcom to use Bigemap Server
# Copyright(c) 2020 BIGEMAP
#

help_remove() {
  echo -e "
The remove command uninstall Bigemap Server.

Usage:

  ./bm-server-installer remove <target>

  target        : (required) The directory where Bigemap Server has been installed
                  on the host.

Example:

  ./bm-server-installer remove bm-server/

"
}

remove() {
  if [ ! -n "$1" ]; then
    echo -e "You must specify the directory where Bigemap Server has been installed. \n"
    help_remove
    exit 1
  fi

  TARGET=$1

  cd $TARGET
  if [ $? -ne 0 ]; then
    exit 1
  fi

  if [ ! -f "bm-server.sh" ]; then
    echo -e "${RED}Bigemap Server has not been installed in this directory. ${NC}\n"
    exit 1
  fi

  ./bm-server.sh stop
  set +e
  docker rm bm-server
  set -e
  docker rmi $(docker images | grep "^bigemap/bm-server" | awk '{print $3}')

  rm docker-compose.yml
  rm bm-server.sh
  echo -e "${GREEN}Bigemap Server uninstall completed.${NC}"
}
