#!/bin/bash
# 
# KickMe - Create a custom Kickstart installation CD
#
# Requires a working disc1 from a Fedora Core installation CD set.
# (Not yet tested with DVD images, but I expect that works too.)
#
# Eric Myers <myers@spy-hill.net>  -  4 December 2007
# Copyright 2007 by Eric Myers, all rights reserved.
# @(#) $Id:$
#######################################################################

echo " "
echo "KickMe: create a customized Fedora installation disk. "
echo " "

#
# Working directory.  Must have room for both the unpacked filesystem 
# and the disk image (so at least twice as much as one image).

SCRATCH=/scratch


CDROM=/cdrom
IMAGEDIR=${SCRATCH}/kick-me
IMAGE=${SCRATCH}/kick-me.iso

# Where to get configuration and message files
# Boot configuration under isolinux, kickstart under kick-me

CONFIG=~myers/linux/ks

# To syncronize with rsync, with file exclusion:
#
RSYNC="rsync -rlptgvu  --delete  --exclude=boot.cat --exclude=TRANS.TBL "
RCOPY="rsync -rlptgvu  --exclude=\"\*~\" "


##
# Functions:

not_disc1() {
  echo " "
  echo "! The disk in the drive is not disc1.  "
  echo "  Please mount disc1 and re-run this script.  "
  exit 1
}

burn_failed() {
  echo " "
  echo "! The burn may have failed.  $1 is missing. "
  exit 7
}


## 
# Use any existing image?

if [ -f $IMAGE ]; then
  echo "* A disk image already exists:"
  echo " "
  ls -l $IMAGE
  echo " "
  echo "  Do you wish to remove it and start over?"
  read ANSR
  [ "$ANSR" = "exit" ] && exit 1
  if [ "$ANSR" = "y" -o "$ANSR" = "yes" ]; then 
     echo -n "* Removing disk image...  "
     /bin/rm -f $IMAGE ${IMAGE}.sha1
     echo "Image removed."
  fi
fi

##
# If there is an ISO image then go burn it.
# If not, build it.

if [ ! -f $IMAGE ]; then

  echo "* Creating a new ISO image.  Start by copying the original. "

  # Mount and verify the original disc1
  # (Eject what is there to be sure we can do so.)
  
  eject ${CDROM}

  echo "* Please insert Fedora install disc1 into the CD drive and press RETURN.  "
  read ANSR
  [ "$ANSR" = "exit" ] && exit 1

  mount ${CDROM}

  [ ! -d ${CDROM}/isolinux ] && not_disc1
  [ ! -d ${CDROM}/repodata ] && not_disc1
  [ ! -f ${CDROM}/repodata/comps.xml ] && not_disc1


  # Working directory:

  [ ! -d $IMAGEDIR ] && mkdir -p $IMAGEDIR


  # Copy disk image to working directory:

  $RSYNC  ${CDROM}/           $IMAGEDIR/
  /bin/rm -f ${IMAGEDIR}/isolinux/boot.cat

  sed -d "s/Fedora Core/Kick Me: Fedora Core/" \
	 < ${CDROM}/.discinfo >$IMAGEDIR/.discinfo


  # check our work

  if [ ! -d $IMAGEDIR/isolinux ]; then
    echo "! Copying disc1 failed. "
    exit 2
  fi

  if [ ! -f $IMAGEDIR/isolinux/initrd.img ]; then
    echo "! Copying disc1 failed. No initrd.img found.  "
    exit 3
  fi


  # Boot configuration and messages

  cp -p ${CONFIG}/isolinux/isolinux.cfg $IMAGEDIR/isolinux
  cp -p ${CONFIG}/isolinux/*.msg        $IMAGEDIR/isolinux

  # Kickstart configuration and supporting files

  mkdir $IMAGEDIR/kick-me
  [ -d ${CONFIG}/kick-me ] && $RCOPY ${CONFIG}/kick-me $IMAGEDIR/

  if [ -f ${IMAGEDIR}/kick-me/ks.cfg ]; then

    # Interactive version of ks.cfg:
    #
    sed -e "s/^#interactive/interactive/"  ${CONFIG}/kick-me/ks.cfg \
	> $IMAGEDIR/kick-me/ks-interactive.cfg

    # Dangerous version of ks.cfg which wipes all partitions:
    #
    sed -e "s/^clearpart  --linux/clearpart -all/"  ${CONFIG}/kick-me/ks.cfg \
	> $IMAGEDIR/kick-me/ks-wipe.cfg

  fi

  date > $IMAGEDIR/CREATE_DATE


  ## 
  # Ready to create image.  We can swap the CD for a blank while
  # the image is being generated.

  echo "  "
  echo "* Filesystem is now ready to image.  Please check that it is correct. "
  echo "  (While the image is being constructed you may remove disc1 from "
  echo "  the tray and replace it with blank media.)  "
  echo "  "
  echo "* Press ENTER when you are ready to proceed.  " 
  read ANSR
  [ "$ANSR" = "exit" ] && exit 1

  eject ${CDROM}
  echo "  "
  echo "* Please remove disc1 and replace it with blank media.   "
  sleep 2

  # Now create new ISO image of this filesystem

  echo " "
  echo "* Building new ISO9660 image..."
  echo " "

  mkisofs  -o $IMAGE -r -J -N -d -hide-rr-moved \
	   -sysid `hostname`  -V "Kick-Me: Fedora Core 6"  \
	   -p "Eric Myers <myers@spy-hill.net>" \
	   -publisher "Spy Hill Research" \
	   -no-emul-boot -boot-info-table -boot-load-size 4 \
	   -b isolinux/isolinux.bin -c isolinux/boot.cat \
	   $IMAGEDIR
  RC=$?
  if [ $RC -ne 0 ]; then
    echo "! mkisofs failed.  Return code $RC  "
    exit $RC
  fi

fi

##
# SHA1 hash of the image

echo -n "* Computing the SHA1 hash of the ISO image...  "
SHA1SUM=`sha1sum $IMAGE`
echo " Done. "

echo "* SHA1 sum for $IMAGE is: "
echo "  $SHA1SUM "
echo $SHA1SUM > ${IMAGE}.sha1


##
# Burn to CD or DVD

echo " " 
echo "* Now ready to burn the ISO image to CD.  "
echo "  When ready to proceed, press ENTER.  "
read ANSR
[ "$ANSR" = "exit" ] && exit 1


if [ ! -f $IMAGE ]; then
  echo "No ISO image found: $IMAGE "
  exit 4
fi

echo "  "
echo "* Burning ISO image to CD...."
echo "  "

cdrecord -v driveropts=burnfree -raw  -data $IMAGE

echo "  "
echo "* Image burned to CD.  Let's mount it and check it...  "
echo "  "

mount ${CDROM}
ls ${CDROM}

[ ! -d ${CDROM}/isolinux ]           && burn_failed isolinux
[ ! -d ${CDROM}/repodata ]           && burn_failed repodata
[ ! -f ${CDROM}/repodata/comps.xml ] && burn_failed comps.xml
[ ! -d ${CDROM}/kick-me ]            && burn_failed kick-me
[ ! -f ${CDROM}/kick-me/ks.cfg ]     && burn_failed kick-me/ks.cfg

eject ${CDROM}
echo "  "
echo "* Basic tests passed.  This CD looks okay.  Done.  "
echo "  "

exit 0

#EOF#
