#!/bin/sh -e

# Install a Gentoo system
# This script is run on tty1 when the live CD is coming up.
#
# Christoph Lameter, <christoph@lameter.com>, July 17, 2002
#
# (C) 2002 Lameter International Corporation.

TMP=/tmp/deploy-$$
mkdir $TMP || true
mkdir /mnt/gentoo || true

# Make sure that an earlier abort in deploy does not mess up things.
umount /mnt/gentoo/boot || true
umount /mnt/gentoo || true

getdisk() {
	dialog --title "Disk Selection" --menu "Please select the Disk to use" 15 60 7 \
	hda	"First IDE Harddisk" \
	sda	"First SCSI Harddisk" \
	custom	"Input the partition name" 2>$TMP/disk || exec $0

	DISK=$(<$TMP/disk)
	if [ "$DISK" = "custom" ]; then
		dialog --title "Disk Selection" --inputbox "Disk Name (hdb,sdb,rd/... etc)" \
		10 60 2>$TMP/disk || exec $0
		DISK=$(<$TMP/disk)
	fi
}

getpartition() {
  PMATCH="Linux\$"
  if [ "$2" != "" ]; then
    PMATCH="Linux $2"
  fi
  PARTLIST=$(fdisk -l /dev/$DISK | grep "^/dev/.*$PMATCH" | \
    sed -e "s%^/dev/$DISK\([0-9]*\)[ *\t]*[^ ]*[ \t]*[^ ]*[ \t]*\([0-9]*\).*%\1 \2KB%")
  dialog --title "Partition Selection" \
          --menu "Please select the $1 Partition to install to" 15 60 8 \
	  $PARTLIST 2>$TMP/function || exec $0
  PARTITION=$(<$TMP/function)
}

wantbootpart () {
  dialog --title "Partition Information" --yesno "Do you want to have a separate /boot partition" 6 60
  return $?
}

getpartparm() {
  if wantbootpart; then
    getpartition "/boot"
    BOOTPART=$PARTITION
  fi
  getpartition "swap" swap
  SWAPPART=$PARTITION
  getpartition "/"
  ROOTPART=$PARTITION
}

infobox () {
  dialog --title "Please wait ..." --infobox "$1" 4 50
}


formatroot() {
  infobox "Formatting / Partition (ext3) on $DISK$1"
  mke2fs -j -q /dev/$DISK$1 >>$TMP/result 2>&1
}

formatboot() {
  infobox "Formatting /boot Partition (ext2) on $DISK$1"
  mke2fs -q /dev/$DISK$1 >>$TMP/result 2>&1
}

formatswap() {
  infobox "Formatting Swap Partition on $DISK$1"
  mkswap /dev/$DISK$1 >>$TMP/result 2>&1
}

showresult() {
  dialog --title "$1" --textbox $TMP/result 24 76
}

#
# For some strange reason grub is not up the task at times
#
grubsetup() {
	  grub-install --root-directory=/mnt/gentoo /dev/$DISK >>$TMP/result 2>&1
# And the menu.lst for grub
	  cat >/mnt/gentoo/boot/grub/menu.lst <<EOF
#serial --unit=0
#terminal --timeout=10 console serial
timeout 10
default 0
# For booting Linux
title  TelemetryBox/Linux
# The following could be wrong. BAD BAD grub is not following linux standards
root (hd0,2)
kernel /boot/vmlinuz root=/dev/$DISK$ROOTPART
#initrd /initrd.img
EOF
}

lilosetup() {
  infobox "Setting up lilo"
	cat >/mnt/gentoo/etc/lilo.conf <<EOF
lba32
#message = /boot/penguins.boot
prompt
timeout = 100
vga = 0x314
boot=/dev/$DISK
root=/dev/$DISK$ROOTPART
image=/boot/vmlinuz
	label=Linux
	read-only
EOF
	lilo -r /mnt/gentoo
}


mountparts() {
	infobox "Mounting Partitions"
	mount /dev/$DISK$ROOTPART /mnt/gentoo 2>>$TMP/result
        if [ "$BOOTPART" != "" ]; then
          mkdir /mnt/gentoo/boot || true
	  mount /dev/$DISK$BOOTPART /mnt/gentoo/boot 2>>$TMP/result
        fi
}

umountparts() {
  infobox "Unmounting Partitions"
  if [ "$BOOTPART" != "" ]; then
    umount /mnt/gentoo/boot 2>>$TMP/result
  fi
  umount /mnt/gentoo 2>>$TMP/result
}

