#!/bin/sh
#
# bootcd2disk
#
# DISK=auto, we will build an boot partition with size defined in BOOTS

BOOTCD2DISKLIB=/usr/share/bootcd/bootcd2disk.lib
if [ -f $BOOTCD2DISKLIB ];then
  . $BOOTCD2DISKLIB $*
else
  echo "Error: Mandatory lib $BOOTCD2DISKLIB not found! ABORT!"
  exit 1
fi

### Main ###

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 $OPT !!!" | tee -a $ERRLOG
  [ "$VFAT" ] && echo "Partition $VFAT will be newly created as vfat !!!" | tee -a $ERRLOG
  echo -n "(y/n) " | tee -a $ERRLOG
  if [ "$SCRIPT" ]; then
    A="y"
  else
    read A
  fi
  echo "$A" >> $ERRLOG
done
if [ "$A" = "n" ]; then
  exit 1
fi

trap cleanup 2

if [ "$SFDISK" ]; then
  echo "--- Partitioning Disk $DISK ---" | tee -a $ERRLOG
  stdout "^ $DISK: "
  stdout "^sfdisk: ERROR: sector .* does not have an msdos signature"
  stdout "^No partitions found"
  run "echo \"$SFDISK\" | sfdisk -qL -uM $DISK >/dev/null"
fi

for i in $VFAT; do
  echo "--- Building Filesystem $i with mkdosfs ---" | tee -a $ERRLOG
  stdout "^mkdosfs "
  run "mkdosfs $i"
done

OPT=""
[ "$EXT3" = "yes" ] && OPT="-j"
for i in $EXT2FS; do
  echo "--- Building Filesystem $i with mke2fs $OPT ---" | tee -a $ERRLOG
  stdout "^mke2fs "
  run "mke2fs $OPT -q $i"
done

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

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

#find mountpoints and exclude them, otherwise cpio might complain
CPIOEXMOUNT="$(mount | grep /mnt/ | awk '{gsub(/\/mnt/,"")};{print}')"
CPIOEXCLUDE=""
if [ ! -z "$CPIOEXMOUNT" ];then
  for AEXCLUDE in $CPIOEXMOUNT; do
    CPIOEXCLUDE="$CPIOEXCLUDE -e '^$AEXCLUDE\$'"
  done
fi

echo "--- copying CD and RAM ---" | tee -a $ERRLOG
run "find / -mount|grep -v -e '^/var.ro\>' -e '^/var\>' $CPIOEXCLUDE |cpio --quiet -pdum /mnt"
run "cd /var.ro; find . -mount | cpio --quiet -pdum /mnt/var"
run "find /mnt/etc /mnt/tmp /mnt/dev /mnt/home /mnt/root -type l | xargs -r rm"
run rm -r /mnt/etc.ro /mnt/tmp.ro /mnt/dev.ro /mnt/home.ro /mnt/root.ro
run cp -a -x /ram1/etc /ram1/tmp /ram1/dev /ram1/home /ram1/root /mnt
run rm -f /mnt/fastboot /mnt/cdboot.catalog /mnt/cdboot.img /mnt/ram1.cpio.gz /mnt/ram2.cpio.gz
run rm -rf /mnt/rr_moved/ /mnt/isolinux/
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 \
  /mnt/etc/bootcd/thisbootcd.conf
run rmdir /mnt/ram1 /mnt/ram2

echo "--- deleting NOT_TO_RAM symlinks ---" 
NOT_TO_RAM_RO=$(find /ram1 /ram2 -xdev -type l | xargs /bin/ls -l | grep -e " -> /etc.ro/" -e " -> /home.ro/" -e " -> /root.ro/" | awk '{ print $(NF) }')
for i in $NOT_TO_RAM_RO; do     # exp: i=/etc.ro/X11
  j=$(echo $i | sed "s/\.ro//") #   => j=/etc/X11
  run rm /mnt/$j                #   => /mnt/etc/X11 -> /etc.ro/X11
  run cp -a $i /mnt/$j
done

# only call the function, if it is defined
doit=$(egrep "^[[:space:]]*(function)*[[:space:]]*after_copy" $CONFDIR/bootcd2disk.conf)
if [ ! -z "$doit" ]; then
  echo "--- do function after_copy ---" | tee -a $ERRLOG
  after_copy
fi

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

echo "--- Resetting mtab ---" | tee -a $ERRLOG
run "rm -f /mnt/etc/mtab"

if [ "$GRUB" ]; then
  echo "--- Building grub/menu.lst ---" | tee -a $ERRLOG
  run "echo \"$GRUB\" > /mnt/boot/grub/menu.lst"
fi

if [ "$LILO" ]; then
  echo "--- Building lilo.conf ---" | tee -a $ERRLOG
  run "echo \"$LILO\" > /mnt/etc/lilo.conf"
fi

if [ "$ELILO" ]; then
  echo "--- Building elilo.conf ---" | tee -a $ERRLOG
  run "echo \"$ELILO\" > /mnt/etc/elilo.conf"
fi

if [ "$SSHHOSTKEY" = "yes" ]; then
  # each installed PC gets a unique hostkey (only if hostkey already existed)
  create_host_keys /mnt/etc/ssh

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

echo "--- Enabling turned off stuff ---" | tee -a $ERRLOG
for f in $DISABLE_CRON; do
  if [ -f /mnt/$f.no_run_on_bootcd ]; then 
    run rm /mnt/$f
    run mv /mnt/$f.no_run_on_bootcd /mnt/$f
  fi
done

if [ "$GRUB" ]; then
  [ -z "$GRUBBOOTDIR" ]&&GRUBBOOTDIR=0
  echo "--- Running grub ---" | tee -a $ERRLOG
echo "root (hd0,$GRUBBOOTDIR)
setup (hd0)
quit
" | grub --batch
fi

if [ "$LILO" ]; then
  echo "--- Running lilo ---" | tee -a $ERRLOG
  run "chroot /mnt mount /proc"
  stdout "^Added "
  run "chroot /mnt lilo -w"
  run "chroot /mnt umount /proc"
fi

if [ "$ELILO" ]; then
  echo "--- Running elilo ---" | tee -a $ERRLOG
  run "chroot /mnt elilo"
fi

echo "--- unmounting DISK ---" | tee -a $ERRLOG
run cp $ERRLOG /mnt/var/log
run "$UMOUNT"

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