#! /bin/sh
#
# ISO Creator with GRUB
#
#   Adam Lackorzynski <adam@os.inf.tu-dresden.de>
#

set -e

if [ "$(id -u)" = 0 ]; then
  echo "Do not run me as root, it's not necessary"
  exit 1
fi

if [ -z "$2" ]; then
  echo "Usage: $0 systemdir iso-image-file"
  exit 1
fi

systemdir=$1
isoimage=$2
[ "${isoimage#/}" = "$isoimage" ] && isoimage=$(pwd)/$isoimage

mkdir -p "$systemdir"

if [ ! -e "$systemdir/boot/grub/stage2_eltorito" ]; then
  if [ -e /usr/lib/grub/i386-pc/stage2_eltorito ]; then
    COPYPATH=/usr/lib/grub/i386-pc
  elif [ -e /usr/share/grub/i386-pc/stage2_eltorito ]; then
    COPYPATH=/usr/share/grub/i386-pc
  elif [ -e /boot/grub/stage2_eltorito ]; then
    COPYPATH=/boot/grub
  fi
  if [ -z "$COPYPATH" ]; then
    echo "Cannot find a stage2_eltorito file..."
    exit 1
  fi

  # copy files
  mkdir -p "$systemdir/boot/grub"
  cp $COPYPATH/stage2_eltorito "$systemdir/boot/grub"
  chmod 644 "$systemdir/boot/grub/stage2_eltorito"
fi

if [ ! -e "$systemdir/boot/grub/menu.lst" ]; then
  cat -<<EOF > "$systemdir/boot/grub/menu.lst"
color 23 52

title Example entry
kernel /bootstrap -serial
modaddr 0x0200000
module /fiasco -nowait -serial_esc -comspeed 115200 -comport 1 -nokdb
module /sigma0
module /roottask
module /hello
EOF
fi

cd "$systemdir"
mkisofs -R -b boot/grub/stage2_eltorito \
        -no-emul-boot -boot-load-size 4 -boot-info-table \
        -hide-rr-moved \
        -J -joliet-long \
        -o "$isoimage" .
