#!/bin/sh
OTA=com.lab126.blanket.ota
LOG=/mnt/us/Komic_update_log.txt
LOCALSTORAGE=/mnt/us/extensions/komic/bin/resource/LocalStorage/file__0.localstorage
GALLERY="/mnt/us/documents/Gallery"
KOMIC="/mnt/us/extensions/komic"
UPSTART="/etc/upstart/komic_event_handler.conf"

progress()
{
	lipc-send-event $OTA otaSplashProgress -i $1
	local x=48
	local y=11
	eips 0 $y "                                                ";
	eips $((($x - $(expr length "$2")) / 2)) $y "$2"
	sleep 3
}

fail()
{
	lipc-send-event $OTA otaSplashError -s "${1}"
	sleep 10
	exit 1;
}

progress 25 "Checking if Komic is installed"
varkomic=$(sqlite3 /var/local/appreg.db "select handlerId from handlerIds where handlerId='com.silver18.komic001'")
varmemory=$(sqlite3 /var/local/appreg.db "select handlerId from properties where handlerId='com.silver18.komic001' and name='memUnloadLevel'")
mesquite=$(sqlite3 /var/local/appreg.db "select value from properties where handlerId='com.silver18.komic001' and name='command'" | grep mesquite)
komicsounds=$(sqlite3 $LOCALSTORAGE "select value from ItemTable where key = 'komicsounds'")
if [[ -n "$varkomic" ]]
then
	echo "Komic is already installed" >> $LOG
	komicinstalled=1
	
	if [[ -z "$mesquite" ]]
	then
		echo "Komic is not yet updated to mesquite" >> $LOG	
		sqlite3 /var/local/appreg.db "delete from properties where handlerId='com.silver18.komic001' and name='command'"
		sqlite3 /var/local/appreg.db "INSERT INTO properties (handlerId, name, value) VALUES ('com.silver18.komic001','command','/usr/bin/mesquite -l com.silver18.komic001 -c /mnt/base-us/extensions/komic/bin/')"
	else
		echo "Komic is already updated to mesquite" >> $LOG	
	fi
	
	if [ -d "$KOMIC" ]
	then
		echo "Komic folder present" >> $LOG
		komicpresent=1
	else
		echo "Komic folder not present" >> $LOG
	fi
	
	if [ -s $LOCALSTORAGE ]
	then
		echo "file__0.localstorage present" >> $LOG
		cp $LOCALSTORAGE /mnt/us/ || fail "Unable to copy file__0.localstorage"
		localstoragepresent=1
	else
		echo "file__0.localstorage not present" >> $LOG
	fi
	
else

	echo "Komic is not yet installed" >> $LOG
	sqlite3 /var/local/appreg.db "INSERT INTO handlerIds VALUES ('com.silver18.komic001')"
	sqlite3 /var/local/appreg.db "INSERT INTO properties (handlerId, name, value) VALUES ('com.silver18.komic001','command','/usr/bin/mesquite -l com.silver18.komic001 -c /mnt/base-us/extensions/komic/bin/')"
	sqlite3 /var/local/appreg.db "INSERT INTO properties (handlerId, name, value) VALUES ('com.silver18.komic001','unloadPolicy','unloadOnPause')"
	
fi

if [[ -n "$varmemory" ]]
then
	echo "Bigger memory space already present" >> $LOG
else
	echo "Bigger memory space not present" >> $LOG
	sqlite3 /var/local/appreg.db "INSERT INTO properties (handlerId, name, value) VALUES ('com.silver18.komic001','memUnloadLevel','150000')"
	sqlite3 /var/local/appreg.db "INSERT INTO properties (handlerId, name, value) VALUES ('com.silver18.komic001','memWarnLevel','71680')"
fi		

progress 50 "Copying Komic"
echo "--------------------------------" >> $LOG
if [[ -n "$komicinstalled" ]]
then
	echo "Updating Komic and managing file__0.localstorage" >> $LOG
	
	if [[ -n "$komicpresent" ]]
	then
		echo "Removing old Komic and unzipping new Komic" >> $LOG
		rm -rf $KOMIC || fail "Unable to delete Komic folder"
		unzip komic.zip -d /mnt/us/extensions/ || fail "Unable to unzip Komic"
	else
		echo "Unzipping new Komic" >> $LOG
		unzip komic.zip -d /mnt/us/extensions/ || fail "Unable to unzip Komic"
	fi
	
	if [[ -n "$localstoragepresent" ]]
	then
		echo "Restoring old file__0.localstorage" >> $LOG
		mv /mnt/us/file__0.localstorage $LOCALSTORAGE || fail "Unable to replace old file__0.localstorage"
		rm -f /mnt/us/file__0.localstorage || fail "Unable to delete old file__0.localstorage"
	else
		echo "Placing new file__0.localstorage" >> $LOG
		mv file__0.localstorage $LOCALSTORAGE || fail "Unable to place new file__0.localstorage"
	fi
	
	if [ -s $UPSTART ]
	then
		echo "Upstart script present" >> $LOG	
	else
		echo "Placing upstart scritp" >> $LOG
		mv komic_event_handler.conf $UPSTART || fail "Unable to place upstart script"
	fi
	
	if [[ "$komicsounds" = "OFF" ]]
	then
		touch /mnt/us/extensions/komic/bin/resource/Sounds/nosound
	fi
	
else

	echo "Installing Komic, placing plain file__0.localstorage and upstart script" >> $LOG
	unzip -o komic.zip -d /mnt/us/extensions/ || fail "Unable to unzip Komic"
	mv file__0.localstorage $LOCALSTORAGE || fail "Unable to place new file__0.localstorage"
	mv komic_event_handler.conf $UPSTART || fail "Unable to place upstart script"
	
fi

progress 75 "Creating Gallery folder"
if [ -d "$GALLERY" ]
then
	echo "Gallery folder present" >> $LOG
	unzip -o =README=.zip -d $GALLERY/ || fail "Unable to unzip README"
else
	echo "Creating gallery folder" >> $LOG
	mkdir -p $GALLERY
	unzip -o =README=.zip -d $GALLERY/ || fail "Unable to unzip README"
fi

progress 100 "Installation complete."
echo "Installation complete" >> $LOG
mv $LOG /mnt/us/extensions/komic/bin/resource/Logs/Komic_update_log.txt