#!/bin/sh
# Filename:      grml-live-remaster
# Purpose:       remaster a grml from the live cd
# Authors:       grml-team (grml.org),
#                (c) Michael Schierl <schierlm@gmx.de>,
#                (c) Michael Prokop <mika@grml.org>,
#                (c) Thorsten Glaser <tg@mirbsd.org>
# Bug-Reports:   see http://grml.org/bugs/
# License:       This file is licensed under the GPL v2 or any later version.
################################################################################
# DISCLAIMER:
# this script currently lacks LOTS of error checking code... any help welcome...
################################################################################

# define function getfilesize before "set -e"
if stat --help >/dev/null 2>&1; then
  getfilesize='stat -c %s'      # GNU stat
else
  getfilesize='stat -f %z'      # BSD stat
fi

set -e # exit on any error

VERSION='0.0.2'
GRML_LIVE_EDITOR=${VISUAL:-${EDITOR:-vi}}

# source core functions {{{
. /etc/grml/lsb-functions
. /etc/grml/script-functions
# }}}

# make sure we have what we need {{{
check4progs mkisofs stat || exit 1

# allow overriding via environment:
if [ -z "$MKSQUASHFS" ] ; then
  if which mksquashfs-lzma >/dev/null 2>&1 ; then
    MKSQUASHFS=mksquashfs-lzma
  elif which mksquashfs >/dev/null 2>&1 ; then
    MKSQUASHFS=mksquashfs
  else
    echo "Error: neither mksquashfs-lzma nor mksquashfs present. Exiting."
    exit 1
  fi
fi
check4root || exit 1
# }}}

if [ x"$1" == x ]; then
   echo "$0 - version $VERSION"
   echo ""
   echo "Usage: $0 destination.iso"
   echo "  destination.iso should point to a path that is on a hard disk,"
   echo "  you might want to mount some swap partitions or swap files"
   echo "  first, because grml-live-remaster will need a lot ot RAM."
   echo ""
   echo "Please report bugs and feature requests: http://grml.org/bugs/"
   exit 1
fi

if [ ! -d /remaster ]; then
   mkdir -p /remaster/chroot /remaster/tmp /remaster/cdrom
   mount -t tmpfs tmpfs /remaster/tmp
   echo "#:# edit the following two lines to change the boot message" \
           >/remaster/msg
   echo "#:#" >>/remaster/msg
   if [ -r /live/image/boot/isolinux/boot.msg ] ; then
      sed 1,2d /live/image/boot/isolinux/boot.msg >>/remaster/msg
   else
      sed 1,2d /live/image/boot.msg >>/remaster/msg
   fi
fi

SQUASHFS_FILE="$(find /live/image/live -name \*.squashfs | head -1)"
if ! grep -q "/remaster/cdrom squashfs" /proc/mounts ;  then
   mount -t squashfs "$SQUASHFS_FILE" /remaster/cdrom -o ro,loop
fi

if ! grep -q "aufs /remaster/chroot" /proc/mounts ; then
   mount -t aufs aufs /remaster/chroot -o br:/remaster/tmp=rw:/remaster/cdrom=rr
fi

for i in dev proc root sys tmp; do
    mount --bind /$i /remaster/chroot/$i
done

echo "Now edit the contents of the live CD in this chrooted shell:"
chroot /remaster/chroot

for i in dev proc root sys tmp; do
        umount /remaster/chroot/$i
done

${GRML_LIVE_EDITOR} /remaster/msg

[ -d /remaster/iso ] || mkdir /remaster/iso

for i in /live/image/*; do
    if [ ! $i == /live/image/live ]; then
       cp -R $i /remaster/iso
    fi
done

if [ -r /remaster/iso/boot/isolinux/boot.msg ] ; then
   rm /remaster/iso/boot/isolinux/boot.msg
fi

# make sure we support usb sticks as well:
if [ -d /live/image/boot/isolinux ] ; then
   BOOTSTUFF=/live/image/boot/isolinux
else
   BOOTSTUFF=/live/image
fi

[ -d /remaster/iso/boot/isolinux ] || mkdir -p /remaster/iso/boot/isolinux

sed 3,4d "${BOOTSTUFF}"/boot.msg \
        >/remaster/iso/boot/isolinux/boot.msg
sed 1,2d /remaster/msg >>/remaster/iso/boot/isolinux/boot.msg

mkdir /remaster/iso/live
$MKSQUASHFS /remaster/chroot /remaster/iso/live/"$(basename $SQUASHFS_FILE)"
umount /remaster/chroot /remaster/cdrom

if [ -f /remaster/iso/boot/isolinux/isolinux.bin ] ; then
   ISOLINUX=boot/isolinux/isolinux.bin
   ISOLINUX_BOOTCAT=boot/isolinux/boot.cat
else
   ISOLINUX=isolinux.bin
   ISOLINUX_BOOTCAT=boot.cat
fi

mkisofs -b $ISOLINUX -no-emul-boot -c $ISOLINUX_BOOTCAT \
        -boot-info-table -boot-load-size 4 -no-pad \
        -l -r -J -o "$1" /remaster/iso
# pad for partition table
siz=$($getfilesize "$1")
cyls=$((siz / 512 / 32 / 16 + 1))       # C=$cyls H=16 S=32 (= 256 KiB units)
siz=$((cyls * 16 * 32 * 512))           # size after padding
dd if=/dev/zero bs=1 count=1 seek=$((siz - 1)) of="$1" 2>/dev/null
rm -R /remaster/iso

# here is the place where we could apply bootgrub.mksh

echo ""
echo "ISO generation complete:"
ls --color -l "$1"
echo "If you want to customize your ISO, just call grml-live-remaster again."

## END OF FILE #################################################################
# vim: ai filetype=sh expandtab
