#!/bin/bash
#
DIALOG="zenity"	
title="FlashLinux 0.3.4"
BOOT=""

COPY() {
	echo "0"
	tar clf - ./* --exclude-from=/mnt/cdrom/usr/share/fl/exclude_full | (cd /mnt/flash && tar xf -)
	echo "100"
}
COPYBOOT() {
	echo "0"
	tar clf - ./*  | (cd /mnt/flash/boot && tar xf -)
	echo "100"
}

WAIT() {
	${1} | ${DIALOG} --progress --pulsate --auto-close \
	--title "${title}" --text "${2}"

	case $? in
	0)			;;
	1)	ABORT 		;;
	-1)	ERROR "${2}"	;;
	esac

}

WAITP() {
	(${1} && sleep 2) | ${DIALOG} --progress --auto-close --title "${title}" --text "${2}"

	case $? in
	0)			;;
	1)	ABORT 		;;
	-1)	ERROR "${2}"	;;
	esac

}

ABORT() {
	${DIALOG} --error --title "${title}" --text "Installation Aborted"
	exit
}

ERROR() {
	${DIALOG} --error --title "${title}" --text "Installation Error: ${1}"
	exit
}

check_block() {

	[ ! -b $1 ] && ERROR "${1} is not a block device"
	size=`fdisk -s ${1}`
	size=`expr ${size} / 1024`
	echo "Verified block device [${1}] size is ${size}Mb"
	[ ${size} -gt 1024 ] && ERROR "${1} has a size of ${size} , quitting in case it's a hard disk"
}

fix_discs() {
	
	[ -e /dev/discs/disc0 ] && return

	index=0
	ide=`find /dev/ide -name "disc"|cut -d"/" -f1-7`
	scsi=`find /dev/scsi -name "disc"|cut -d"/" -f1-7`
	drives="${ide} ${scsi}"

	rm -f /dev/discs/*
	for i in ${drives}
	do
		ln -s ${i} /dev/discs/disc${index}
		index=`expr ${index} + 1`
	done
}

MKFS() {
	sleep 1
	echo "0"
	sleep 1
	mkfs -text2 -m 0 ${BOOT} >/tmp/gb 2>&1
	echo "100"
	sleep 2
}

MOUNTJ() {
	sleep 1
	echo "0"
	sleep 1 
	mount -t jffs2 /dev/mtdblock/0 /mnt/flash
	echo "100"
	sleep 2
} 

ERASE() {
	sleep 1
	echo "0"
	/usr/sbin/flash_eraseall -j /dev/mtd/0 
	sync
	sync
	echo "100"
	sleep 2
} 

GRUB() {
	sleep 1
	echo "0"
	sleep 1
	grub-install --recheck --root-directory=/mnt/flash ${DEVICE}
	echo "100"
	sleep 2
}

INSTALL() {
	##
	##	Try to install the JFFS block driver
	##
	DEVICE=${1}
	##
	modprobe blkmtd device=${3}
	##
	[ $? != 0 ] && ERROR "failed to find BLKMTD device"
	##
	[ ! -c /dev/mtd/0 ] && ERROR "BLKMTD, character device is missing"
	##
	[ ! -b /dev/mtdblock/0 ] && ERROR "BLKMTD, block device is missing"
	##
	ROOT=${3}
	WAITP ERASE "Formatting device ${3} with JFFS2 filesystem, Please wait ..."
 	##
	BOOT=${2}
	WAIT MKFS "Formatting ${2} with EXT2 filesystem, please wait ..."
	##
	mkdir -p /mnt/flash
	##
	WAIT MOUNTJ "Mounting the USB Flash Key on ${3} - Please wait ..."
	##
	mkdir -p /mnt/flash/boot
	##
	mount ${2} /mnt/flash/boot
	##
	cd /mnt/livecd && WAIT COPY "Copying files from the CD to the key - Please wait ..."
	##
	echo -n "FL 0.3.4" > /mnt/flash/boot/.encryptec
	rm -rf /mnt/flash/boot/*
	##
	cd /mnt/cdrom/boot && WAIT COPYBOOT "Copying BOOT loader the CD to the key - Please wait ..."
	##
	WAIT GRUB "Running Grub-Install"
	##
	cat /mnt/flash/boot/grub/grub.conf | sed "/default 1/s//default 2/" > /tmp/grub.conf
	mv /tmp/grub.conf /mnt/flash/boot/grub/
	##
	umount /mnt/flash/boot
	umount /mnt/flash

}

PICKDEVICE() {

	(for i in `find /sys/bus/scsi/devices -name block -follow -maxdepth 3 -ls|cut -d"/" -f2-10`
	do
		base=`echo ${i}|cut -d"/" -f1-5`
		name=`cat /${base}/model`
		vend=`cat /${base}/vendor`
		dev=`readlink /${i}|cut -d"/" -f11`
		echo ${dev}
		if [ -z "${vend}" ] ; then 
			echo "${name}"
		else
			echo "${name} [${vend}]"
		fi
	done) | \
	${DIALOG} --list --title "${title}" --column "Device" --column "Type" \
	--text "\
`cat <<EOF
Please select the device onto which you would like to install Flash Linux. 

Be aware that this process will erase the current contents of the device you select.
EOF`"

}

PICKBOOT() {

	(for i in `find /sys/block/${1} -name start -ls |cut -d"/" -f5`
	do
		siz=`cat /sys/block/${1}/${i}/size`
		echo ${i}
		echo "`expr ${siz} / 2048`Mb"
	done) | \
	${DIALOG} --list --title "${title}" --column "Partition" --column "Size" \
	--text "\
`cat <<EOF
Your choices so far;
  Installing FlashLinux 0.3.4
  Chosen device is (${1})

Please select a BOOT partition
EOF`"

}

PICKROOT() {

	(for i in `find /sys/block/${1} -name start -ls |cut -d"/" -f5`
	do
		siz=`cat /sys/block/${1}/${i}/size`
		echo ${i}
		echo "`expr ${siz} / 2048`Mb"
	done) | \
	${DIALOG} --list --title "${title}" --column "Partition" --column "Size" \
	--text "\
`cat <<EOF
Your choices so far;
  Installing FlashLinux 0.3.4
  Chosen device is ........... /dev/${1}
  Chosen boot partition is ... /dev/${2}

Please select a ROOT partition
EOF`"

}

${DIALOG} --question --title "${title}" --text "\
`cat <<EOF
Welcome to the FlashLinux installer.

This application will install FlashLinux on the device you chose as part of this process. Please be aware that the contents of your chosen device will be permenantly erased.

If you are unsure, please click 'Cancel' now.
EOF`"

case $? in
0)			;;
1)	ABORT 		;;
-1)	ERROR "unknown"	;;
esac
##
##	Fix up any broken-ness in /proc
##
fix_discs
##
##	Let the user pick a device
##
device=`PICKDEVICE`

	case $? in
	0)			;;
	1)	ABORT 		;;
	-1)	ERROR "unknown"	;;
	esac
##
##
##
boot=`PICKBOOT ${device}`
##
##
##
case $? in
0)			;;
1)	ABORT 		;;
-1)	ERROR "unknown"	;;
esac
##
##
##
root=`PICKROOT ${device} ${boot}`
##
##
##
case $? in
0)			;;
1)	ABORT 		;;
-1)	ERROR "unknown"	;;
esac
##
##	Make sure things "look" ok
##	Code # 18 avoidance tactics
##
check_block /dev/${device}
check_block /dev/${boot}
check_block /dev/${root}
##
##	Ask for final committment
##
${DIALOG} --question --title "${title}" --text "\
`cat <<EOF
You have chosen to install FlashLinux to;

Device: /dev/${device}
Boot partition: /dev/${boot}
Root partition: /dev/${root}

This operation will erase the contents of /dev/${device} and there will be no way to recover any overwritten data.

Are you sure ?

EOF`"
##
##
##
case $? in
0)			;;
1)	ABORT 		;;
-1)	ERROR "unknown"	;;
esac
##
##	Go fer it!
##
INSTALL /dev/${device} /dev/${boot} /dev/${root}

${DIALOG} --info --title "${title}" --text "\
`cat <<EOF
Installation Complete.

Have fun!
EOF`"

