#!/bin/sh
# installs a rootfs tar archive from OpenADK onto a Compact Flash disk

check_exit() {
        if [ $? -ne 0 ];then
                echo "Installation failed."
                exit 1
        fi
}

rescue=0
while getopts "r" option
do
	case $option in
		r)
			rescue=1
			;;
		*)
			printf "Option not recognized\n"
			exit 1
			;;
	esac
done
shift $(($OPTIND - 1))

if [ -z $1 ];then
        printf "Please give your root tar archive as parameter\n"
        exit 1
fi

if [ $rescue -eq 1 ];then
	if [ -z $2 ];then
		printf "Please give your rescue kernel image as second parameter\n"
		exit 2
	fi
	if [ ! -f $2 ];then
		printf "$2 is not a file, Exiting.\n"
		exit 1
	fi
fi

printf "Creating partitions ...\n"
parted -s /dev/sda mklabel msdos
check_exit
sleep 2
maxsize=$(env LC_ALL=C parted /dev/sda -s unit cyl print |awk '/^Disk/ { print $3 }'|sed -e 's/cyl//')
rootsize=$(($maxsize-2))
start=0
rootp=1
cfgfsp=2
if [ $rescue -eq 1 ];then
	rootp=3
	cfgfsp=4
	start=2
	parted -s /dev/sda unit cyl mkpart primary ext2 0 1
	check_exit
	mke2fs -q /dev/sda1
	check_exit
	parted -s /dev/sda unit cyl mkpart primary ext2 1 2
	check_exit
	mke2fs -q /dev/sda2
	check_exit
fi
parted -s /dev/sda unit cyl mkpart primary ext2 $start $rootsize
check_exit
mke2fs -q /dev/sda1
check_exit
parted -s /dev/sda unit cyl mkpart primary fat32 $rootsize $maxsize
check_exit
parted -s /dev/sda set $rootp boot on
check_exit
sfdisk --change-id /dev/sda $cfgfsp 88 >/dev/null 2>&1
check_exit
# settle down
sleep 2
mount -t ext2 /dev/sda$rootp /mnt
check_exit
printf "Extracting install archive ...\n"
tar -C /mnt -xzpf $1
check_exit
chmod 1777 /mnt/tmp
chmod 4755 /mnt/bin/busybox
if [ $rescue -eq 1 ];then
	mkdir /mnt/boot/grub
fi

speed=$(awk -F \, '/console=ttyS0/ { print $2 }' /proc/cmdline|sed -e "s/ .*$//")

if [ $rescue -eq 1 ];then
	umount /mnt
	mount /dev/sda2 /mnt
	cp $2 /mnt/rescue
fi
printf "Installing bootloader ...\n"
mkdir -p /mnt/boot/grub
if [ $rescue -eq 1 ];then
mount /dev/sda1 /mnt/boot/grub
cat << EOF > /mnt/boot/grub/grub.cfg
set timeout=1
serial --unit=0 --speed=$speed
terminal_output serial 
terminal_input serial 

if [ -s \$prefix/grubenv ]; then
  load_env
fi

set default="\${saved_entry}"

if [ \${prev_saved_entry} ]; then
  set saved_entry=\${prev_saved_entry}
  save_env saved_entry
  set prev_saved_entry=
  save_env prev_saved_entry
  set boot_once=true
fi

function savedefault {
  if [ -z \${boot_once} ]; then
    saved_entry=\${chosen}
    save_env saved_entry
  fi
}

function recordfail {
  set recordfail=1
  if [ -n \${have_grubenv} ]; then if [ -z \${boot_once} ]; then save_env recordfail; fi; fi
}

menuentry "GNU/Linux (OpenADK)" {
	recordfail
        insmod ext2
        set root=(hd0,3)
        linux /boot/kernel root=/dev/sda$rootp ro init=/init panic=10
}

menuentry "GNU/Linux (OpenADK) Rescue Mode" {
	recordfail
        insmod ext2
        set root=(hd0,2)
        linux /rescue ro init=/init panic=10
}
EOF
else
cat << EOF > /mnt/boot/grub/grub.cfg
set default=0
set timeout=1
serial --unit=0 --speed=$speed
terminal_output serial 
terminal_input serial 

menuentry "GNU/Linux (OpenADK)" {
        insmod ext2
        set root=(hd0,1)
        linux /boot/kernel root=/dev/sda$rootp ro init=/init panic=10
}
EOF
fi
grub-install --root-directory=/mnt /dev/sda 
check_exit
if [ $rescue -eq 1 ];then
	grub-set-default --root-directory=/mnt 0
	umount /mnt/boot/grub
fi
umount /mnt
printf "Successfully installed. You can reboot now.\n"
exit 0
