#!/usr/local/bin/bash
#
# Make and burn a CD-ROM image
# Greg Lehey, 26 June 2005
# 
# $Id: mkcd,v 1.3 2005/07/10 00:12:44 grog Exp $
#
# Adapted from:
# Module: mkisoimages.sh
# Author: Jordan K Hubbard
# Date:   22 June 2001
#
# $FreeBSD: src/release/i386/mkisoimages.sh,v 1.13 2005/01/30 21:10:51 kensmith Exp $
#
# Usage:
#
# mkcd [-i] [-b] [-s speed] [-I imagename] [-p projectname] [-v]
#
# -i specifies to build the ISO image.
# -I is the name of the ISO image to build.  Normally this is
#    specified in the project file, which always overrides the command
#    line variable.
# -b specifies to burn the ISO image to disk.
# -s specifies speed of burn (not needed for DVD)
# -v specifies verbose output from the script.
# -p specifies the name of a file that supplies the following variables:
#
# PUBLISHER	The name of the publisher
# BOOTFILE      The name of the bootstrap.  If this variable is missing or
#               null, the ISO image will not be bootable.
# BOOTCAT	For Linux at any rate, a boot catalogue.
# LABEL		
# DIRS		A list of directory names.
# ISOIMAGE	Name of ISO image.
# CDDEV		Device name of the CD burner
#
# whatever standards this architecture supports (may be unsupported),
# LABEL is the ISO image label, ISOIMAGE is the filename of the
# resulting ISO image, base-bits-dir contains the image contents and
# extra-bits-dir, if provided, contains additional files to be merged
# into base-bits-dir as part of making the image.

MKISO=
BURN=
SPEED=
VERBOSE=
bootable=

while [ "$#" -gt 1 ]; do
  if [ "$1" = "-i" ]; then
  	MKISO="YES"
  	shift
  fi
  
  if [ "$1" = "-b" ]; then
  	BURN="YES"
  	shift
  fi
  
  if [ "$1" = "-I" ]; then
  	ISOIMAGE=$2
  	shift 2
  fi
  
  if [ "$1" = "-s" ]; then
  	SPEED="-s $2"
  	shift
  	shift
  fi
  
  if [ "$1" = "-v" ]; then
  	VERBOSE="YES"
  	shift
  fi

  if [ "$1" = "-p" ]; then
     source $2
     shift 2
  fi
done

type mkisofs 2>&1 | grep " is " >/dev/null
if [ $? -ne 0 ]; then
	echo The cdrtools port is not installed.  Trying to get it now.
	if [ -f /usr/ports/sysutils/cdrtools/Makefile ]; then
		cd /usr/ports/sysutils/cdrtools && make install BATCH=yes && make clean
	else
		if ! pkg_add -r cdrtools; then
			echo "Could not get it via pkg_add - please go install this"
			echo "from the ports collection and run this script again."
			exit 2
		fi
	fi
fi

if [ "$VERBOSE" = "YES" ]; then
    echo "project file:	" $1
    echo "PUBLISHER:	" $PUBLISHER
    echo "LABEL:		" $LABEL
    echo "BOOTFILE:	" $BOOTFILE
    echo "BOOTCAT:	" $BOOTCAT
    echo "DIRS:		" $DIRS
    echo "ISOIMAGE:	" $ISOIMAGE
    echo "CDDEV:		" $CDDEV
    echo
fi

if [ "$MKISO" = "YES" ]; then
  if [ "$BOOTFILE" != "" ]; then
# This seems to work for Fedora.  It remains to be seen whether the 
# -boot-load-size 4 -boot-info-table cause problems elsewhere.
# Possibly it needs to be split out into a different option.
    bootable="$bootable -b $BOOTFILE -no-emul-boot -boot-load-size 4 -boot-info-table"
  fi
  if [ "$BOOTCAT" != "" ]; then
    bootable="$bootable -c $BOOTCAT"
  fi
  if [ "$DIRS" = "" ]; then
    echo No DIRS variable defined
    exit 1
  fi
  if [ "$LABEL" = "" ]; then
    echo No LABEL variable defined
    exit 1
  fi
  if [ "$ISOIMAGE" = "" ]; then
    echo No ISOIMAGE variable defined
    exit 1
  fi
  if [ "$PUBLISHER" = "" ]; then
    echo No PUBLISHER variable defined
    exit 1
  fi
  if [ "$VERBOSE" = "YES" ]; then
      echo mkisofs $bootable -r -J -V $LABEL -publisher "$PUBLISHER" -o $ISOIMAGE $DIRS
  fi
  mkisofs $bootable -r -J -V $LABEL -publisher "$PUBLISHER" -o $ISOIMAGE $DIRS
else             # no MKISO
  if [ "$BURN" = "" ]; then
    echo 'Warning: no action specified.  Select at least one of -i and -b.'
  fi
fi

if [ "$BURN" = "YES" ]; then
  if [ "$CDDEV" = "" ]; then
    echo No CDDEV variable defined
    exit 1
  fi
  if [ "$ISOIMAGE" = "" ]; then
    echo No ISOIMAGE variable defined
    exit 1
  fi
  if [ ! -f $ISOIMAGE ]; then
    echo No ISO image $ISOIMAGE found.
    exit 1
  fi
  if [ "$VERBOSE" = "YES" ]; then
      echo /usr/local/bin/growisofs -Z $CDDEV=$ISOIMAGE -use-the-force-luke=notray -use-the-force-luke=tty $SPEED
  fi
  /usr/local/bin/growisofs -Z $CDDEV=$ISOIMAGE -use-the-force-luke=notray -use-the-force-luke=tty $SPEED
fi
