#!/usr/bin/sudo /bin/bash

#first check if we are installed or liveCD
if [ -d /rofs/etc ]; then
   echo "IGP-init: Running from live CD, must install before running product."
   echo "IGP-init: Starting installer"
   echo
   notify-send "Starting installer"

   sudo umount -l /dev/mapper/ventoy
   sudo ubiquity &
   exit 0
fi

#set to 1, for debugging, CAN BE SET TO 1 IF DEBUG FILE IS PRESENT IN TERMINAL HOME
MOUNT_CORE_RW=0
#!!!Additionaly!!!
#if this file exists, core will be mounted RW!!!
#use this for persistent rw mounting between core updates!
if [ -f /var/IGP/.mount_core_rw ]; then
	MOUNT_CORE_RW=1
fi


#path to packages folder
PACKAGES=/var/IGP/packages

tryToUpdate(){
#this will update .pack.update and .pack.rw.update!!
dfiles=`find $PACKAGES -type f -name "*.pack*.update" -print`
for UPDATE_PACK_PATH in $dfiles
do
        #is update package present?
        PACK_BASE_PATH=${UPDATE_PACK_PATH%.pack.update}
	#now test .pack.update if it matches MD5 file move the file over main.., if not delete it
        if [ -f $UPDATE_PACK_PATH ]; then
		if [ -f $UPDATE_PACK_PATH.md5 ]; then
		        if [ "`md5sum $UPDATE_PACK_PATH|cut -d' ' -f1-1`" != "`cat $UPDATE_PACK_PATH.md5|cut -d' ' -f1-1`" ]; then
        	           rm -rf $UPDATE_PACK_PATH
			   echo "Failed to update ${PACK_BASE_PATH/".old."}, checksum failure!"
			   notify-send "Failed to update ${PACK_BASE_PATH/".old."}, checksum failure!"
        	        fi
		fi
	        mv $UPDATE_PACK_PATH ${UPDATE_PACK_PATH%.update}
		rm -rf $UPDATE_PACK_PATH.md5
		rm /var/IGP/.firstrun_* -rf #s tem zagotovimo, da se bo ob prvem zagonu paketa posodobilo vse datoteke iz pack_firstrun
	        echo
	        echo "Updated ${PACK_BASE_PATH/".old."} package!"
	        notify-send "Updated ${PACK_BASE_PATH/".old."} package!"
        else
	        echo "No update files found for $PACK_BASE_PATH!"
        fi
done
}
tryToUpdate;



##___________________________________________________________________________
## THIS SECTION MOUNTS TERMINAL_HOME RW PACKAGE and invokes pack-init.sh inside of PACKAGE
##---------------------------------------------------------------------------
#ze prej smo ga update-ali..
MOUNTPOINT=/var/IGP/terminal_home
PACK_PATH=$PACKAGES/terminal_home.pack.rw

#is terminal_home pack present in $PACKAGES???
if [ ! -f $PACK_PATH ]; then
	echo
	echo "Terminal_home package is missing. It is a good idea to have it in $PACKAGES."
	echo "Please run the script reset_terminal_home.sh in order to make an empty package."
	notify-send "Terminal_Home pack is missing. Using plain folder..."
else
	fsck.ext4 -cDfty -C 0 $PACK_PATH
	mkdir -p $MOUNTPOINT
	mount -o rw -o loop  $PACK_PATH $MOUNTPOINT
	if [ $? -eq 0 ]; then
		echo "IGP-init: $PACK_PATH mounted on $MOUNTPOINT."
	else
		echo "IGP-init: ERROR occoured while mounting $PACK_PATH on $MOUNTPOINT."
		exit 1
	fi

	#load package if loader exists
	if [ -x $MOUNTPOINT/pack-init.sh ]; then
		cd $MOUNTPOINT
		echo "IGP-init: Starting terminal_home pack-load.sh from inside package..."
		./pack-init.sh
		if [ $? -eq 0 ]; then
			echo "IGP-init: TerminalHome loaded."
			notify-send "IGP-init: TerminalHome loaded."
		else
			echo "IGP-init: ERROR loading TerminalHome pack."
			notify-send "IGP-init: ERROR loading TerminalHome pack."
			exit 1
		fi
	fi

#	if [ -f /var/IGP/terminal_home/debug ]; then
#		MOUNT_CORE_RW=1
#	fi
fi #IF packge present in $PACKAGES

##___________________________________________________________________________
## THIS SECTION RUNS PACKAGE INSTALLER IF CORE PACK IS MISSING ON THE SYSTEM
##---------------------------------------------------------------------------
MOUNTPOINT=/home/IGP
PACK_INSTALLER=/var/IGP/packages/pack-installer.sh
PACK_PATH=''
#mora biti po update!!, ker paketa ga morda prej ni bilo
if [ -f /var/IGP/backup.chk ]; then
#ce imamo backup.chk potem delamo tenerife
	PACK_PATH=`find  $PACKAGES/.old.*core*.pack -print`
