#! /bin/bash
# this script read/writes the pxelinux config files for the nodes/hosts to be installed with Ka
# the file $template must be edited
# many fancy message displays in this script because it can be pretty slow (DNS calls)
. /etc/ka/ka.conf
. /etc/ka/ka.funcs

rundir="$pxelinux_dir"
# we no longer use pxelinux's gethostip. gethostip="bin/gethostip -x"
gethostip="ip_to_hex"
template="pxelinux.cfg/template"


ip_to_hex()
{
	local ip=$1
	ip=${ip//\./ }
	printf "%02X%02X%02X%02X\n" $ip
}


usage()
{
cat << EOF
Usage: 
Set/Get machine step for operation with Ka
$0 -g machines_list
$0 -s -t step_name machines_list
	-g, --get : read machine step
	-s, --set : set machine step
	-t : specify step name
	-l : list available steps
	
machines_list can be under this form:
-m machine1 -m machine2 -m machine3 
or, if you are in a cluster:
-r 1 11 -n 13 -r 15 20
(nodes 1 to 11, node 13, node 15 to 20)	
EOF
}

bad_range()
{
	echo Machine numbers must be between $first_node and $last_node
}


add_range()
{
	local err
	# check machine numbers
	test $first_node -le $1 > /dev/null 2>&1 
	err=$?
	if [ $err -eq 2 ]; then usage; exit 1; fi
	if [ $err -ne 0 ]; then bad_range; exit 1; fi
	test $last_node -ge $2 > /dev/null 2>&1 
	err=$?
	if [ $err -eq 2 ]; then usage; exit 1; fi
	if [ $err -ne 0 ]; then bad_range; exit 1; fi
	
	local list=`get_ip_list_range $1 $2`
	ip_list="$ip_list $list"
}


add_host()
{
	local machine=$1
	local ip=`get_ip $machine`
	if [ $? -ne 0 ];then 
		echo Machine $machine not found
	else
		ip_list="$ip_list $ip"
	fi
}

cd "$rundir" 

task=usage

while test $# -gt 0 ; do
	case "$1" in
		-h | --help | --h* )
			 usage; exit 0 ;;
		-s | --set )
			task=set
			shift
			;;
		-g | --get )
			task=get
			shift
			;;
		-l | --list )
			task=list
			shift
			;;
		-t | --step )
			stepname="$2"
			shift; shift
			;;
		-r | --range )
			shift
			first=$1
			last=$2
			/bin/echo -n $'\r'		
			/bin/echo -n "Getting IP list for range $first $last                            "
			add_range $first $last
			shift; shift
			;;
		-n | --machinenum )
			/bin/echo -n $'\r'		
			/bin/echo -n "Getting IP for machine num $2                                     "
			add_range $2 $2
			shift; shift
			;;
		-m | --machine )
			/bin/echo -n $'\r'		
			/bin/echo -n "Getting IP for machine $2                                         "
			add_host $2
			shift; shift
			;;
		* )
			usage; exit 1 ;;
	esac
done
/bin/echo -n $'\r'"                                                                                     "
/bin/echo -n $'\r'


case $task in
	list)
	echo "Reading $template..."
	echo "Possible steps:"
	cat "$template" | grep ^LABEL | cut -d ' ' -f 2
	;;

	set)
	echo "Reading $template..."
	possible_steps=`cat "$template" | grep ^LABEL | cut -d ' ' -f 2`
	if ! echo "$possible_steps" | grep -qs "^$stepname\$"; then
		echo "$stepname is not a valid step"
		echo "Choices are  : "
		for i in $possible_steps;do
			echo " -> " $i
		done
		exit 1
	fi
	
	echo Writing files
	for ip in $ip_list ; do
		/bin/echo -n $'\r'		
		fich=pxelinux.cfg/`$gethostip $ip`
		/bin/echo -n $ip : $fich
		echo DEFAULT $stepname > $fich
		cat "$template" >> $fich
		chmod 666 $fich
		fich=`basename $fich`
		ln -fs ../$fich pxelinux.cfg/IP/$ip
	done
	echo
	;;

	get)
	for ip in $ip_list ; do
		/bin/echo -n "$ip : "
		fich=pxelinux.cfg/`$gethostip $ip`
		grep DEFAULT $fich | cut -d ' ' -f 2-
	done
	;;
	
	*)
	usage
	exit 1
esac
exit 0
