#!/bin/bash

option=$1
shift
value="$*"
if [ -n "$value" ]
then
	case $option in
	"tftp-server-name")
		if [ "$value" != "${value//\"}" ]
		then
			# A string value maybe ASCII formated
			temp=${value//\"}
			size=${#temp}
			if [ "$size" -ne 4 ]
			then
				value=$temp
			else
				# We most likely have been given ASCII formatted ip address
				value=$(printf %d "'${temp:0:1}")
				for ((idx=1; idx<$size; idx++))
				do
					value="${value}.$(printf %d "'${temp:$idx:1}")"
				done
			fi
		else
			# Possibly a HEX string
			temp_lc="$LC_ALL"
			LC_ALL=C
			if [ -z "${value//[0-9a-fA-F:]}" ]
			then
				temp_ifs="$IFS"
				IFS=:
				temp=($value)
				IFS="$temp_ifs"
				value="$((16#${temp[0]}))"
				for ((idx=1; idx<${#temp[*]}; idx++))
				do
					value="${value}.$((16#${temp[$idx]}))"
				done
				[ $idx -ne 4 ] && logger -p user.warning -t "$0[$$]" "Invalid $option option value \"$*\""
			fi
			LC_ALL="$temp_lc"
		fi
		;;
	"vlan-id"|"vlan-qos")
		temp="${value//\"}"
		size=${#temp}
		if [ -n "$temp" ] && [ -z "${temp//[0-9]}" ]
		then
			# Everything is a number - not translating
			value="$temp"
		elif [ -n "${temp//[!:]}" ] && [ $size -gt 2 ]
		then
			# Possibly a HEX string
			temp_lc="$LC_ALL"
			LC_ALL=C
			if [ -z "${temp//[0-9a-fA-F:]}" ]
			then
				temp_ifs="$IFS"
				IFS=:
				temp=($temp)
				IFS="$temp_ifs"
				# translave at most 32 bit integers
				if [ "${#temp[*]}" -le 4 ]
				then
					value=
					for idx in ${!temp[*]}
					do
						[ ${#temp[$idx]} -eq 1 ] && value="${value}0${temp[$idx]}" || value="${value}${temp[$idx]}"
					done
					value=$((16#${value}))
				fi
			fi
			LC_ALL="$temp_lc"
		else
			if [ -z "${temp//[0-9a-fA-F:]}" ] && [ $size -le 2 ]
			then
				value=$((16#${temp}))
			else
				# A string value maybe ASCII formated
				value=$(printf %02x "'${temp:0:1}")
				for ((idx=1; idx<$size; idx++))
				do
					value="${value}$(printf %02x "'${temp:$idx:1}")"
				done
				value=$((16#${value}))
			fi
		fi
		;;
	esac
fi
echo "$value"

