#!/bin/sh
#
# bootcd2disk
#
CD2DCNF="/etc/bootcd/bootcd2disk.conf"
# DISK=auto, we will build an boot partition with size defined in BOOTS
BOOTS=50

VERBOSE=""
if [ "$1" = "-v" ]; then
  VERBOSE="$1"
  shift
fi

if [ "$#" != "0" ]; then
  echo "Usage: bootcd2disk [-v]"
  echo "  use man bootcd2disk to get help"
  echo "  and see $CD2DCNF"
  exit 1
fi

CONFVARS="DISK SFDISK EXT2FS SWAP MOUNT UMOUNT FSTAB LILO SSHHOSTKEY"
unset $CONFVARS
. $CD2DCNF
. /usr/lib/bootcd/bootcd.lib

for i in $CONFVARS; do
  [ "`set | grep ^$i=`" ] || err "$i is not set in /etc/bootcd/bootcdwrite.conf"
done

cleanup()
{
  echo "cleanup"
}

date "+--- $0 %d.%m.%Y ---" > $ERRLOG
echo "To see full output: tail -f $ERRLOG" | tee -a $ERRLOG


if [ "$SFDISK" != "auto" ]; then
  [ "$EXT2FS" = "auto" ] && warn "To define EXT2FS=auto makes only sence if you define SFDISK=auto too"
  [ "$SWAP" = "auto" ] && warn "To define SWAP=auto makes only sence if you define SFDISK=auto too"
fi

if [ "$EXT2FS" != "auto" ]; then
  [ "$MOUNT" = "auto" ] && warn "To define MOUNT=auto makes only sence if you define EXT2FS=auto too"
  [ "$UMOUNT" = "auto" ] && warn "To define UMOUNT=auto makes only sence if you define EXT2FS=auto too"
  [ "$FSTAB" = "auto" ] && warn "To define FSTAB=auto makes only sence if you define EXT2FS=auto too"
  [ "$LILO" = "auto" ] && warn "To define LILO=auto makes only sence if you define EXT2FS=auto too"
fi

if [ "$DISK" = "auto" ]; then
  DISK=`sfdisk -s -uM | head -1 | cut -d: -f1`
  [ -b $DISK ] || err "Could not automatically calculate DISK"
  echo "auto DISK=<$DISK>" >>$ERRLOG
  [ "$VERBOSE" ] && echo "DISK=<$DISK>"
fi

if [ "$SFDISK" = "auto" ]; then
  [ -b "$DISK" ] || err \
    "If you define SFDISK=auto, please define DISK as auto or <block-device>"
  MEMS=`free | grep Mem | awk '{print $2}'`
  SWAPS=`echo "2*$MEMS/1024" | bc`
  SFDISK="
,$BOOTS
,$SWAPS,S
;
"
  echo "auto SFDISK=<$SFDISK>" >>$ERRLOG
  [ "$VERBOSE" ] && echo "SFDISK=<$SFDISK>"
fi

if [ "$EXT2FS" = "auto" ]; then
  [ -b "$DISK" ] || err "If you define EXT2FS=auto, please define DISK as auto or <block-device>"
  EXT2FS="${DISK}1 ${DISK}3"
  echo "auto EXT2FS=<$EXT2FS>" >>$ERRLOG
  [ "$VERBOSE" ] && echo "EXT2FS=<$EXT2FS>"
fi

if [ "$SWAP" = "auto" ]; then
  [ -b "$DISK" ] || err "If you define SWAP=auto, please define DISK as auto or <block-device>"
  SWAP="${DISK}2"
  echo "auto SWAP=<$SWAP>" >>$ERRLOG
  [ "$VERBOSE" ] && echo "SWAP=<$SWAP>"
fi

if [ "$MOUNT" = "auto" ]; then
  [ -b "$DISK" ] || err "If you define MOUNT=auto, please define DISK as auto or <block-device>"
  MOUNT="mount ${DISK}3 /mnt; mkdir /mnt/boot; mount ${DISK}1 /mnt/boot"
  echo "auto MOUNT=<$MOUNT>" >>$ERRLOG
  [ "$VERBOSE" ] && echo "MOUNT=<$MOUNT>"
fi

if [ "$UMOUNT" = "auto" ]; then
  [ -b "$DISK" ] || err "If you define UMOUNT=auto, please define DISK as auto or <block-device>"
  UMOUNT="umount /mnt/boot; umount /mnt"
  echo "auto UMOUNT=<$UMOUNT>" >>$ERRLOG
  [ "$VERBOSE" ] && echo "UMOUNT=<$UMOUNT>"
fi

if [ "$FSTAB" = "auto" ]; then
  [ -b "$DISK" ] || err "If you define FSTAB=auto, please define DISK as auto or <block-device>"
  FSTAB="
