#! /bin/sh

# This script will check if the UDHCP_OPTARGS_VENDOR_CLASS
# <UDHCP_OPTARGS_RELEASE> field is not set, fetch the release info value and
# exchange "UDHCP_OPTARGS_RELEASE" with the firmware release info

local conff=/etc/conf.d/udhcpc.conf vendor_class=
local relvars=/usr/share/axis-release/variables
local block_parameters= prod_nbr=
local count=0 vc= oldIFS= pp= ppp=

[ ! -r $conff ] || . $conff

if [ $UDHCP_RUNTIME_CHECK = no ]; then
	block_parameters=$(bootblocktool -l)

	for pp in $block_parameters; do
		ppp=${pp#*"root.Brand.ProdNbr="}
		[ "${#ppp}" = "${#pp}" ] || prod_nbr=$ppp
	done

	if [ "$prod_nbr" ]; then
		oldIFS="$IFS"
		IFS=,
		for p in $UDHCP_OPTARGS_VENDOR_CLASS; do
			if [ $count -eq 2 ]; then
				vc="$vc,$prod_nbr"
			else
				if [ $count -eq 0 ]; then
					vc=$p
				else
					vc="$vc,$p"
				fi
			fi
			count=$(($count + 1))
		done
		IFS=$oldIFS

		sed -i -e "s/$UDHCP_OPTARGS_VENDOR_CLASS/$vc/" $conff
	fi

	vendor_class=${UDHCP_OPTARGS_VENDOR_CLASS##*,}

	if [ "$vendor_class" = UDHCP_OPTARGS_RELEASE ]; then
		[ ! -r $relvars ] || . $relvars
		sed -i -e "s/,UDHCP_OPTARGS_RELEASE/,$RELEASE/" $conff
	fi

	sed -i -re 's/^(UDHCP_RUNTIME_CHECK=).*/\1yes/' $conff
fi
