set -x

if [[ $# -lt 1 || $# -gt 6 ]]
then
	PARAM_ERROR="Invalid number of argument."
fi

APPCORE_VER=appcore-3.0
OOBLIB_VER=ooblib-1.0

INSTALL_PATH=$1
APPCORE_OPTIONS=""
NUM_PARAMS_USED=0
COPY="yes"
CONFIG="yes"
DEFAULT_CONFIG="yes"

shift

while [ $# -ge 1 ]
do
	case $1 in
	"--install-type")	
			INSTALL_TYPE=$2
			case $INSTALL_TYPE in
				"standalone") 
					APPCORE_OPTIONS="$APPCORE_OPTIONS,{disabled_modules,[https_handler,https_server,oob_handler,snmp_handler]}"
					;;
				"agent")
					APPCORE_OPTIONS="$APPCORE_OPTIONS,{disabled_modules,[oob_handler,snmp_handler]}"
					;;
				"complete")
					;;
				*) PARAM_ERROR="Invalid Installtype."
			esac
			NUM_PARAMS_USED=2
			;;
						
	"--password") 		
			APPCORE_OPTIONS="$APPCORE_OPTIONS,{admin_password,\"$2\"}"
			NUM_PARAMS_USED=2
			;;	
			
	"--agent-version") 		
			APPCORE_OPTIONS="$APPCORE_OPTIONS,{agent_version,\"$2\"}"
			NUM_PARAMS_USED=2
			;;	
			
	"--config-only") 		
			COPY="no"
			NUM_PARAMS_USED=1
			;;	

	"--no-default-config") 		
			DEFAULT_CONFIG="no"
			NUM_PARAMS_USED=1
			;;	
	
	"--no-config") 		
			CONFIG="no"
			NUM_PARAMS_USED=1
			;;	

	*) 					
			PARAM_ERROR="Invalid Parameter."
			break;;
	esac
	shift $NUM_PARAMS_USED
done


if [[ ! -z $PARAM_ERROR || $# -ne 0 ]]
then
	echo "Invalid Options : $PARAM_ERROR"
	echo "install <installation path> [--password <admin password>] [--install-type <standalone | agent | complete>]"
	exit 2
fi

if [ -n $APPCORE_OPTIONS ]
then
	APPCORE_OPTIONS=${APPCORE_OPTIONS:1}
fi

APPCORE_OPTIONS="[$APPCORE_OPTIONS]"

if [ $COPY == "yes" ]
then

	mkdir $INSTALL_PATH
	tar -xzf erlang.tar.gz -C $INSTALL_PATH
	
	ERTS=`ls $INSTALL_PATH/erlang | grep erts`	
	sed 's|ROOTDIR=.*|'"ROOTDIR=$INSTALL_PATH/erlang"'|g' $INSTALL_PATH/erlang/bin/erl > $INSTALL_PATH/erlang/$ERTS/bin/erl
	
	cp -f $INSTALL_PATH/erlang/erts-*/bin/erl $INSTALL_PATH/erlang/bin/erl

	service Appcore stop 2>/dev/null

	tar -xzf appcore.tgz -C $INSTALL_PATH
	mv $INSTALL_PATH/lib $INSTALL_PATH/appcore

	cp -rf ../include $INSTALL_PATH
	cp -rf ../samples $INSTALL_PATH
	cp -f ../readme.txt $INSTALL_PATH

	# copy Appcore erlang files
	mkdir $INSTALL_PATH/erlang/lib/$APPCORE_VER
	tar -xzf appcore_ebin.tgz -C $INSTALL_PATH/erlang/lib/$APPCORE_VER
	
	# copy ooblib erlang files
	mkdir $INSTALL_PATH/erlang/lib/$OOBLIB_VER
	tar -xzf ooblib_ebin.tgz -C $INSTALL_PATH/erlang/lib/$OOBLIB_VER

	sed 's|APPCORE_INSTALL_PATH|'"$INSTALL_PATH"'|g' Appcore > /etc/init.d/Appcore
	chmod 755 /etc/init.d/Appcore
	chkconfig --add Appcore
	chkconfig Appcore on

	#chcon for security. || true - to continue even after failing
	chcon -t texrel_shlib_t $INSTALL_PATH/appcore/* 1>/dev/null 2>&1 || true
fi
if [ $CONFIG == "yes" ]
then
	if [ $DEFAULT_CONFIG == "yes" ]
	then
		EVAL_FUN="install:configure($APPCORE_OPTIONS)"	
	else
		EVAL_FUN="install:configure(nodefaults,$APPCORE_OPTIONS)"
	fi
	ERL_PATH=$INSTALL_PATH/erlang
	pushd $ERL_PATH/lib/$APPCORE_VER/ebin

	ERL_OPTIONS=" -noshell -eval $EVAL_FUN -s erlang halt"

	$ERL_PATH/bin/erl  $ERL_OPTIONS 

	#Set permissions for cookie file
	chmod 400 .erlang.cookie
	
	popd

	if [ $? -ne 0 ]
	then
		echo "**Installation Failed. $POSSIBLE_ERROR"
	fi

fi
