#!/bin/sh

#set -x

rootsize=1024
usbrootsize=512
cfgsize=16

create_bootcfg() {
	cat << FOO > /mnt/boot/boot.cfg
default 0
timeout 2
showmenu 1
title OpenADK
	kernel (wd0,0)/boot/kernel
	args root=/dev/sda1

title USB
	kernel (usb0,0)/boot/kernel-usb
	args root=/dev/sdc1
FOO

}

create_fstab() {
	cat << FOO > /mnt/etc/fstab
/dev/sda2 /home ext4 defaults 0 0
/dev/sda3 none swap sw 0 0
FOO

}

create_usb_fstab() {
	cat << FOO > /mnt/etc/fstab
/dev/sdc2 /home ext4 defaults 0 0
FOO

}

create_home() { 
	mkdir -p /mnt/home
	mkdir -p /tmp/home/lemote
	chown 1000:1000 /tmp/home/lemote
	echo "lemote:x:1000:1000:Linux User,,,:/home/lemote:/bin/mksh" >> /mnt/etc/passwd
	echo "lemote:.m3Jtyyc5L4W6:15426:0:99999:7:::" >> /mnt/etc/shadow
	echo "setxkbmap -option terminate:ctrl_alt_bksp" >> /tmp/home/lemote/.xsession
	echo fluxbox >> /tmp/home/lemote/.xsession
}

usb_install() {
	size=$(parted -s /dev/sdc unit mb print|awk '/Disk/ {print $3}'|sed -e "s#MB##")
	homesize=$(($size-$usbrootsize-$cfgsize))
	mkdir /tmp/home
	parted -s /dev/sdc mklabel msdos
	parted -s /dev/sdc mkpart primary ext2 0 ${usbrootsize}M 2>&1 >/dev/null
	parted -s /dev/sdc mkpart primary ext2 ${usbrootsize}M ${homesize}M
	parted -s /dev/sdc -- mkpart primary ext2 ${homesize}M -0
	sfdisk --change-id /dev/sdc 3 88 2>&1 >/dev/null
	echo "Creating filesystem on /home"
	mke2fs -q -j /dev/sdc2
	mount -t ext4 /dev/sdc2 /tmp/home
	echo "Copying system to disk" 
	dd if=/dev/sdc of=/tmp/home/lemote.tar.gz bs=16384 skip=1024 count=8000 2>/dev/null
	echo "Creating filesystem on /"
	mke2fs -q -j /dev/sdc1
	mount -t ext4 /dev/sdc1 /mnt
	echo "Installing system to USB disk ..."
	tar xzf /tmp/home/lemote.tar.gz -C /mnt
	test -x /mnt/usr/bin/sudo && chmod u+s /mnt/usr/bin/sudo
	test -x /mnt/usr/bin/Xorg && chmod u+s /mnt/usr/bin/Xorg
	create_home
	create_bootcfg
	create_usb_fstab
	umount /mnt
	umount /tmp/home
	
}

disk_install() {
	size=$(parted -s /dev/sda unit mb print|awk '/Disk/ {print $3}'|sed -e "s#MB##")
	swap=1024
	homesize=$(($size-$rootsize-$cfgsize-$swap))
	swapsize=$(($homesize+$swap))
	mkdir /tmp/home
	parted -s /dev/sda mklabel msdos
	parted -s /dev/sda mkpart primary ext2 0 ${rootsize}M 2>&1 >/dev/null
	parted -s /dev/sda mkpart primary ext2 ${rootsize}M ${homesize}M
	parted -s /dev/sda mkpart primary ext2 ${homesize}M ${swapsize}M
	parted -s /dev/sda -- mkpart primary ext2 ${swapsize}M -0
	sfdisk --change-id /dev/sda 3 82 2>&1 >/dev/null
	sfdisk --change-id /dev/sda 4 88 2>&1 >/dev/null
	sleep 2
	echo "Creating filesystem on /home"
	mke2fs -q -j /dev/sda2
	mkswap /dev/sda3
	mount -t ext4 /dev/sda2 /tmp/home
	echo "Copying system to disk" 
	dd if=/dev/sdc of=/tmp/home/lemote.tar.gz bs=16384 skip=1024 count=8000 2>/dev/null
	mke2fs -q -j /dev/sda1
	mount -t ext4 /dev/sda1 /mnt
	echo "Installing system to hard disk ..."
	tar xzf /tmp/home/lemote.tar.gz -C /mnt
	test -x /mnt/usr/bin/sudo && chmod u+s /mnt/usr/bin/sudo
	test -x /mnt/usr/bin/Xorg && chmod u+s /mnt/usr/bin/Xorg
	create_home
	create_bootcfg
	create_fstab
	umount /mnt
	umount /tmp/home
	
}

. /etc/functions.sh
echo 0 > /proc/sys/kernel/printk

load_modules /etc/modules
for f in /etc/modules.d/*; do
	[[ -e $f ]] && load_modules /etc/modules.d/*
	break
done

echo "  ___                      _    ____  _  __ "
echo " / _ \ _ __   ___ _ __    / \  |  _ \| |/ / "
echo "| | | | '_ \ / _ \ '_ \  / _ \ | | | | ' /  "
echo "| |_| | |_) |  __/ | | |/ ___ \| |_| | . \  "
echo " \___/| .__/ \___|_| |_/_/   \_\____/|_|\_\ "
echo "      |_|                                   "
echo ""
# settle down, after module load
sleep 2
echo "Should I install OpenADK to disk (d) or usb disk (u) or do nothing (any key)"
read answer
if [ "$answer" = "d" ];then
	disk_install
fi
if [ "$answer" = "u" ];then
	usb_install
fi
echo "Should I reboot now (y/n)"
read answer
if [ "$answer" = "y" ];then
	/bin/busybox reboot -f
else
	exit
fi
