# Copyright (C) 2016 BROADSoftware
#
# This file is part of HADeploy
#
# HADeploy is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# HADeploy is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with HADeploy.  If not, see <http://www.gnu.org/licenses/>.

MYDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )/." && pwd )"
CWD=$(pwd)


function usage {
    echo 'USAGE:'
	echo 'hadeploy'
	echo '     --src <srcFile> [--src <srcFile>, ...]'
	echo '     [--scope <scope> [--scope <scope>]]'
	echo '     [--noScope <scope> [--noScope <scope>]]'
	echo '     [--askVaultPassword] [--vaultPasswordFile <vaultPasswordFile>]'
	echo '     [--workingFolder <workingFolder>]'
	echo '     [--loggingConf <loginConfFile>]'
	echo '     --action <deploy|remove|dumpvars|stop|start|status|dumpvars|none>'
	echo '  or'
	echo 'hadeploy --version'
}

VERSION="0.6.0"
SRC=""
WF=""
VAR=""
LOGF=""
VAULT_PASSWORD=""
VERBOSE=""
SCOPE=""
NO_SCOPE=""

while [[ $# > 0 ]]
do
	case $1 in
		--src)
			SRC="$SRC $2"
			shift
		;;
		--scope)
			SCOPE="$SCOPE $2"
			shift
		;;
		--noScope)
			NO_SCOPE="$NO_SCOPE $2"
			shift
		;;
		--var)
			SRC="$SRC $2"
			shift
		;;
		--workingFolder)
			WF=$2
			shift
		;;
		--loggingConf)
			LOGF=$2
			shift
		;;
		--action)
			#ACTION=$(echo "$2" | tr '[:upper:]' '[:lower:]')
			ACTION=$2 
			shift
		;;
		--askVaultPassword)
			VAULT_PASSWORD="--ask-vault-pass"
		;;
		--vaultPasswordFile)
			if [[ "${2:0:1}" == / || "${2:0:2}" == ~[/a-z] ]]
			then
				VAULT_PASSWORD="--vault-password-file $2"
			else
    			VAULT_PASSWORD="--vault-password-file ${CWD}/$2"
			fi
			shift
		;;
		--v)
			VERBOSE="-v"
		;;
		--vv)
			VERBOSE="-vv"
		;;
		--vvv)
			VERBOSE="-vvv"
		;;
		--help)
			usage
			exit 0
		;;
		--version)
			echo "HADeploy v$VERSION"
			echo "Location:${MYDIR}"
			exit 0
		;;
		*)
			echo "Unknown parameter $1"
			usage
			exit 1
		;;
	esac
	shift
done

if [ "$SRC" = "" ]; then echo "At least one --src file must be defined"; usage; exit 1; fi
if [ "$ACTION" = "" ]; then usage; exit 1; fi

# Patch for compatibility
if [ "$ACTION" = "DEPLOY" ]; then ACTION=deploy; fi
if [ "$ACTION" = "REMOVE" ]; then ACTION=remove; fi
if [ "$ACTION" = "NONE" ]; then ACTION=none; fi



if [ "$LOGF" = "" ]
then
	LOGOPT="" 
else
	LOGOPT=" --yamlLoggingConf $LOGF"
fi

if [ "$WF" = "" ]
then
	WF="/tmp/hadeploy-work-$$"
else
	WF="${WF}/hadeploy-work"
	rm -rf "${WF}"
fi
mkdir -p ${WF}

if [ "$ACTION" != "dumpvars" ]
then
	echo "WORKING_FOLDER: $WF"
fi


if [ -x "$MYDIR/hadeploy-main" ]
then 
	CMD="$MYDIR/hadeploy-main"				# We are in setup.py installed layout 
elif [ -f $MYDIR/../lib/hadeploy/core/main.py ]
then
	export PYTHONPATH=$MYDIR/../lib/
	CMD="python $MYDIR/../lib/hadeploy/core/main.py"	# We are in raw source layout
else 
	echo "Unable to find appropriate launcher"; exit 1
fi

if [ "$SCOPE" != "" ]
then
	SCOPE="--scope $SCOPE"
fi
if [ "$NO_SCOPE" != "" ]
then
	NO_SCOPE="--noScope $NO_SCOPE"
fi

SRC="hadeploy_action=${ACTION} ${SRC}"

set +x
$CMD --workingFolder $WF $LOGOPT --src $SRC $SCOPE $NO_SCOPE --action $ACTION
if [ $? -ne "0" ]
then 
	exit 1
fi


case $ACTION in
	none)
	;;
	dumpvars)
	;;
	*)
		bash -c "cd $WF; ansible-playbook ${VAULT_PASSWORD} ${VERBOSE} ${ACTION}.yml"
	;;
esac
		

						