#!/bin/bash
# vim:set sw=4 ts=4:
# $Id: make_inst_dvd,v 1.11 2005/04/11 14:14:34 fabian Exp $
#
#############################################################################
#
# ALICE
# Automatic Linux Installation and Configuration Environment
#
# Copyright (c) 2000-2002 SuSE Linux Solutions AG, Eschborn, Germany
#               2002-2004 SuSE Linux AG, Eschborn, Germany
#               2005           SUSE GmbH, Nuernberg, Germany
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#############################################################################
#
# Author: 
#
# Please have a look at  make_indt_dvd.HOWTO
#
##
sample_DOTmake_inst_dvd=$(cat <<EOF
# ----------- SNIP -----------------
# .make_inst_dvd
#
# iso9660 parameters
#
image_path=sles8.iso
boot_bin=boot/loader/isolinux.bin
boot_cat=boot/loader/bootcat.cat
#
## drive parameters
#
drive_speed=4
device_loc=2,0,0
device_path=/dev/sr2
#
## dist parameters
#
base_dir=/install/suse-devel/i386/SLES8/
sub_dirs="boot SuSE-SLES  UnitedLinux  UnitedLinux-1.0-Service-Pack  alice media.1 yast"
root_files="content driverupdate"
# ----------- SNAP -----------------
EOF
)
#
#
#
#
#
#
#
####### create a graft point list ###########################################
#
graftIt()
{
	typeset base=$1
	shift
	while [ $# -gt 0 ]; 
	do 
		printf "$1/=$base/$1 " 
		shift 
	done 
}
#
####### simplify the mkisofs output #########################################
#
minimkiso()
{
    #
    # strip the mkisofs output
    #
    awk '
	BEGIN      { barmode=0 }
	/^Using/   { next }
	$1 !~ /%$/ { if (barmode==1) printf "\n"
		print $0
		barmode=0
	}
	$1 ~  /%$/ {
		barmode=1
		gsub(".[0-9][0-9]%","",$1)
		percent_now=$1
		bar_width=40
		bar_done=(bar_width * percent_now) / 100
		for (i=1; i<=bar_width; i++) {
			if ( i <= bar_done ) printf "#"
			else printf "-"
		}
		printf " (%s)\r",$8
		system("usleep 20")
	}
	END        { if (barmode==1) printf "\n" }
        '
}
#
####### the main program ####################################################
#
system_names=""
only_create_iso_image=0
use_growisofs=0

while [[ $# -gt  0 ]]
do
	case "$1" in
		--show_sample )
			echo "$sample_DOTmake_inst_dvd"
			exit 1
			;;
		--only_create_iso_image ) # do NOT burn DVD after creation of the ISO
			# this option CAN NOT be combined with --use_growisofs
			only_create_iso_image=1
			;;
		--use_growisofs )
			use_growisofs=1
			;;
		* )
			system_names="${system_names} ${1}"
			;;
	esac
	shift
done

#
. ./.make_inst_dvd
#
##### check some options
#
if [[ ( ${only_create_iso_image} == 1 ) && ( ${use_growisofs} == 1 ) ]]
then
	echo "ERROR: You can NOT combine --only_create_iso_image and --use_growisofs"
fi
#
# internal values
#
mytag="make_inst_dvd"
#
# alice paths
#
if [[ -n "$ALICE_HOME" ]]
then
	alice_home="$ALICE_HOME"
else
	alice_home=$(pwd)/additions/alice
fi
alice_dir=/usr/lib/alice2 
local_dir=$(pwd)
#
####### create the AutoYaST XML files #######################################
#
mkdir -p $local_dir/additions/yast.cfg
echo "Calling alice2autoyast.pl"
cd $alice_dir
for SYS_NAME in $system_names
do
	#
	# MAKE XML FILEs
	# SL 8x
	#
	# TODO: use the correct path to alice2autoyast.pl
	#   at this moment we need the quick fix for $PATH (see above)
	# TODO: the xml files should be stored according to the
	#   ALICE tag SYS_AUTOYAST_URL
	#
	$alice_dir/utils/alice2autoyast.pl --host $SYS_NAME \
	    --alice_home=$alice_home \
	    --xml_file=$local_dir/additions/yast.cfg/$SYS_NAME.xml \
        || err_exit "can't create $SYS_NAME.xml"
