#!/bin/sh
# Copyright HacknDev (Fahrzin Hemmati fahhem@fahhem.com)
# This script automatically installs Palm OS to your microdrive
# 
# Programs needed before install:
# curl | wget
# unzip
# cabextract
# unshield
# python2.4
# subversion
# xargs
# dd
# md5sum


# Now checking for all necessary programs
# curl or wget. curl preferred
if [ -n "$(which curl)" ]; then
	DL_PROG="$(which curl)"
	DL_OPTS="-C - -O"
elif [ -n "$(which wget)" ]; then
	DL_PROG="$(which wget)"
	DL_OPTS="-c"
else
	echo "Please install curl or wget."
	exit
fi

check_exist() {
if [ -z "$(which $1)" ]; then
	echo "Please install $1."
	exit
fi
}

check_exist unzip
check_exist cabextract
check_exist unshield
check_exist python2.4
check_exist svn
check_exist xargs
check_exist md5sum
check_exist dd


# Now that everything is here, use it.

if [ -z "$(pwd | grep \"/new_ld_drive\$\")" ]; then
	mkdir -p new_ld_drive && cd new_ld_drive
fi

if [ -z "$(du LifeDrive_Update_2_0_EFIGS_win.zip | grep 21853)" ]; then
	$DL_PROG $DL_OPTS http://palmone.r3h.net/downloads.palmone.com/LifeDrive_Update_2_0_EFIGS_win.zip
fi

if [ ! -e "LifeDrive 2.0 Updater.exe" ]; then
	unzip LifeDrive_Update_2_0_EFIGS_win.zip
fi

if [ ! -d "Disk1" ]; then
	cabextract 'LifeDrive 2.0 Updater.exe'
fi

mkdir -p data && cd data

if [ ! -d "BrahmaUpdate" ]; then
	unshield x ../Disk1/data1.cab
fi

cd Brahma* 2> /dev/null

svn cat https://svn.sourceforge.net/svnroot/hackndev/linux4palm/tools/unpdb.py > unpdb.py

if [ ! -e "brahma-palmos.zip" ]; then
	ls brahma-palmos.zip.?.pdb | sort | xargs -ti python unpdb.py {} - | dd skip=1 bs=32 > brahma-palmos.zip
fi

if [ 	-z "$(du -b brahma-palmos.zip | grep 20479778)" \
				-o  \
	-z "$(md5sum brahma-palmos.zip | grep 242847c981475636f7b74c7ba9a40379)" \
	\
	]; then
	echo "Something went wrong."
	echo "This is likely linked to python or the subversion repository."
	echo "PM fahhem about this."
	exit
fi

svn cat https://svn.sourceforge.net/svnroot/hackndev/linux4palm/tools/makecafe.py > makecafe.py

if [ ! -e "rom-partition" ]; then
	python makecafe.py -c brahma-palmos.zip > rom-partition
fi

if [	-z "$(md5sum rom-partition | grep 639952c7a50e8d12d1d9351f3cbe9aa6)" ]; then
	echo "Something else went wrong."
	echo "I have no idea what. Might be python, could be a bad makecafe.py."
	echo "PM fahhem about this."
	exit
fi

if [ ! -e "table.sct" ]; then
	echo 'AAAAAAAAAAAAAAAAAAAAAQEABlgPCD8AAACACwIAAFgQCAAoHAu/CwIAgLAAAAAoHQsLz13xP7wCAIBLdwAAAAAAAAAAAAAAAAAAAAAAVao=' | python -c 'import base64,sys;sys.stdout.write("\0"*432+base64.b64decode(sys.stdin.read()))' > table.sct
fi

echo "All the files are set up now."

#store existing devs
if [ ! -e "oldDevs" ]; then
	ls /dev/*d* > oldDevs
fi

echo "If you have a CF Card reader, go ahead and plug it in now."
echo "If its a CF-IDE converter, turn off your computer, plug it in and re-run this command."

while [ "$YESNO" != "y" ]; do
echo -n "Are you ready? [y/n]"
read -n 1 YESNO
if [ "$YESNO" == "n" ]; then
	echo "Exiting..."
	echo "When you are ready, re-run this script."
	exit
fi
done

echo

ls /dev/*d* > newDevs

# Auto-find new device

DEVNAME=$(diff -Bu oldDevs newDevs | grep "^+/dev" | grep -o "/[/a-z]*")
if [ -z "$DEVNAME"  -o -z "$(echo $DEVNAME | wc -l | cut -b 1 | grep 1)" ]; then
	echo "Do you know the /dev/ entry of your drive?"
	echo "If so, please enter it now. Enter 'n' if you do not know."
	read DEVNAME
fi

if [ "$DEVNAME" == "n" ]; then
	echo "Please find out the /dev/ entry and re-run this script."
	exit
fi

if [ -z "$(echo $DEVNAME | grep /dev/hd)" ]; then
# CF-IDE converter
	hdparm -d0 $DEVNAME
fi

echo "Now writing the data to the microdrive."
echo "DO NOT UNPLUG THE DRIVE."

dd if=table.sct of=$DEVNAME conv=notrunc
echo "Partition Table written. Now writing main data."

if [ -z "$(which sdd)" ]; then
	DD=dd
	END_OPTS=""
	echo "Since you don't have sdd installed, we will use dd."
	echo "sdd allows a progress bar to be shown."
	YESNO=""
	while [ "$YESNO" != "y" ]; do
		echo -n "Would you like to continue with dd or install sdd and re-run this? [y/n]"
		read -n 1 YESNO
		if [ "$YESNO" == "n" ]; then
			exit
		elif [ "$YESNO" == "y" ]; then
			break
		else
			echo "Please enter y or n."
			YESNO=""
		fi
	done
else
	DD=sdd
	END_OPTS="| perl -e 'print $i++.\" MB\n\" while(read(STDIN,$c,3));'"
fi

$DD if=rom-partition of=$DEVNAME seek=134079 bs=512 conv=notrunc $END_OPTS
