#!/bin/bash

platform="Linux" # Assumed
uname=`uname`
if [[ $uname == "Darwin" ]]; then
	platform = "MacOS / OSX"
elif [[ $uname == MINGW* ]]; then
	platform = "Windows"
fi

usage(){
  echo "Usage: $0 <command> [options]"
  echo
  echo "This program helps to manage the setup/teardown of the docker containers for running WebODM. We recommend that you read the full documentation of docker at https://docs.docker.com if you want to customize your setup."
  echo 
  echo "Command list:"
  echo "	start		Start WebODM"
  echo "	stop		Stop WebODM"
  echo "	update		Update WebODM to the latest release"
  echo "	rebuild		Rebuild all docker containers and perform cleanups"
  exit
}

check_command(){
	check_msg="\e[92m\e[1m OK\e[0m\e[39m"
	hash $1 2>/dev/null || not_found=true 
	if [[ $not_found ]]; then
		check_msg="\e[91m can't find $1! Check that the program is installed before launching WebODM. $2\e[39m"
	fi

	echo -e "Checking for $1... $check_msg"
	
	if [[ $not_found ]]; then
		exit 1
	fi
}

environment_check(){
	check_command "docker" "https://www.docker.com/"
	check_command "git" "https://git-scm.com/downloads"
	check_command "python" "https://www.python.org/downloads/"
	check_command "pip" "Run \e[1msudo easy_setup pip\e[0m"
	check_command "docker-compose" "Run \e[1mpip install docker-compose\e[0m"	
}

run(){
	echo $1
	$1
}

start(){
	run "docker-compose -f docker-compose.yml -f docker-compose.nodeodm.yml up"
}

rebuild(){
	run "docker-compose down"
	run "rm -fr node_modules/"
	run "rm -fr nodeodm/external/node-OpenDroneMap"
	run "docker-compose build --no-cache"
	echo -e "\e[1mDone!\e[0m You can now start WebODM by running ./$0 start"
}

if [[ $1 = "start" ]]; then
	environment_check
	echo "Starting WebODM..."
	start
elif [[ $1 = "stop" ]]; then
	environment_check
	echo "Stopping WebODM..."
	docker-compose down
elif [[ $1 = "rebuild" ]]; then
	environment_check
	echo  "Rebuilding WebODM..."
	rebuild
elif [[ $1 = "update" ]]; then
	echo "Updating WebODM..."
	git pull origin master
	rebuild
else
	usage
fi