done
cd -
#
########### create the iso image #############################################
#
# TODO: optionaly use growisofs to do the mkisof AND dvdrecord stuff
#
logger -s "now start the mkisofs"
grafts=$(graftIt "$base_dir" $sub_dirs )
grafts="$grafts $(graftIt "." additions)"
    #
    # create sort file, so catalog and bin are placed to the
    # beginning of the CD (needed for some BIOS versions)
    #
    SORTFILE=/tmp/$$.SORT
    touch $SORTFILE
    (
            echo "./4iso/boot/loader/boot.cat 3"
            find ./4iso/boot/loader -printf "%p 1\n"
            # last priority wins
            echo "./4iso/boot/loader/$iso_linux_bin 2"
    ) > $SORTFILE
cat $SORTFILE
    #
    #
    #
rm -rf 4iso
mkdir 4iso
cp -LpR "$base_dir/boot" ./4iso
for f in $root_files
do
	cp -d $base_dir/$f ./4iso
done
cp $local_dir/additions/yast.cfg/$SYS_NAME.xml ./4iso/info
cp $local_dir/additions/yast.cfg/$SYS_NAME.xml ./4iso/boot/loader/info

#
## TODO: create an entry for isolinux.bin
#		 for now, we only create ONE entry, but we need one entry PER SYSTEM
#		
isolinux_cfg=./4iso/boot/loader/isolinux.cfg

(
	cat <<- EOF
	default harddisk
	
	# hard disk
	label harddisk
	  kernel linux
	  append SLX=0x202

	# autoinst
	label autoinst
		kernel linux
		append initrd=initrd ramdisk_size=65536 autoyast=device://hdc/info install=device://hdc/
	
	# manual
	label manual
	  kernel linux
	  append initrd=initrd ramdisk_size=65536 manual=1

	# rescue
	label rescue
	  kernel linux
	  append initrd=initrd ramdisk_size=65536 rescue=1

	# memory test
	label memtest
	  kernel memtest

	implicit        0
	gfxboot         bootlogo
	display         message
	prompt          1
	timeout         100
	readinfo        2
	framebuffer     1
	EOF
#	cat "${isolinux_cfg}"
) > "${isolinux_cfg}".$$
mv "${isolinux_cfg}".$$ "${isolinux_cfg}"
#
####### now 'burn' ithe iso on a DVD-R #######################################
#
if [[ "${use_growisofs}" == 0 ]]
then
	mkisofdcmd="mkisofs -o $image_path"
else
	if [[ -z "${device_path}" ]]
	then
		echo "ERROR: You have to specify device_path=/dev/srX"
		echo "in .make_inst_dvd"
	fi
	mkisofdcmd="growisofs -Z $device_path"
fi
$mkisofdcmd \
	-JR -no-emul-boot -boot-info-table \
	-b $boot_bin -c $boot_cat -boot-load-size 4 \
	-sort $SORTFILE \
	-graft-points \
	$grafts \
	4iso \
	 2>&1 | minimkiso
if [ $? -ne 0 ]
then
	logger -t "$mytag" -s "mkisofs failed"
	exit 1
fi
#
#
if [[ "$only_create_iso_image" == 1 ]]
then
	echo "ISO Image is ready to use now: $image_path"
	echo "Skip burning the DVD (used option --only_create_iso_image)"
	echo "EXIT (OK)"
	exit 0
fi
#
####### now 'burn' ithe iso on a DVD-R #######################################
#
if [[ "${use_growisofs}" == 0 ]]
then
	logger -s "now start the dvdrecord"
	if which dvdrecord
	then
		cdrecord_cmd="dvdrecord"
	else
		if which cdrecord
		then
			cdrecord_cmd="cdrecord"
		fi
	fi
	$cdrecord_cmd -v dev=$device_loc speed=$drive_speed $image_path
	if [ $? -ne 0 ]
	then
		logger -t "$mytag" -s "$cdrecord_cmd failed"
		exit 1
	fi
fi
logger -s "finished"
#
####### the end ##############################################################
#
