#!/bin/sh

module_id() {
    # Get model name
    echo `grep "^Hardware" /proc/cpuinfo | sed -e "s/.*: *//" | tr a-z A-Z`
}

mount /proc 2>/dev/null

mount -o remount,rw /

case $1 in
'start')
	case `module_id` in
		"HP IPAQ H3100" )
			ARGS="--mono --force-portrait" ;;
		"HP IPAQ H3600" | "HP IPAQ H3700" | "HP IPAQ H3900" | *COLLIE | *POODLE)
		    	ARGS="--flip --force-portrait" ;;
		"HP IPAQ H3800" | "RAMSES")
			ARGS="--force-portrait" ;;
		"SHARP SHEPHERD" | "SHARP HUSKY" | "SHARP CORGI")
			case `uname -r` in
				2.4*)		
					ARGS="--force-landscape --flip" ;;
				*)
					ARGS="--force-landscape" ;;
			esac
			;;
		"SHARP SPITZ" | "SHARP AKITA" | "SHARP BORZOI" )
			ARGS="--force-landscape --flip"
			;;
		"HP IPAQ H5400" | "HP IPAQ H2200" )
			;;
	esac
	echo "0" > /proc/sys/kernel/printk

	# Clear tty2 	
	echo -e "\033c" > /dev/tty2
	
	chvt 2
	
	echo -e "\nLoading bootsplash..." > /dev/tty2
	echo "Use Alt-LeftArrow to see the boot messages." > /dev/tty2

	# Catch when the user has changed the symlinks manually (normally it doesn't matter which link we check
	# as themes always update both of them
	echo "$ARGS" | grep -q -- "--force-landscape" && source_link="splash-l.svg" || source_link="splash-p.svg" 

	touch /etc/bootsplash.name

	# bootsplash takes quite some time to load external images into an svg file.
	# We speed things up by dumping the content of fb0, and catting it back (which works instantly)
	if test -e /etc/bootsplash.raw.gz -a "`cat /etc/bootsplash.name`" = "`readlink /usr/share/gpe/$source_link`"			
	then
		echo -e "\nUsing /etc/bootsplash.raw.gz...." > /dev/tty2
		sleep 1

		# Turn off the cursor on tty2
		echo -e "\033[?25l\000" > /dev/tty2 

		zcat /etc/bootsplash.raw.gz > /dev/fb0
	else
		rm -f /etc/bootsplash.*
		# Store the symlinks name as an indication which theme is used at the time we dump the image
		if (/usr/bin/gpe-bootsplash $ARGS)
		then			
			readlink /usr/share/gpe/$source_link > /etc/bootsplash.name
			
			cat /dev/fb0 | gzip >/etc/bootsplash.raw.gz
		else
			echo -e "\nNOTE: gpe-bootsplash failed to start" >/dev/tty2
			sleep 3
			chvt 1
		fi
		
	fi	
	;;
'stop')
;;
*)
esac
