#!/bin/bash

# (c) 2014-2015 Sam Nazarko
# email@samnazarko.co.uk

fb_restore() {
	[ -e /var/run/fb_resolution ] && /bin/fbset $(cat /var/run/fb_resolution)
	/bin/fbset -depth 8 && /bin/fbset -depth 16
	echo 1 >/sys/class/vtconsole/vtcon1/bind
}

set_font() {
	vres=$(/bin/fbset | grep geometry | awk '{print $3}')
	if ((vres<=660))
	then
		setfont OSMC-Lat15-Terminus14.psf.gz
	elif ((661<=vres && vres<=890))
	then
		setfont OSMC-Lat15-TerminusBold22x11.psf.gz
	elif ((891<=vres))
	then
		setfont OSMC-Lat15-TerminusBold28x14.psf.gz
	fi
}

export TERM=linux
chown osmc:osmc /sys/class/vtconsole/vtcon*/bind

if [ "$1" = "stop" ]; then
	fb_restore
	exit
fi

trap 'wait; exit 0' SIGTERM

chmod a+rw /dev/tty1
/usr/bin/setterm --blank 0 </dev/tty1 >/dev/tty1

boot_space=$(df -B 1M /boot | sed 1d | awk '{ print $4}')
root_space=$(df -B 1M / | sed 1d | awk '{ print $4}')

/sbin/checkmodifier; CODE=$?
if [ $CODE -eq 0 ] && [ -e /dist_upgrade_wanted ]; then CODE=5; fi
case $CODE in
4)
	/usr/bin/setterm --cursor on >/dev/tty1
	systemctl start getty@tty1
	exit 0
	;;
5)
	if [ -e /dist_upgrade_wanted ]; then apt-get clean; fi
	rm /dist_upgrade_wanted

	set_font
	if [ $boot_space -lt 30 ] || [ $root_space -lt 300 ]; then
		dialog --backtitle "Package configuration" --title "OSMC Update" --infobox "\n        WARNING! Insufficient disk space available to complete update.\n\n30MB required on /boot (${boot_space}MB available) and 300MB required on root partition. (${root_space}MB available)\n\nPlease free sufficient disk space and check for updates to try again. Rebooting in 60 seconds..." 11 85 > /dev/tty1
		sleep 60; reboot
	fi

	dialog --backtitle "Package configuration" --title "OSMC Update" --infobox "\n     Establishing a connection to the OSMC update servers..." 5 71 > /dev/tty1

	count=60
	while [ $count -gt 0 ]; do
		if grep -q nfsroot /proc/cmdline; then
			ping -c 1 -W 2 apt.osmc.tv >/dev/null 2>&1 && break
		else
			if connmanctl state | grep -iq 'online'; then break; fi
		fi
		sleep 5; let count-=5
	done

	if [ $count -lt 1 ]
	then
		dialog --backtitle "Package configuration" --title "OSMC Update" --infobox "\n    No internet connection was found. Rebooting in 20 seconds." 5 71 > /dev/tty1
		sleep 20; reboot
	fi
        dialog --backtitle "Package configuration" --title "OSMC Update" --infobox "\n         Updating the list of available OSMC packages..." 5 71 > /dev/tty1
        err=$(apt-get -qq update); if [ -n "$err" ]
        then
                dialog --backtitle "Package configuration" --title "OSMC Update" --infobox "The following errors occured while searching for new packages:\n\n${err}\n\nContinuing in 20 seconds..." 0 0 > /dev/tty1
                sleep 20
        fi
        systemctl start manual-update
        exit 0
        ;;
esac

if [ ! -e /var/run/fb_resolution ]; then
	/bin/fbset | grep geometry | awk '{print "-xres "$2" -yres "$3" -vxres "$4" -vyres "$5}' > /var/run/fb_resolution
fi

if [ $root_space -lt 100 ]; then
	set_font
	dialog --infobox "\n            WARNING! Free disk space remaining is only ${root_space}MB.\n\nKodi will be unable to start if disk space is completely exhausted. Please free sufficient disk space as soon as possible. Continuing in 10 seconds..." 8 80 > /dev/tty1
	sleep 10
fi

while true; do
        if [ -f /home/osmc/.factoryreset ]; then rm -rf /home/osmc/.kodi && rm /home/osmc/.factoryreset; fi
	if [ -f /tmp/guisettings.restore ]; then mv /tmp/guisettings.restore /home/osmc/.kodi/userdata/guisettings.xml; fi
        # clean zero-byte database files that prevent migration/startup
        for file in /home/osmc/.kodi/userdata/Database/*.db; do [ -s $file ] || rm -f $file; done
	KODI=/usr/lib/kodi/kodi.bin
	setcap 'cap_net_bind_service=+ep' $KODI
	starttime=$(date +%s)
	echo "Starting Kodi..."
	sudo -u osmc LIRC_SOCKET_PATH=/var/run/lirc/lircd $KODI --standalone -fs; CODE="$?"
	endtime=$(date +%s)
	seconds=$(($endtime - $starttime)); hours=$((seconds / 3600)); seconds=$((seconds % 3600)); minutes=$((seconds / 60)); seconds=$((seconds % 60))
	echo "Kodi exited with return code $CODE after $hours hours, $minutes minutes and $seconds seconds"

	fb_restore
	case $CODE in
	64|66 )
		exit 0;;
	esac
	chmod a+rw /dev/tty1
	systemctl status getty@tty1 >/dev/null; GETTY=$?
	if [ "$GETTY" -ne 0 ]; then
		chvt 1
		/usr/bin/setterm --cursor off >/dev/tty1
		/usr/bin/clear >/dev/tty1
		/usr/bin/ply-image "$CODE"
		read -n 1 -s dump </dev/tty1
		read -n 1 -s -t 10 key </dev/tty1
	fi
	if [ "$key" = $'\e' -o "$GETTY" -eq 0 ]; then
		set_font
		/bin/fbset -depth 8 && /bin/fbset -depth 16
		/usr/bin/setterm --cursor on >/dev/tty1
		/bin/systemctl start getty@tty1
		count=30
		loggingin=1
		while [ $count -gt 0 ]; do
			terminal_live=$(/bin/ps aux | /usr/bin/grep -v grep | /usr/bin/grep -c ^root.*tty.*login)
			/usr/bin/who | /usr/bin/awk '{print $2}' | /usr/bin/grep -q tty1; loggedin="$?"
			if [ $terminal_live -gt 0 ] && [ $loggingin -gt 0 ] && [ $loggedin -ne 0 ]; then
				count=5
			elif [ $loggedin -eq 0 ]; then
				count=5
				loggingin=0
			else
				let count=count-5
			fi
			/bin/sleep 5
		done
		/bin/systemctl stop getty@tty1
	fi
done
