#!/bin/bash
. /home/IGP/common.config #here is configuration of ROULETTE and FC5 locations


GREEN='\033[0;32m' #red
RED='\033[0;31m' #red
NC='\033[0m' # No Color

if [ -f $ROULETTE/pack.info ]; then
	PACK_NAME=`cat $ROULETTE/pack.info |jq -r '.PackName'`
	if [ "$PACK_NAME" == "null" ]; then
		PACK_NAME=""
	fi
fi

if [[ -z "$PACK_NAME" ]]; then
     	echo
        echo "Please enter pack name to download from FTP(uppercase, lowercase is important!)."
       	echo "Example: 'core_gameart' or  'core_roulette_tronius' or 'core_lady_enigma':"
        read PACK_NAME
fi

echo "Updating core '$PACK_NAME'..."
echo

#preberemo jurisdikcijo, na FTP-ju bomo iskali to mapo
echo
PACK_VERSION=`cat $ROULETTE/pack.info |jq -r '.Version'`

if [ -x $ROULETTE/sqlexec.sh ]; then
	JURISDICTION=`echo "select coalesce(RES_STR_VALUE, '') from GET_PARAM('GLOBAL', 'JURISDICTION', 11)" | $ROULETTE/sqlexec.sh |head -6|tail -1|sed -e 's/  *$//'|sed 's/^M$//'|sed 's/[ ^t]*$//'`
	if [ -n "$JURISDICTION" ]; then
        	echo "Downloading packages for JURISDICTION=$JURISDICTION"
	else
        	echo "Can not fetch JURISDICTION info; downloading from DEFAULT source"
	        JURISDICTION=DEFAULT
	fi
	VERSION_FOLDER=`echo "select coalesce(RES_STR_VALUE, '') from GET_PARAM('GLOBAL', 'FTP_VERSION_FOLDER', 11)" | $ROULETTE/sqlexec.sh |head -6|tail -1|sed -e 's/  *$//'|sed 's/^M$//'|sed 's/[ ^t]*$//'`
	if [ -n "$VERSION_FOLDER" ]; then
        	echo "Downloading packages for VERSION=$VERSION_FOLDER"
	else
	        echo "Can not fetch FTP_VERSION_FOLDER info from database; defaulting to V$PACK_VERSION"
        	VERSION_FOLDER="V$PACK_VERSION"
	fi
else
	echo "WARNING: No pack is mounted! CAN NOT DETERMINE JURISDICTION AND PACK VERSION"
	JURISDICTION=DEFAULT
	PACK_VERSION=7.3
       	VERSION_FOLDER="V$PACK_VERSION"
	echo "DEFAULTING TO: JURISDICTION=$JURISDICTION and VERSION=$VERSION_FOLDER"
fi


PARAMS=""
[[ $@ == *"force_yes"* ]] && PARAMS=$PARAMS" force_yes"
[[ $@ == *"force_ftp"* ]] && PARAMS=$PARAMS" force_ftp"
[[ $@ == *"force_usb"* ]] && PARAMS=$PARAMS" force_usb"
[[ $@ == *"dependency_only"* ]] && PARAMS=$PARAMS" dependency_only"
./update_pack.sh $PACK_NAME $VERSION_FOLDER $JURISDICTION $PARAMS

RETURN=$?

#if error downloading, exit.
# codes under 10 indicate an actual change in package occured, under 100 mean success but no change in package, above 100 are errors
if [[ $RETURN -gt 10 ]]; then
	if [[ $RETURN -gt 100 ]]; then
		echo -e "$RED""ERROR updating core '$PACK_NAME' - exit code $RETURN.""$NC"
	else
		echo -e "$GREEN""Core '$PACK_NAME' is already the newest version.""$NC"
	fi
        exit $RETURN
else
	if [[ $@ == *"dependency_only"* ]];then
		echo -e "$GREEN""Successfully updated dependencies""$NC"
	else
		echo -e "$GREEN""Successfully updated core '$PACK_NAME'.""$NC"
		# update success, remove first run!
#firstrun je samo za core.., mogoce bi morali imeti za vsak paket svojega in prestaviti tole v update_pack.sh
		rm $FIRSTRUN
	fi
fi


if [[ $@ != *"no_reboot"* ]]; then
	if [[ $@ == *"reboot"* ]]; then
		ans=yes
	else
		echo
		echo "REBOOT [yes,NO]?"
		read ans
	fi
	[[ -z $ans ]] && ans="no" && echo $ans #default
	if [ $ans == "yes" ] || [ $ans == "YES" ]; then
		if [ -f /var/IGP/ext/ultra_fast_reboot.sh ]; then
			/var/IGP/ext/ultra_fast_reboot.sh
		elif [ -f /home/IGP/ultra_fast_reboot.sh ]; then
			/home/IGP/ultra_fast_reboot.sh
		else 
			init 6
		fi
	fi
fi

exit $RETURN
