#!/bin/sh

if [ -x /usr/bin/update-desktop-database ]; then
  update-desktop-database /usr/share/applications
fi

if [ -x /usr/bin/gtk-update-icon-cache ]; then
  gtk-update-icon-cache -f /usr/share/icons/hicolor
fi

if [ -x /usr/bin/maemo-select-menu-location ]; then
  maemo-select-menu-location x48.desktop
fi

test -x /usr/bin/maemo-confirm-text || exit 0

cat <<EOF > /tmp/alert.txt
This is an emulator of the HP 48 SX and GX calculators.

Access http://www.hpcalc.org for Manuals, Tips, and 
Applications for this calculator.

This version displays in landscape mode, and includes
HP48GX ROM revision R from hpcalc.org.
EOF
maemo-confirm-text "x48" /tmp/alert.txt

rm -f /tmp/alert.txt

#Check if rom, ram and hp48 files exists
cat <<EOF > /tmp/alert.txt
This is your first time with x48 emulator, we need load x48
application for build ROM/RAM files.
On emulator press "NO" (F key) on "Try To Recover Memory?" alert, 
wait message "Memory Clear!", press "ON" key and close x48 emulator.
EOF

cd /home/user/.hp48/
if test -a /home/user/.hp48/rom;  then
	if test -a /home/user/.hp48/ram;  then
		if test -a /home/user/.hp48/hp48;  then
			rm ~/.hp48/rom.dump
		else
			maemo-confirm-text "x48" /tmp/alert.txt
			x48
			rm ~/.hp48/rom.dump
		fi
	else
		maemo-confirm-text "x48" /tmp/alert.txt
		x48
		rm ~/.hp48/rom.dump
	fi
else
	maemo-confirm-text "x48" /tmp/alert.txt
	x48
	rm ~/.hp48/rom.dump
fi
	
rm -f /tmp/alert.txt

#Setup correct owner!
/bin/chown -R 29999:29999 /home/user/.hp48

exit 0
