#!/bin/bash

# righthand script
# Used to wrap custom operations inside one script
#

############################# utility functions ################################

RH_ROOT_DIR=$(dirname $(readlink -f "$0"))

cd_rh()
{
	cd $RH_ROOT_DIR
}

cd_root()
{
	cd_rh
	cd ..
}

cd_source()
{
	cd_rh
	cd ../source/
}


########################## RH user defined functions ###########################

# all user defined operation starts with "rh_op_" prefix.

rh_op_compile()
{
	_rh_try_ret_help "Compiles the project including javascript transpilation"
	set -e
	cd_source
	mvn compile
	mvn exec:java compile -Dexec.args="compile"
}

rh_op_test()
{
	_rh_try_ret_help "Run unit tests"
	set -e
	cd_source
	mvn test
}

rh_op_prepare()
{
	_rh_try_ret_help "Prepares the CI environment. eg: builds the images used in the build, test, release stages"
	set -e
	cd_root
}


rh_op_package()
{
	_rh_try_ret_help "Package runnable artifact"
	set -e
	cd_source
	mvn package
	cp target/holdem.jar ../WD
}

rh_op_integrate()
{
	_rh_try_ret_help "Run integration tests"
	# nothing to do yet
	exit 0
}

rh_op_full_release()
{
	_rh_try_ret_help "Do the full cycle from build to release"
	rh_op_compile
	rh_op_test
	rh_op_package
	rh_op_build_release_image
}

rh_op_build_release_image()
{
	_rh_try_ret_help "Build a runnable docker image."
	cd_root
	pwd
	docker build -t holdem:$(rh_op_get_version_tag) -f ./dockerproduction/Dockerfile WD
}

rh_op_publish_release_image()
{
	_rh_try_ret_help "Pushes the build docker image to the docker registry spcified by \$DOCKER_TARGET_REGISTRY_ADDRESS env variable."
	if [ -z $DOCKER_TARGET_REGISTRY_ADDRESS ];
	then
		echo "\$DOCKER_TARGET_REGISTRY_ADDRESS env variable not specified. exiting"
		exit 1
	fi
	VER=holdem:$(rh_op_get_version_tag)
	TO=$DOCKER_TARGET_REGISTRY_ADDRESS/$VER
	docker tag $VER $TO
	docker push $TO
}


rh_op_release()
{
	_rh_try_ret_help "Runs build_release_image and publish_release_image in sequence"
	rh_op_build_release_image
	rh_op_publish_release_image
}

rh_op_run()
{
	_rh_try_ret_help "Run project"
	cd_root
	cd WD
	java -jar holdem.jar
}

rh_op_get_version_tag()
{
	echo $(git describe --tags --dirty 2> /dev/null || echo $(git rev-parse --abbrev-ref HEAD)"-"$(git describe --dirty --always))
}

############################# RH helper functions ##############################

_rh_try_ret_help()
{
	if [ ! -z $RH_RUNTIME_REQUEST_HELP ]
	then
		echo $1
		exit 1;
	fi
}

_rh_get_universe_name()
{
	if [ ! -z $RH_UNIVERSE_NAME ]
	then
		echo "$RH_UNIVERSE_NAME"
	elif [ -f "$(dirname $0)/.rh_universe_name" ]
	then
		cat "$(dirname $0)/.rh_universe_name"
	else
		echo $(dirname $(readlink -f "$0")) | grep -Po "[^/]+$"
	fi
}

############################ RH built-in functions #############################

rh_op_enter()
{
	_rh_try_ret_help 'Executes new bash shell that include rh command and all neighbor scripts ($PATH injection).'

	export PS1="RH("$(_rh_get_universe_name)"): $(bash -i -c 'echo $PS1') "
	export PATH="$PATH:"$(dirname $(readlink -f "$0"))
	EX="export PS1="$(printf "%q" "$PS1")";"
	EX+="complete -W \"${RH_FUNCTIONS[@]}\" 'rh';"
	exec /bin/bash --init-file <(echo "$EX")
}

rh_op_echo()
{
	_rh_try_ret_help "Test command, echoes back every argument"
	echo "echo in RH: $@"
}

rh_op_help()
{
	_rh_try_ret_help "Returns RH help with the list of all defined functions"

	echo "RH stands for 'right hand'. It is a wrapper script wherein you can collect tiny commands."
	echo "Specify one righthand function from the followings:"

	RH_RUNTIME_REQUEST_HELP=true

	for f in ${RH_FUNCTIONS[@]}
	do
		# if the command has help
		if type "rh_op_"$f | grep -q _rh_try_ret_help
		then
			echo -e "\t$f - "$("rh_op_"$f)
		else
			echo -e "\t$f"
		fi
	done
}

rh_op_print_universe()
{
	_rh_try_ret_help 'Prints the logical name of the RH command repository. You can change by specifying $RH_UNIVERSE_NAME or editing the content of the rh command neighbor file .rh_universe_name'
	echo $(_rh_get_universe_name)
}

############################## Application area ################################

# Collection available commands
RH_FUNCTIONS=()

for func in `declare -F | grep -Po '(?<=rh_op_).*$' | sort`
do
	RH_FUNCTIONS+=($func);
done

# print help if nothing specified

if [ $# '<' '1' ]
then
	rh_op_help
	exit 1
fi

# run command
for f in ${RH_FUNCTIONS[@]}
do
	if [ "$1" "=" "$f" ]
	then
		"rh_op_"$f "${@:2}"
		exit $?
	fi
done

echo "Righthand function $1 not found, so now printing help..."
rh_op_help
exit 1