copycd () {
  infobox "Copying CD content to $DISK$ROOTPART"
  cp -a /cdroot/* /mnt/gentoo >>$TMP/result 2>&1
  # Get rid of certain dangerous things
  infobox "Running finalization script"
  chroot /mnt/gentoo isolinux/install-complete
}


bootable() {
  if [ ! -d /mnt/gentoo/etc ]; then
    dialog --msgbox "Cannot make empty partitions bootable" 5 50
    exec $0
  fi

  infobox "Generating initial /etc/fstab"
# Generate /etc/fstab
  cat >/mnt/gentoo/etc/fstab <<EOF
# /etc/fstab: static file system information.
#
# <file system> <mount point>   <type>  <options> <dump> <pass>
/dev/$DISK$ROOTPART	/		ext3 defaults 0      1
proc            /proc           proc    defaults  0      0
/dev/$DISK$SWAPPART   swap            swap    swap
EOF

  if [ "$BOOTPART" != "" ]; then
    echo "/dev/$DISK$BOOTPART	/boot		ext2 defaults 0 2" >>/mnt/gentoo/etc/fstab
  fi
#	  cat /etc/initial.modules >>/mnt/gentoo/etc/mkinitrd/modules
# mkinitrd is not able to determine the root filesystem.
#  echo "ext3" >>/etc/mkinitrd/modules
#  echo "ROOT=/dev/$DISK$ROOTPART" >/etc/mkinitrd/mkinitrd.conf

#  INITRD=initrd-`uname -r`
#  rm /mnt/gentoo/boot/$INITRD || true
#  rm /mnt/gentoo/initrd.img || true
#  infobox "Preparing initrd"
#  mkinitrd -o /mnt/gentoo/boot/$INITRD /lib/modules/*
#  ln -s boot/$INITRD /mnt/gentoo/initrd.img
  lilosetup
}

sfdiskpart() {
#
# SFDISK did some strange things to a HD maybe better avoid it?
#
  sfdisk -uM /dev/$1 >>$TMP/result 2>&1 <<EOF
,20,L
,200,S
,,L
EOF
}

#
# Parted unable to extend the last partition to the end of the hd
# without trickery
#
partedpart() {
	parted -s /dev/$1 >>$TMP/result 2>&1 <<EOF
mklabel msdos
mkpart primary ext2fs 0 20
mkpart primary linux-wap 21 300
mkpart primary ext2fs 301 -
quit
EOF
}

fdiskpart() {
  fdisk /dev/$1 >>$TMP/result 2>&1 <<EOF
d
1
d
2
d
3
d
4
n
p
1

+40M
a
1
n
p
2

+400M
t
2
82
n
p
3


w
"
EOF
}


autopart() {
  infobox "Partitioning Disk $1"
  fdiskpart $1
}

# Deployment of TelemetryBox

dialog --backtitle "Lameter International Corporation presents" \
        --title "TelemetryBox/Gentoo V$(</etc/telemetrybox_version) Deployment" \
	--menu "It is recommended that the whole harddisk be dedicated \
to the TelemetryBox. Simply select 'install' and the harddisk will be \
made ready for starting up a TelemetryBox. The other functions available \
here allow for custom installations that will allow dual boots and other \
refined tricks but this is only advisable for experts." 18 73 7 \
	install "Wipeout first HD and install with defaults" \
	cfdisk	"Partition the Hardddisk" \
	autopart "Autopartition (20M /boot, 200M Swap and big /)" \
	format	"Format a Partition" \
	copycd	"Copy CD image to a partition" \
	bootable "Make a Partition bootable" \
	reboot	"Reboot" \
	shutdown	"Shutdown" \
	exit	"Exit Install" \
	    2>$TMP/function

>$TMP/result
case $(<$TMP/function) in
	exit)		rm -rf $TMP;exit 0;;
	shutdown)	rm -rf $TMP;shutdown -h now;sleep 5m;;
	reboot)		rm -rf $TMP;shutdown -r now;sleep 5m;;
	cfdisk)		
		getdisk
		cfdisk /dev/$DISK
		;;
	install)
		getdisk
		autopart $DISK
		SWAPPART=2
		BOOTPART=1
		ROOTPART=3
		formatboot $BOOTPART
                formatswap $SWAPPART
		formatroot $ROOTPART
		mountparts
		copycd
		bootable
		umountparts
		showresult "Installation Results"
		;;
	format)
		getdisk
		dialog --title "Formatting" --menu "Type of Partition to Format"  12 60 3 \
		   formatboot	"Format a /boot partition" \
		   formatroot 	"Format a / partition" \
		   formatswap	"Format a swap partition" 2>$TMP/out || exec deploy	
		PTYPE=$(<$TMP/out)
		if [ "$PTYPE" == "formatswap" ]; then
		   getpartition "" swap
                else
		  getpartition ""
	        fi
		$PTYPE $PARTITION
		showresult "$PTYPE Partition Formatted on $DISK$PARTITION"
		;;
	copycd)
		getdisk
		getpartparm
		mountparts
		copycd
		umountparts
		echo Done >>$TMP/result
		showresult "CD content copied to partition $DISK$PARTITION"
		;;
	bootable)
		getdisk
		getpartparm
		mountparts
		bootable
		umountparts
		echo Done >>$TMP/result
		showresult "Partition $DISK$PARTITION made bootable"
		;;
	autopart)
		getdisk
		autopart $DISK
		showresult "Disk $DISK partitioned"
		;;
esac

exec $0
