#!/bin/ksh -p
#
# ident "@(#)utdhcpopt.ksh	1.8 04/08/20 SMI"
#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

TMPDELETEFILE=/var/opt/SUNWut/tmp/$$.delete

ProcessAdd() {
	BEGAN=false

	while read input
	do
		case $input in
		(begin*)
			COMMAND=${input#begin }
			if [[ $COMMAND = "options" ]]; then
				BEGAN=true
				if [[ -f $OPTIONSFILE ]]; then
                			print -u2 "DHCP options configuration already exists."
                			exit 1
        			fi
			elif [[ $COMMAND = "ether" ]]; then
				BEGAN=true
			else
                            	print -u2 "Error: Invalid block type.  Please try again."
				exit 1
			fi
                        ;;
		(end)
			break
			;;
		(macaddress*)
			if $BEGAN; then
				ConvertKeyToLower "$input"
				input="$_RETURN_VAL"
				ETHER=${input#macaddress=}
				normalizeEther
				UT_DHCP_MACADDRESS=$ETHER
				ETHERFILE=${ETHERFILEPATHPREFIX}${UT_DHCP_MACADDRESS}
                                if [[ -f $ETHERFILE ]]; then
                                        print -u2 "DHCP ether configuration already exists."
                                        exit 1
                                fi
			else
				print -u2 "Error: Input must start with a \"begin\" statement"
				exit 1
			fi
			;;
		(*)
			if ! $BEGAN ; then
				print -u2 "Error: Input must start with a \"begin\" statement"
				exit 1
			else
				ConvertKeyToLower "$input"
                        	input="$_RETURN_VAL"
				SourceOptions "$input"
			fi
			;;
		esac
	done
	if [[ $COMMAND = "options" ]]; then
		ValidateOptionsBlock
		if [[ $? != 0 ]]; then
			print -u2 "Error: Invalid options block"
			exit 1
		else
			GenerateOptionsDHCPDConf >> $OPTIONSFILE		
			if $CHROOTED; then
				ETCOPTIONSFILE="/etc/$OPTIONSFILENAME"
				cp $OPTIONSFILE $ETCOPTIONSFILE 2>/dev/null
				OPTINSERT=$ETCOPTIONSFILE
			else
				OPTINSERT=$OPTIONSFILE
			fi
			ls $DHCPDCONF | xargs grep $OPTINSERT >/dev/null
                	if [[ $? != 0 ]]; then
                        	InsertOptionsInclude $OPTINSERT
                	fi
		fi
	elif [[ $COMMAND = "ether" ]]; then
		ValidateEtherBlock
		if [[ $? != 0 ]]; then
       			print -u2 "Error: Invalid ether block"
			exit 1
		else
			GenerateEtherDHCPDConf >> $ETHERFILE
			if $CHROOTED; then
				ETCETHERFILE="/etc/$ETHERFILEPREFIX$ETHER"
				cp $ETHERFILE $ETCETHERFILE 2>/dev/null
				ETHERINSERT=$ETCETHERFILE
			else
				ETHERINSERT=$ETHERFILE
			fi
			ls $DHCPDCONF | xargs grep $ETHERINSERT >/dev/null
			if [[ $? != 0 ]]; then
				InsertGenericInclude $ETHERINSERT
			fi	
		fi
        fi
	print "Configuration added."
	exit 0
}

GenerateEtherDHCPDConf() {
	AddEtherColon
	print "subclass \"SunRayEther\" 1:$ETHERCOLON {"
	print "	vendor-option-space NewT;"
	GenerateOptionsDHCPDBlock	
	print "}"
}

GenerateOptionsDHCPDConf() {
cat <<!
option space NewT;
option NewT.AuthSrvr code 21 = ip-address;
option NewT.AuthPort code 22 = unsigned integer 16;
option NewT.NewTVer  code 23 = string;
option NewT.LogHost  code 24 = ip-address;
option NewT.LogKern  code 25 = unsigned integer 8;
option NewT.LogNet   code 26 = unsigned integer 8;
option NewT.LogUSB   code 27 = unsigned integer 8;
option NewT.LogVid   code 28 = unsigned integer 8;
option NewT.LogAppl  code 29 = unsigned integer 8;
option NewT.NewTBW   code 30 = unsigned integer 32;
option NewT.FWSrvr   code 31 = ip-address;
option NewT.NewTDispIndx   code 32 = unsigned integer 32;
option NewT.Intf     code 33 = string;
option NewT.NewTFlags      code 34 = unsigned integer 32;
option NewT.AltAuth  code 35 = array of ip-address;
option NewT.BarrierLevel   code 36 = unsigned integer 32;

class "SunRayEther" {
	match option dhcp-client-identifier;
}
        
class "SunRay" {
	match option vendor-class-identifier;
}

subclass "SunRay" "SUNW.NewT.SUNW" {
	vendor-option-space NewT;
!
	GenerateOptionsDHCPDBlock
	print "}"
}

ProcessDelete() {
	BEGAN=false

	while read input
	do
       		case $input in
		(begin*)
			COMMAND=${input#begin }
                        if [[ $COMMAND = "options" ]]; then
                                BEGAN=true
                                if [[ ! -f $OPTIONSFILE ]]; then
                                        print -u2 "Configuration for DHCP options does not exist."
                                        exit 1
                                fi
                        elif [[ $COMMAND = "ether" ]]; then
                                BEGAN=true
                        else
                                print -u2 "Error: Invalid block type.  Please try again."
                                exit 1
                        fi
                        ;;
		(macaddress*)
                        if $BEGAN; then
                                ConvertKeyToLower "$input"
                                input="$_RETURN_VAL"
                                ETHER=${input#macaddress=}
                                normalizeEther
                                UT_DHCP_MACADDRESS=$ETHER
                                ETHERFILE=${ETHERFILEPATHPREFIX}${UT_DHCP_MACADDRESS}
                                if [[ ! -f $ETHERFILE ]]; then
                                        print -u2 "DHCP ether configuration $UT_DHCP_MACADDRESS does not exist."
                                        exit 1
                                fi
                        else
                                print -u2 "Error: Input must start with a \"begin\" statement"
                                exit 1
                        fi
                        ;;
		(end)
                        if $BEGAN; then
				if [[ $COMMAND = "options" ]]; then
					DeleteGenericInclude "SunRay-options"
					rm -f $OPTIONSFILE
					if $CHROOTED; then
						rm -f "/etc/$OPTIONSFILENAME"
					fi
				elif [[ $COMMAND = "ether" ]]; then
					DeleteGenericInclude "SunRay-ether-$ETHER"
					rm -f $ETHERFILE
					if $CHROOTED; then
						rm -f "/etc/$ETHERFILEPREFIX$ETHER"
					fi
				fi
                        else
                                return 0
                        fi
			break
				;;
		(*)
                        if $BEGAN; then
                                print -u2 "Error: Invalid argument."
				exit 1
                        else
                                print -u2 "Error: Must specify a valid block."
				exit 1
                        fi
					;;
		esac
	done
	print "Configuration deleted."
}

ProcessChange() {
	BEGAN=false

	TranslateDHCPDConf
        InitDHCPBlockParser $UTDHCPFILE
        while read input
        do
                case $input in
		(begin*)
                        COMMAND=${input#begin }
                        if [[ $COMMAND = "options" ]]; then
                                BEGAN=true
                                if [[ ! -f $OPTIONSFILE ]]; then
                                        print -u2 "Configuration for DHCP options does not exist."
                                        exit 1
                                fi
				GetNextDHCPBlock options
                        elif [[ $COMMAND = "ether" ]]; then
                                BEGAN=true
                        else
                                print -u2 "Error: Invalid block type.  Please try again."
                                exit 1
                        fi
                        ;;
                (macaddress*)
                        if $BEGAN; then
                                ConvertKeyToLower "$input"
                                input="$_RETURN_VAL" 
                                ETHER=${input#macaddress=}
                                normalizeEther
                                ETHERFILE=${ETHERFILEPATHPREFIX}${ETHER}
                                if [[ ! -f $ETHERFILE ]]; then
                                        print -u2 "DHCP ether configuration $UT_DHCP_MACADDRESS does not exist."
                                        exit 1
                                fi
				while GetNextDHCPBlock ether
                                do
                                        if [[ $UT_DHCP_MACADDRESS = $ETHER ]]; then
                                                break
                                        fi
                                done
                        else
                                print -u2 "Error: Input must start with a \"begin\" statement"
                                exit 1
                        fi
                        ;;
                (end)
                        break
                        ;;
                (*)
                        if ! $BEGAN ; then
                                print -u2 "Error: Input must start with a \"begin\" statement"
				exit 1
			else
				ConvertKeyToLower "$input"
                        	input="$_RETURN_VAL"
                        	SourceOptions "$input" 
			fi
                        ;;
                esac
        done
	DestroyDHCPBlockParser
	rm -f $UTDHCPFILE 2>/dev/null
	if [[ $COMMAND = "options" ]]; then
		ValidateOptionsBlock
        	if [[ $? != 0 ]]; then
                	print -u2 "Error: Invalid options block"
                	exit 1
        	else
			GenerateOptionsDHCPDConf >> $OPTIONSFILE.$$
			rm -f $OPTIONSFILE 2>/dev/null
			mv $OPTIONSFILE.$$ $OPTIONSFILE
			if $CHROOTED; then
                                ETCOPTIONSFILE="/etc/$OPTIONSFILENAME"
                                cp $OPTIONSFILE $ETCOPTIONSFILE 2>/dev/null
			fi
        	fi
	elif [[ $COMMAND = "ether" ]]; then
		ValidateEtherBlock
		if [[ $? != 0 ]]; then
			print -u2 "Error: Invalid ether block"
                        exit 1 
		else
			GenerateEtherDHCPDConf >> $ETHERFILE.$$
			rm -f $ETHERFILE 2>/dev/null
			mv $ETHERFILE.$$ $ETHERFILE
			if $CHROOTED; then
                                ETCETHERFILE="/etc/$ETHERFILEPREFIX$ETHER"
                                cp $ETHERFILE $ETCETHERFILE 2>/dev/null
			fi
		fi
	fi
        print "Configuration changed."
        exit 0
}

ListEtherBlocks() {
	TranslateDHCPDConf
        if [[ $? != 0 ]]; then
                return 0
        fi
        InitDHCPBlockParser $UTDHCPFILE
        if [[ $? != 0 ]]; then
                return 0
        fi
        while GetNextDHCPBlock ether
        do
                CreateDHCPBlock ether
        done
        DestroyDHCPBlockParser
        rm -f $UTDHCPFILE 2>/dev/null
}

ListOptionsBlocks() {
	TranslateDHCPDConf
	if [[ $? != 0 ]]; then
		return 0
	fi
	InitDHCPBlockParser $UTDHCPFILE
	if [[ $? != 0 ]]; then
                return 0
        fi
	while GetNextDHCPBlock options
        do
                CreateDHCPBlock options
	done	
	DestroyDHCPBlockParser
	rm -f $UTDHCPFILE 2>/dev/null
}

# main

ETC_OPT_UT=/etc/opt/SUNWut
UT_DHCP_BASEDIR=`(cd ${ETC_OPT_UT}/dhcp ; /bin/pwd)`
. ${UT_DHCP_BASEDIR}/../../support_lib/dhcp_config
. ${UT_DHCP_BASEDIR}/dhcp_config_linux

CheckChrooted

if [[ $# = 0 ]]; then
	ListOptionsBlocks
	ListEtherBlocks
	exit 0
fi	

case $1 in
(add)
	ProcessAdd 
	;;
(change)
	ProcessChange 
	;;
(delete)
	ProcessDelete 
	;;
(list)
	if [[ -z $2 ]]; then
		ListOptionsBlocks
		ListEtherBlocks
		exit 0
	fi
	if [[ $2 = "options" ]]; then
		ListOptionsBlocks
		exit 0
	elif [[ $2 = "ether" ]]; then
		ListEtherBlocks
		exit 0
	else
		print -u2 'Must specify either options or ether.'
		exit 1
	fi
	;;
(*)
	exit 1
esac