fi
if [ -z $PACK_PATH ]; then
#ce nismo tenerife, potem loadamo core.pack
	PACK_PATH=`find  $PACKAGES/*core*.pack -print`
fi


if [ -z "$PACK_PATH" ]; then
#is CorePack present in $PACKAGES???
	echo
	echo "Core package is missing. Running Package Installer."
	notify-send "Core package is missing. Running Package Installer."

	#START PACKAGES INSTALLER
	if [ -f "$PACK_INSTALLER" ]; then
	        lxterminal --command="$PACK_INSTALLER no_reboot" --title="$PRODUCT_NAME PACKAGE INSTALLER" --geometry=170x70 &
	        wait $!
        	INSTALLER_RESULT=$?
	else
		echo
		echo "Can not find package installer in $PACK_INSTALLER."
		notify-send "Can not find package installer in $PACK_INSTALLER."
	fi
fi

#Ponovno, ker paketa morda pred PackageInstaller-jem ni bilo
tryToUpdate;

##___________________________________________________________________________
## THIS SECTION MOUNTS CORE PACKAGE and invokes pack-init.sh inside of PACKAGE
##---------------------------------------------------------------------------
#mount core
if [ -z "$PACK_PATH" ]; then
	echo
	echo "Core package is missing. Can not continue. Please install CorePack in $PACKAGES."
	notify-send "Core package is missing. Can not continue. Please install CorePack in $PACKAGES."
else
	#mount the CorePackage
	mkdir -p $MOUNTPOINT
	if [ $MOUNT_CORE_RW -gt 0 ]; then
		fsck.ext4 -cDfty -C 0 $PACK_PATH
		mount -o rw -o loop  $PACK_PATH $MOUNTPOINT
	else
		mount -o ro -o loop  $PACK_PATH $MOUNTPOINT
	fi

	if [ $? -eq 0 ]; then
		echo "IGP-init: ${PACK_PATH/".old."} mounted on $MOUNTPOINT."
	else
		echo "IGP-init: ERROR occoured while mounting ${PACK_PATH/".old."} on $MOUNTPOINT."
		exit 1
	fi

	#load package if loader exists
	if [ -x $MOUNTPOINT/pack-init.sh ]; then
		cd $MOUNTPOINT
		echo "IGP-init: Starting pack-init.sh from inside package..."
		./pack-init.sh
		if [ $? -eq 0 ]; then
			echo "IGP-init: CorePack loaded."
			notify-send "IGP-init: CorePack loaded."
		else
			echo "IGP-init: ERROR loading CorePack."
			notify-send "IGP-init: ERROR loading CorePack."
			exit 1
		fi
	fi
fi # if core.pack in $PACKAGES

##___________________________________________________________________________
## THIS SECTION MOUNTS CHECKSUMMER PACKAGE and invokes pack-init.sh inside of PACKAGE
##---------------------------------------------------------------------------
#ze prej smo ga update-ali..
MOUNTPOINT=/home/CHECKSUMMER
PACK_PATH=$PACKAGES/checksummer.pack

#is CHECKSUMMER pack present in $PACKAGES???
if [ -z "$PACK_PATH" ]; then
	echo
	echo "Checksummer package is missing. Please install checksummer.pack in $PACKAGES."
	notify-send "Checksummer package is missing. Please install checksummer.pack in $PACKAGES."
else
	mkdir -p $MOUNTPOINT
	mount -o ro -o loop  $PACK_PATH $MOUNTPOINT
	if [ $? -eq 0 ]; then
		echo "IGP-init: $PACK_PATH mounted on $MOUNTPOINT."
	else
		echo "IGP-init: ERROR occoured while mounting $PACK_PATH on $MOUNTPOINT."
		exit 1
	fi

	#load package if loader exists
	if [ -x $MOUNTPOINT/pack-init.sh ]; then
		cd $MOUNTPOINT
		echo "IGP-init: Starting checksummer pack-load.sh from inside package..."
		./pack-init.sh
		if [ $? -eq 0 ]; then
			echo "IGP-init: Checksummer loaded."
			notify-send "IGP-init: Checksummer loaded."
		else
			echo "IGP-init: ERROR loading Checksummer pack."
			notify-send "IGP-init: ERROR loading Checksummer pack."
			exit 1
		fi
	fi
fi #IF packge present in $PACKAGES

#if update all is pending, make it another try
if [ -z "$UPDATE_ALL_IN_PROGRESS" ]; then
#default location
        UPDATE_ALL_IN_PROGRESS='/var/IGP/.update_all_in_progress'
fi
if [ -f $UPDATE_ALL_IN_PROGRESS ]; then
	nohup /var/IGP/slot_update_all.sh &
fi

exit 0
