#
# By Chih-Wei Huang <cwhuang@linux.org.tw>
# Last updated 2012/07/07
#
# License: GNU Public License
# We explicitely grant the right to use the scripts
# with Android-x86 project.
#

tempfile=/tmp/temp-$$
menufile=/tmp/menu-$$

CPIO=/system/xbin/cpio

rebooting()
{
	dialog --title " Rebooting... " --nocancel --pause "" 8 41 1
	sync
	umount -a
	reboot -f
}

choose()
{
	dialog --clear --title " $1 " \
		--menu "$2" 20 71 13 --file $menufile 2> $tempfile

	retval=$?
	choice=`cat $tempfile`
}

partition_drive()
{
	echo -n > $menufile
	for i in /sys/block/[sh]d[a-z]; do
		echo -n `basename $i` >> $menufile
		if [ -f $i/removable -a `cat $i/removable` -eq 0 ]; then
			echo -n ' "Harddisk ' >> $menufile
		else
			echo -n ' "Removable' >> $menufile
		fi
		if [ -f $i/size ]; then
			echo -n " (" `cat $i/size` "blocks)" >> $menufile
		fi
		echo '"' >> $menufile
	done
	count=`wc -l $menufile | awk '{ print $1 }'`
	if [ $count -eq 0 ]; then
		dialog --title " Error " --msgbox \
			"\nOK. There is no hard drive to edit partitions." 8 49
		return 255
	fi
	if [ $count -eq 1 ]; then
		choice=`awk '{ print $1 }' $menufile`
		retval=0
	else
		choose "Choose Drive" "Please select a drive to edit partitions:"
	fi
	if [ $retval -eq 0 ]; then
		cfdisk /dev/$choice
		if [ $? -eq 0 ]; then
			retval=1
		else
			retval=255
		fi
	fi
	return $retval
}

select_dev()
{
	fdisk -l | grep ^/dev | cut -b6-12,55- | awk '{
		if (!match($2, "Extended")) {
			printf("\"%-28s", $0)
			system("echo -n `cat /sys/block/*/"$1"/../device/model`")
			printf("\" \"\"\n")
		}
	} END {
		printf("\"Create/Modify partitions\" \"\"\n\"Detect devices\" \"\"")
	}' > $menufile
	choose "Choose Partition" "Please select a partition to install Android-x86:"
	return $retval
}

progress_bar()
{
	dialog --clear --title " $1 " --gauge "\n $2" 8 70
}

format_fs()
{
	local cmd
	echo -e '"Do not format" ""\next3 ""\next2 ""\nntfs ""\nfat32 ""' > $menufile
	choose "Choose filesystem" "Please select a filesystem to format $1:"
	case "$choice" in
		ext3)
			cmd="mke2fs -jL"
			;;
		ext2)
			cmd="mke2fs -L"
			;;
		ntfs)
			cmd="mkntfs -fL"
			;;
		fat32)
			cmd="newfs_msdos -L"
			;;
		*)
			;;
	esac
	if [ -n "$cmd" ]; then
		dialog --title " Confirm " --no-label Skip --yesno \
			"\n You chose to format $1 to $choice.\n All data in that partition will LOSE.\n\n Are you sure to format the partition $1?" 10 51
		[ $? -ne 0 ] && return 1
		$cmd Android-x86 /dev/$1 | awk '{
			# FIXME: very imprecise progress
			if (match($0, "done"))
				printf("%d\n", i+=33)
		}' | progress_bar "Formatting" "Formatting partition $1..."
	fi
}

create_entry()
{
	title=$1
	shift
	echo -e "title $title\n\tkernel /$asrc/kernel$vga $@ SRC=/$asrc\n\tinitrd /$asrc/initrd.img\n" >> $menulst
}

create_menulst()
{
	menulst=/hd/grub/menu.lst
	[ -n "$VESA" ] && vga=" vga=788 modeset=0"
	echo -e "${GRUB_OPTIONS:-default=0\ntimeout=6\nsplashimage=/grub/android-x86.xpm.gz\n}root (hd0,$1)\n" > $menulst
	cmdline=`cat /proc/cmdline | sed "s|\(initrd.*img\s*\)||; s|quiet\s*||; s|\(vga=\w\+\?\s*\)||; s|\(DPI=\w\+\?\s*\)||; s|\(INSTALL=\w\+\?\s*\)||; s|\(SRC=\S\+\?\s*\)||; s|\(DEBUG=\w\+\?\s*\)||; s|\(BOOT_IMAGE.*\)||"`

	create_entry "Android-x86 $VER" quiet $cmdline
	create_entry "Android-x86 $VER (Debug mode)" $cmdline DEBUG=2
	create_entry "Android-x86 $VER (Debug nomodeset)" nomodeset $cmdline DEBUG=2
	create_entry "Android-x86 $VER (Debug video=LVDS-1:d)" video=LVDS-1:d $cmdline DEBUG=2
}

create_winitem()
{
	win=`fdisk -l /dev/$(echo $1 | cut -b-3) | grep ^/dev | cut -b6-12,55- | awk '{
		if (match($2, "NTFS"))
			print $1
	}' | head -1`
	if [ -n "$win" ]; then
		dialog --title " Confirm " --yesno \
			"\nThe installer found a Windows partition in /dev/$win.\n\nDo you want to create a boot item for Windows?" 9 59
		[ $? -ne 0 ] && return 1
		wp=$((`echo $win | cut -b4-`-1))
		echo -e "title Windows\n\trootnoverify (hd$d,$wp)\n\tchainloader +1\n" >> $menulst
	fi
}

