#!/bin/bash
#
################################################################################
# CHDK installer script for OSX
################################################################################


################################################################################
# identify OS version for correct diskutil syntax
################################################################################

OS_VERSION=`sw_vers | grep Build | awk '{print $2}' | sed s/[A-Z]/\ /g | awk '{print $1}'`


################################################################################
# identify disk for CHDK installation
################################################################################

while [[ $REPLY1 != "yes" ]]
do
	clear
	printf "\nThis script will step you through preparing a memory card for use with CHDK."
	printf "\n\nI have tested the script on Leopard and Snow Leopard (Mac OS X 10.5 and 10.6)"
	printf "\nand it worked for me.  However, use this at your own risk!"
	printf "\n\nThe tools in utilized by this script can format (i.e. ERASE) any,"
	printf "\nyes ANY disk on your computer that you specify."
	printf "\n\nI've tried to make it easy to do it right, but please pay attention!"
	printf "\n\n\n\n\nAre you willing to take responsibility for your own actions"
	printf "\nand proceed with caution?\n\n"
	echo "type 'yes' or 'no'"

	read REPLY1; [ "$REPLY1" == yes ] || exit
done

clear
diskutil list

printf "\nThis is a list of all disks in your system."
printf "\nIdentify the one you want to install CHDK on."
printf "\n\nenter the disk number: disk"

read NUMBER; DEVICE=disk${NUMBER}

if [[ $OS_VERSION > 0 ]]
then
	READONLY=`diskutil info $DEVICE | grep "Read-Only Media" | awk '{print $3}'`
elif [[ $OS_VERSION == 9 ]]
then
	READONLY=`diskutil info $DEVICE | grep "Read Only" | awk '{print $3}'`
else
	echo "Your OS is older than Leopard and I haven't tested the script on it."
	echo "Hack away at your own peril and have a nice day!"
	exit
fi

if ! [ -e /dev/$DEVICE ]
then
	echo "/dev/$DEVICE does not exist!"
	exit
elif [[ $DEVICE == `df | grep -e "/$" | sed s/[s/]/\ /g | awk '{print $2" "$3 }' | sed s/\ /s/g` ]]
then
	echo "That's your boot volume!  Don't erase that!"
	exit
elif [[ $READONLY == "Yes" ]]
then
	echo "File system is read-only.  Unlock your card!"
	exit
fi	

printf "Enter a name for the CHDK partition (or press return for CHDK) : "
read CHDK_VOL; [ "$CHDK_VOL" != "" ] || CHDK_VOL="CHDK"
	
printf "Enter a name for the other partition (or press return for CAMCARD) : "
read CAMCARD_VOL; [ "$CAMCARD_VOL" != "" ] || CAMCARD_VOL="CAMCARD"

WARN="We are about to *COMPLETELY* erase ${DEVICE}.  Is this correct?"
echo
echo $WARN | sed s/./!/g
echo $WARN
echo $WARN | sed s/./!/g
echo "type 'yes' or 'no'"
read REPLY2; [ "$REPLY2" == yes ] || exit


################################################################################
# format and partition the disk
################################################################################

if [[ $OS_VERSION > 0 ]]
then
	#diskutil partitionDisk $DEVICE 2 MBR "MS-DOS FAT16" $CHDK_VOL 16M "MS-DOS FAT32" $CAMCARD_VOL R
	diskutil partitionDisk $DEVICE 1 MBR "MS-DOS FAT16" $CHDK_VOL R
elif [[ $OS_VERSION == 9 ]]
then
	diskutil partitionDisk $DEVICE 2 MBRFormat "MS-DOS FAT16" $CHDK_VOL 16M "MS-DOS FAT32" $CAMCARD_VOL 16M
else
	echo "Your OS is older than Leopard and I haven't tested the script on it."
	echo "Hack away at your own peril and have a nice day!"
	exit
fi

printf "\n\nI sure hope this is what you wanted!"
printf "\n\nPress RETURN to continue" ; read


################################################################################
# make CHDK partition bootable
################################################################################

#dd if=/dev/${DEVICE}s1 of=BootSector.bin bs=512 count=1
#printf "BOOTDISK" | dd bs=1 count=8 seek=0x40 conv=notrunc of=BootSector.bin
#dd if=BootSector.bin of=/dev/${DEVICE}s1 bs=512 count=1
#rm BootSector.bin

clear
printf "Type 'yes' to make the volume named $CHDK_VOL on $DEVICE bootable"
printf "\nor just press RETURN to skip and continue.\n"; read REPLY3

if [ $REPLY3 == "yes" ]
then
	diskutil unmountDisk $DEVICE
	clear
	
	echo "---- BEFORE ----"
	hexdump -C -n 80 /dev/${DEVICE}s1
	echo "---- BEFORE ----"

	echo
	printf "BOOTDISK" | dd bs=1 count=8 seek=0x40 conv=notrunc of=/dev/${DEVICE}s1
	echo

	echo "---- AFTER ----"
	hexdump -C -n 80 /dev/${DEVICE}s1
	echo "---- AFTER ----"

	printf "\n\nPress RETURN to continue" ; read
	
	diskutil mountDisk $DEVICE
fi


################################################################################
# maybe put veer.req and/or vers.req files onto card
################################################################################

while [[ $REPLY4 != "4" ]]
do
	clear
	echo "Would you like a 'ver.req' and/or 'vers.req' file placed on your card"
	echo "to help you identify your camera firmware version?"
	echo
	echo "1. only 'ver.req' file"
	echo "2. only 'vers.req' file"
	echo "3. both files"
	echo "4. do nothing"
	printf "\nEnter a number: "

	read REPLY4; [ "$REPLY4" != "" ] || exit

	case "$REPLY4" in
		1)
			touch /Volumes/${CHDK_VOL}/ver.req;
			REPLY4=4
			;;
		2)
			touch /Volumes/${CHDK_VOL}/vers.req;
			REPLY4=4
			;;
		3)
			touch /Volumes/${CHDK_VOL}/ver.req;
			touch /Volumes/${CHDK_VOL}/vers.req;
			REPLY4=4
			;;
	esac
done


################################################################################
# the end!
################################################################################

echo
ls -1 /Volumes/$CHDK_VOL
echo
echo "If you need to, don't forget to lock your card after copying CHDK files to it"
printf "\n\nHappy shooting!\n\n"
