#!/bin/sh
# vim:set sw=4 ts=4:
# $Id: make_grub_disk,v 1.3 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: 
#
# ALICE
# Automatic Linux Installation and Configuration Environment
#
#############################################################################
#
# Copyright (c) 2000 SuSE Linux Solutions AG, Eschborn, Germany
# All rights reserved.
#
# This file is part of ALICE, which was sponsored by
# T-Online International AG
#
# Author: Fabian Herschel
#
#############################################################################
#
# make_grub_disk
#   creates a network grub menu (master menu)
#
#############################################################################
#
# Options
# START HELP
# make_grub_disk
#   --dhcp                  # default; netgrub will start dhcp
#   --server  SERVERIP      # the server to conenct to
#   --ipaddr  IPADDR        # exclusive to --dhcp; the clients IP Addr, also use --netmask 
#   --menu    PATH          # The menu file path on the server (relative to tftp root)
#   --netmask MASK          # exclusive to --dhcp; the clients netmask, also use --ipaddr 
#   --gateway IPADDR        # default gateway
#   --dest    DEVICE|PATH   # Where to write the image
#   --src     DEVICE|PATH   # Where to get a image template
#   --help                  # This help
# STOP HELP
#   
#############################################################################
#
opt_dhcp=1
opt_ipaddr=""
opt_netmask=""
opt_gateway=""
opt_srv=""
opt_menu="/grub/menu.lst"
opt_dest=""
opt_src=""
opt_dest_device=0
opt_src_device=0
opt_only_exchange_menu=1
echo "make_grub_disk 0.1.0"
#
while [ $# -gt 0 ]
do
	case "$1" in
		--dhcp ) #
				opt_dhcp=1;
				;;
		--server ) #
				opt_srv=$2;
				shift
				;;
		--ipaddr ) #
				opt_ipaddr=$2;
				opt_dhcp=0;
				shift
				;;
		--menu ) #
				opt_menu=$2;
				shift
				;;
		--gateway ) #
				opt_gateway=$2
				shift
				;;
		--netmask ) #
				opt_netmask=$2
				shift
				;;
		--src ) #
				opt_src=$2
				opt_only_exchange_menu=0
				shift
				;;
		--dest ) #
				opt_dest=$2
				shift
				;;
	        --help )
				awk '/START HELP/,/STOP HELP/' $0 | egrep -v "START HELP|STOP HELP"
				exit
		^		;;
		* )      #
				echo "Do not know option $1" >&2
				exit
				;;
	esac
	shift
done
#
#### CHECK SOME PARAMS
#
if [ "$opt_src" = "$opt_dest" ]
then
	opt_only_exchange_menu=1	
fi
echo "$opt_src" | grep -q "^/dev/" && opt_src_devive=1
echo "$opt_dest" | grep -q "^/dev/" && opt_dest_device=1
#
##### CREATE GRUB MENU
#
echo "creating grub menu"
(
	echo "color white/blue black/light-gray"
	echo "default 0"
	echo "timeout 1"
	echo "title SUSE grub network disk"
	if [ $opt_dhcp -eq 1 ] 
	then
		echo "dhcp"
	fi
	if [ -n "$opt_srv" ]
	then
		ifcfg_parms="$ifcfg_parms --server=$opt_srv"
	fi
	if [ -n "$opt_ipaddr" ]
	then
		ifcfg_parms="$ifcfg_parms --address=$opt_ipaddr"
	fi
	if [ -n "$opt_gateway" ]
	then
		ifcfg_parms="$ifcfg_parms --gateway=$opt_gateway"
	fi
	if [ -n "$opt_netmask" ]
	then
		ifcfg_parms="$ifcfg_parms --mask=$opt_netmask"
	fi
	echo "ifconfig $ifcfg_parms"
	if [ -n "$opt_menu" ]
	then
		echo "configfile (nd)$opt_menu"
	fi
) > m$$.lst
#
##### ACQUIRE GRUB TEMPLATE IMAGE 
#
# skip if opt_only_exchange_menu is set
#
if [ "$opt_only_exchange_menu" -eq 0 ]
then
	echo "loading grub image from source ($opt_src)"
	if [ "$opt_src_device" -eq 1 ]
	then
		dd if="$opt_src" of=grub_$$.img bs=1440k
	else
		# TODO: How to handle a directory tree instead of a floppy immage?
		dd if="$opt_src" of=grub_$$.img bs=1440k
	fi
fi
#
##### PLACE MENU ON NEW GRUB FLOPPY IMAGE
#
if [ -n "$opt_dest" -o -n "$opt_src" ]
then
	if [ -z "$opt_dest" ]
	then
		opt_dest=grub_$$.img
	fi
	if [ "$opt_only_exchange_menu" -eq 1 ]
	then
		#
		# no source was specifed, but a destibation to place the menu
		#
		echo "copying the grub menu to ($opt_dest)/boot/grub/menu.lst"
		mkdir -p /tmp/m$$
		if [ $opt_dest_device -eq 1 ]
		then
			mount -oloop $opt_dest /tmp/m$$
		else
			mount $opt_dest /tmp/m$$
		fi
		mv m$$.lst /tmp/m$$/boot/grub/menu.lst
		umount /tmp/m$$
		rmdir /tmp/m$$
	else
		#
		# a source, but no dest was specified
		#
		echo "copying the grub menu to (grub_$$.img)/boot/grub/menu.lst"
		mkdir -p /tmp/m$$
		mount -oloop grub_$$.img /tmp/m$$
        mv m$$.lst /tmp/m$$/boot/grub/menu.lst
        umount /tmp/m$$
        rmdir /tmp/m$$
	fi
else
	# neither a source not a destination was specified, so only create the menu
	echo "The new grub menu is: $(pwd)/m$$.lst"
fi
#
##### COPY NEW GRUB IMAGE TO THE DEST
# 
# skip if opt_only_exchange_menu is set
if [ "$opt_only_exchange_menu" -eq 0 ]
then
	if [ -z "$opt_dest" -o "$opt_dest" = "grub_$$.img" ]
	then
		echo "The grub floppy image is: $(pwd)/grub_$$.img"
	else
		echo "copying the grub image to the destination ($opt_dest)"
		dd of="$opt_dest" if=grub_$$.img bs=1440k
		rm grub_$$.img
	fi
fi