create_img()
{
	bname=`basename $2`
	if [ -e $2 ]; then
		dialog --title " Confirm " --defaultno --yesno \
			"\n $bname exists. Overwrite it?" 7 38
		[ $? -ne 0 ] && return 255
		rm -f $2
	fi
	dialog --title " Question " --nook --nocancel --inputbox \
		"\nPlease input the size of the $bname in MB (max 2047):" 8 63 $1 2> $tempfile
	size=`cat $tempfile`
	[ 0$size -le 0 -o 0$size -gt 2047 ] && size=2047
	( dd bs=1M count=$size if=/dev/zero | pv -ns ${size}m | dd of=$2 ) 2>&1 \
		| progress_bar "Creating $bname" "Expect to write $size MB..."
}

create_data_img()
{
	dialog --title " Confirm " --yesno \
		"\nThe installer is going to create a disk image to save the user data. At least 512MB free disk space is recommended.\n\nAre you sure to create the image?" 11 62

	if [ $? -eq 0 ]; then
		if create_img 512 data.img; then
			losetup /dev/loop6 data.img
			mke2fs -jL /data /dev/loop6 > /dev/tty6
		fi
		[ $? -ne 0 ] && dialog --msgbox "\n Failed to create data.img." 7 33
	else
		dialog --title " Warning " --msgbox \
			"\nOK. So data will be save to a RAMDISK(tmpfs), and lose after power off." 8 49
	fi
}

install_to()
{
	cd /
	mountpoint -q /hd && umount /hd
	while [ 1 ]; do
		format_fs $1
		try_mount rw /dev/$1 /hd && break
		dialog --clear --title " Error " --defaultno --yesno \
			"\n Cannot mount /dev/$1\n Do you want to format it?" 8 37
		[ $? -ne 0 ] && return 255
	done

	fs=`cat /proc/mounts | grep /dev/$1 | awk '{ print $3 }'`

	asrc=android-$VER
	dialog --title " Confirm " --no-label Skip --defaultno --yesno \
		"\n Do you want to install boot loader GRUB?" 7 47
	if [ $? -eq 0 ]; then
		cp -af /grub /hd
		d=0
		while [ 1 ]; do
			h=`echo $d | awk '{ printf("%c", $1+97) }'`
			[ -d /sys/block/[sh]d$h/$1 ] && break
			d=$(($d+1))
		done
		p=$((`echo $1 | cut -b4-`-1))
		create_menulst $p
		create_winitem $1 $d
		rm -f /hd/boot/grub/stage1
		echo "setup (hd$d) (hd$d,$p)" | grub > /dev/tty5
		[ $? -ne 0 ] && return 255
	fi

	[ "$fs" != "vfat" -a "$fs" != "fuseblk" ] && dialog --title " Question " --yesno \
		"\nDo you want to install /system directory as read-write?\n\nMaking /system be read-write is easier for debugging, but it needs more disk space and longer installation time." 10 61
	instal_rw=$?

	files="mnt/$SRC/kernel mnt/$SRC/initrd.img mnt/$SRC/ramdisk.img"
	[ $instal_rw -eq 0 ] && files="$files android/system" || files="$files mnt/$SRC/system.*"
	size=0
	for s in `du -sk $files | awk '{print $1}'`; do
		size=$(($size+$s))
	done
	mkdir hd/$asrc
	cd hd/$asrc
	rm -rf system*
	( ( cd /; find $files | $CPIO -H newc -o ) | pv -ns ${size}k | ( $CPIO -iud > /dev/null; echo $? > /tmp/result )) 2>&1 \
		| progress_bar "Installing Android-x86" "Expect to write $size KB..."
	result=$((`cat /tmp/result`*255))

	if [ $result -eq 0 ]; then
		chmod 644 *
		chown 0.0 *
		mv mnt/$SRC/* . && rm -rf mnt
		[ -d android ] && mv android/* . && rmdir android

		case "$fs" in
			vfat|fuseblk)
				create_data_img
				;;
			*)
				mkdir data
				;;
		esac
	fi

	sync

	return $result
}

install_hd()
{
	select_dev || rebooting
	retval=1
	case "$choice" in
		[sh]d*)
			install_to $choice
			retval=$?
			;;
		Create*)
			partition_drive
			retval=$?
			;;
		Detect*)
			dialog --title " Detecting... " --nocancel --pause "" 8 41 1
			;;
	esac
	return $retval
}

do_install()
{
	until install_hd; do
		if [ $retval -eq 255 ]; then
			dialog --title ' Error! ' --yes-label Retry --no-label Reboot \
				--yesno '\nInstallation failed! Please check if you have enough free disk space to install Android-x86.' 8 51
			[ $? -eq 1 ] && rebooting
		fi
	done

	[ -n "$VESA" ] || runit="Run Android-x86"
	dialog --clear --title ' Congratulations! ' \
		--menu "\n Android-x86 is installed successfully.\n " 11 51 13 \
		"$runit" "" "Reboot" "" 2> $tempfile
	case "`cat $tempfile`" in
		Run*)
			cd /android
			umount system
			if mountpoint -q /sfs; then
				umount /sfs
				mount -o loop /hd/$asrc/system.sfs /sfs
				mount -o loop /sfs/system.img system
			else
				mount -o loop /hd/$asrc/system.img system
			fi
			if [ -d /hd/$asrc/data ]; then
				mount --bind /hd/$asrc/data data
			elif [ -e /hd/$asrc/data.img ]; then
				mount -o loop /hd/$asrc/data.img data
			fi
			;;
		*)
			rebooting
			;;
	esac
}