${DISK}1 /boot ext2 defaults 0 1
${DISK}2 none  swap sw 0 0
${DISK}3 /     ext2 defaults,errors=remount-ro 0 1
proc      /proc proc defaults 0 0
"
  echo "auto FSTAB=<$FSTAB>" >>$ERRLOG
  [ "$VERBOSE" ] && echo "FSTAB=<$FSTAB>"
fi

if [ "$LILO" = "auto" ]; then
  [ -b "$DISK" ] || err "If you define LILO=auto, please define DISK as auto or <block-device>"

LILO="
boot=$DISK
delay=20
vga=0

image=/vmlinuz
root=${DISK}3
label=Linux
read-only
"
  echo "auto LILO=<$LILO>" >>$ERRLOG
  [ "$VERBOSE" ] && echo LILO="<$LILO>"
fi

echo ""
A=""
while [ "$A" != "y" -a "$A" != "n" ]
do
  [ "$DISK" ] && echo "Harddisk $DISK will be erased!!!" | tee -a $ERRLOG
  [ "$SWAP" ] && echo "Partition $SWAP will be newly created as SWAP !!!" | tee -a $ERRLOG
  [ "$EXT2FS" ] && echo "Partition $EXT2FS will be newly created as EXT2FS !!!" | tee -a $ERRLOG
  echo -n "(y/n) " | tee -a $ERRLOG
  read A; echo "$A" >> $ERRLOG
done
if [ "$A" == "n" ]; then
  exit 1
fi

trap cleanup SIGINT

echo "--- Partitioning Disk $DISK ---" | tee -a $ERRLOG
stdout "^ $DISK: "
stdout "^sfdisk: ERROR: sector .* does not have an msdos signature"
run "echo \"$SFDISK\" | sfdisk -qL -uM $DISK >/dev/null"

for i in $EXT2FS; do
  echo "--- making Partition Tables $i ---" | tee -a $ERRLOG
  stdout "^mke2fs .* for EXT2 FS"
  run "mke2fs $i >/dev/null"
done

for i in $SWAP; do
  echo "--- making SWAP $i ---" | tee -a $ERRLOG
  stdout "^Setting up swapspace"
  run "mkswap $i"
done

echo "--- mounting DISK ---" | tee -a $ERRLOG
run "$MOUNT"

echo "--- copying CD and RAM ---" | tee -a $ERRLOG
run cp -a -x / /mnt
run rm /mnt/etc /mnt/tmp /mnt/dev /mnt/var /mnt/home /mnt/root
run rm -r /mnt/etc.ro /mnt/tmp.ro /mnt/dev.ro /mnt/home.ro /mnt/root.ro
run mv /mnt/var.ro /mnt/var
run cp -a -x /ram1/etc /ram1/tmp /ram1/dev /ram1/home /ram1/root /mnt
run rm /mnt/fastboot /mnt/cdboot.catalog /mnt/cdboot.img
run rm /mnt/etc/rcS.d/S12bootcdram.sh \
  /mnt/etc/rcS.d/S13bootcdflop.sh \
  /mnt/usr/bin/bootcd2disk \
  /mnt/usr/bin/bootcdflopcp \
  /mnt/etc/bootcd/bootcd2disk.conf
run rmdir /mnt/ram1 /mnt/ram2

echo "--- Building fstab ---" | tee -a $ERRLOG
run "echo \"$FSTAB\" > /mnt/etc/fstab"

echo "--- Building lilo.conf ---" | tee -a $ERRLOG
run "echo \"$LILO\" > /mnt/etc/lilo.conf"

if [ "$SSHHOSTKEY" == "yes" ]; then
  # each installed PC gets a unique hostkey"
  echo "--- Building ssh_host_key ---" | tee -a $ERRLOG
  run mkdir -p /mnt/etc/ssh

  # just controlling exitcode for ssh-keygen, because there are different 
  # versions of ssh-keygen running and each one makes different output on
  # stdout and stderr
  run "rm -f /mnt/etc/ssh/ssh_host_key /mnt/etc/ssh/ssh_host_key.pub"
  run "ssh-keygen -b 1024 -f /mnt/etc/ssh/ssh_host_key -N '' >>$ERRLOG 2>&1"

elif [ "$SSHHOSTKEY" != "no" ]; then
  warn 'SSHHOSTKEY is not defined as "yes" or "no".' \
       'It will be treated as "no".'
fi

echo "--- Running lilo ---" | tee -a $ERRLOG
stdout "^Added "
run "chroot /mnt lilo"

echo "--- unmounting DISK ---" | tee -a $ERRLOG
run "$UMOUNT"

echo "Please Reboot now !" | tee -a $ERRLOG
