#!/usr/local/bin/bash
#
# Make and burn a CD-ROM image
# Greg Lehey, 26 June 2005
#
# $Id: mkcd,v 1.7 2010/03/20 21:57:50 root 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 ()
  {
  echo ' Usage:'
  echo ''
  echo ' mkcd [-i] [-b] [-c cddev] [-s speed] [-I imagename] [-p projectname] [-v] [-n] dirs'
  echo ''
  echo ' -b specifies to burn the ISO image to disk.'
  echo ' -c specifies the CD device name.'
  echo ' -i specifies to build the ISO image.'
  echo ' -I is the name of the ISO image to build.  Normally this is'
  echo '    specified in the project file, which always overrides the command'
  echo '    line variable.'
  echo ' -n specifies to print the commands, but not to perform them'
  echo ' -s specifies speed of burn (not needed for DVD)'
  echo ' -v specifies verbose output from the script.'
  echo ' -p specifies the name of a file that supplies the following variables:'
  echo ''
  echo ' PUBLISHER	The name of the publisher.  By default, the GECOS name of the user.'
  echo ' BOOTFILE      The name of the bootstrap.  If this variable is missing or'
  echo '               null, the ISO image will not be bootable.'
  echo ' BOOTCAT	For Linux at any rate, a boot catalogue.'
  echo ' LABEL		Label for ISO images, deafult ISOIMAGE.'
  echo ' DIRS		A list of directory names, in addition to those on the command line.'
  echo ' ISOIMAGE	Name of ISO image, default ISOIMAGE in current directory.'
  echo ' CDDEV		Device name of the CD burner'
  echo ' TABLENAME     Name of translation table, by default .MAP'
  echo ''
  echo ' whatever standards this architecture supports (may be unsupported),'
  echo ' LABEL is the ISO image label, ISOIMAGE is the filename of the'
  echo ' resulting ISO image, base-bits-dir contains the image contents and'
  echo ' extra-bits-dir, if provided, contains additional files to be merged'
  echo ' into base-bits-dir as part of making the image.'
}

# defaults
PUBLISHER=`who am i`
PUBLISHER=${PUBLISHER/ */}
PUBLISHER=`finger $PUBLISHER | head -1`
PUBLISHER=${PUBLISHER/*Name: /}
if [ "$PUBLISHER" = "" ]; then   # in case GECOS is stuffed
  PUBLISHER=anonymous
fi
echo $PUBLISHER
ISOIMAGE=ISOIMAGE
LABEL=ISOIMAGE
# CD burner.  This must be a SCSI device.
CDDEV=/dev/cd0

# local variables
MKISO=
BURN=
SPEED=
VERBOSE=
# Default name of translation table, (mkisofs's default is TRANS.TBL)
TABLENAME=.MAP
bootable=
DRYRUN=

while [ "$#" -gt 0 ]; do
  found=NIL
  if [ "$1" = "-b" ]; then
  	BURN="YES"
	found=T
  	shift
  fi

  if [ "$1" = "-c" ]; then
  	CDDEV=$2
	found=T
  	shift 2
  fi

  if [ "$1" = "-i" ]; then
  	MKISO="YES"
	found=T
  	shift
  fi

  if [ "$1" = "-I" ]; then
  	ISOIMAGE=$2
	found=T
  	shift 2
  fi

  if [ "$1" = "-n" ]; then
        DRYRUN=YES
	VERBOSE=YES
	found=T
        shift
  fi

  if [ "$1" = "-s" ]; then
  	SPEED="-speed=$2"
	found=T
  	shift 2
  fi

  if [ "$1" = "-v" ]; then
  	VERBOSE="YES"
	found=T
  	shift
  fi
  if [ "$1" = "-V" ]; then
  	VERIFY=YES
	found=T
  	shift
  fi

  if [ "$1" = "-p" ]; then
     PROJFILE=$2
	found=T
     shift 2
  fi
  if [ "$found" = NIL ]; then
    CDIRS="$CDIRS $1"		# must be a directory
    shift
  fi
done

if [ "$PROJFILE" != "" ]; then
     source $PROJFILE
fi

# DIRS may be set from PROJFILE.  Add in command line dirs
DIRS="$DIRS $CDIRS"

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:	" $PROJFILE
    echo "PUBLISHER:	" $PUBLISHER
    echo "LABEL:		" $LABEL
    echo "BOOTFILE:	" $BOOTFILE
    echo "BOOTCAT:	" $BOOTCAT
    echo "DIRS:		" $DIRS
    echo "ISOIMAGE:	" $ISOIMAGE
    echo "CDDEV:		" $CDDEV
    echo "VERIFY:		" $VERIFY
    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
     echo Make ISO:
     echo "  " mkisofs -f $bootable -T -table-name $TABLENAME -r -J -V $LABEL -publisher "$PUBLISHER" -o $ISOIMAGE $DIRS
  fi
  if [ "$DRYRUN" != YES ]; then
    mkisofs -f $bootable -T -table-name $TABLENAME -r -J -V $LABEL -publisher "$PUBLISHER" -o $ISOIMAGE $DIRS
  fi
else             # no MKISO
  if [ "$BURN" = "" ]; then
    echo 'Warning: no action specified.  Select at least one of -i and -b.'
    echo
    usage
  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
     echo Burn:
     echo "  " /usr/local/bin/growisofs -Z $CDDEV=$ISOIMAGE -use-the-force-luke=notray -use-the-force-luke=tty $SPEED
  fi
  if [ "$DRYRUN" != YES ]; then
    /usr/local/bin/growisofs -Z $CDDEV=$ISOIMAGE -use-the-force-luke=notray -use-the-force-luke=tty $SPEED
  fi
fi
if [ "$VERIFY" = YES ]; then
  if [ "$VERBOSE" = "YES" ]; then
     echo
     echo Verify:
     echo "  " dd if=$CDDEV of=/dev/null bs=128k conv=sync
  fi
  if [ "$DRYRUN" = YES ]; then
    exit 0
  fi
  dd if=$CDDEV of=/dev/null bs=128k conv=sync
  if [ $? != 0 ]; then
    echo '*** FAILED TO VERIFY' $CDDEV '***'
    exit 1
  fi
fi