#
# vim:set sw=4 ts=4:
# $Id: boot_switch,v 1.4 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: 
#
########################################################################
#
# (c) 2003 SuSE Linux AG
# GPL
#
########################################################################
#
# Sets the boot mode of pxe clients. The disk mode is realized by
# removing the pxelinux link for that host.
# Please note that you need different filename entries in the
# config of the dhcp server for each node.
#
# The filename entry must be /tftpboot/pxe/pxelinux.192.168.1.30
# for example.
#

red="\033[31m"
norm="\033[m\017"
green="\033[32m"
blue="\033[0,34m"


DIR="/srv/tftp"
if [[ -r /etc/sysconfig/alice ]]
then
	. /etc/sysconfig/alice
	if [[ -n "$ALICE_TFTP_ROOT" ]]
	then
		DIR="$ALICE_TFTP_ROOT"
	fi
fi
ip=""

arch=i386

while [ $# -gt 0 ]
do
	case $1 in
		--arch )
			 arch=$2
			 shift
			;;
		--create )
			 action=create
				;;
		--disk ) 
			 action=disk
				;;
		--install )
			 action=install
				;;
		--show )
			 action=show
				;;
		[1-9]* )
			 ip=$1
	esac
	shift
done
DIR="$DIR/$arch/pxe"

#
########################################################################
#

test_mode()
{
   # params: a list of pxe file links
   # link name should be: pxelinux.IP[.moved]

	while [ $# -gt 0 ]
	do
		if [ "$1" = pxelinux.0 ]; then shift; continue; fi
	   # cut if the leading "pxelinux"
		F=${1#pxelinux.}
		I=${F%.moved}
		if [ "$F" = "$I" ]
		then
#		     printf "Boot switch is set to ${red}INSTALL${norm} for ip $I\n"	
			 printf "%-15s ${red}INSTALL${norm}\n" $I
		else
#		     printf "Boot switch is set to ${green}DISK${norm}    for ip $I\n"	
			 printf "%-15s ${green}DISK${norm}\n" $I
		fi
		shift
	done
}
	
#
########################################################################
#

case $action in
	disk )
		mv $DIR/pxelinux.$ip $DIR/pxelinux.$ip.moved
		echo "Boot mode switched to disk"
		;;
	install )
		mv $DIR/pxelinux.$ip.moved $DIR/pxelinux.$ip
		echo "Boot mode switched to install"
		;;
	show )
		cd $DIR
		if [ -z "$ip" ]
		then
			ip="[1-9]*"
			echo "List of bootmodes"
		fi
		test_mode $(ls pxelinux.$ip pxelinux.$ip.moved | sort -u)
		;;
	create )
		if [ ! -f $DIR/pxelinux.$ip -a ! -f $DIR/pxelinux.$ip.moved ]
		then
			cd $DIR
			ln -s pxelinux.0 pxelinux.$ip.moved
			echo "Boot mode created. Boot mode switched to disk."
			net_addr_hex=$(echo $ip | awk -F\. '{ printf "%02X%02X%02X%02X\n",$1,$2,$3,$4}')
			echo "Please also edit $DIR/pxelinux.cfg/$net_addr_hex"
		fi
		;;
esac
