1#!/bin/sh
2# convert:
3
4# dhcptype=5
5# serverid=172.16.42.102
6# lease=97200
7# interface=eth0
8# ip=172.16.42.177
9# subnet=255.255.255.0
10# mask=24
11# broadcast=172.16.22.255
12# router=172.16.42.98
13# dns=10.34.32.125 10.32.63.5 10.34.255.7 10.11.255.27
14# domain=lab.example.com example.com
15# ntpsrv=10.34.32.125 10.34.255.7
16
17# into:
18
19#let cfg=cfg+1
20#if[$cfg]=...; ip[$cfg]=...; ipmask[$cfg]=.../...; gw[$cfg]=...; net[$cfg]=... dns[$cfg]=...
21
22exec >/dev/null
23#exec >"$0.out"  # debug
24exec 2>&1
25
26test "$interface" || exit 1
27test "$ip" || exit 1
28
29# some servers do not return subnet option.
30# guess it for standard private networks.
31if ! test "$mask"; then
32	case "$ip" in
33	10.*)
34		mask=8;;
35	192.168.*)
36		mask=16;;
37	#172.16-31.x.x
38	172.1[6789].*)
39		mask=12;;
40	172.2[0123456789].*)
41		mask=12;;
42	172.3[01].*)
43		mask=12;;
44	esac
45fi
46
47# some servers do not return router option.
48# assume DHCP server is the router.
49if ! test "$router"; then
50	test "$serverid" && router="$serverid"
51fi
52
53{
54echo "let cfg=cfg+1"
55test "$interface"	&& echo "if[\$cfg]='$interface'"
56test "$ip"		&& echo "ip[\$cfg]='$ip'"
57test "$ip" && test "$mask" \
58			&& echo "ipmask[\$cfg]='$ip/$mask'"
59test "$router"		&& echo "gw[\$cfg]='$router'"
60test "$dns"		&& echo "dns[\$cfg]='$dns'"
61# TODO: I never saw a dhcp server which correctly announces
62# which subnet(s) is/are available thru advertised router
63# Assume 0/0
64echo "net[\$cfg]='0/0'"
65} >"$1"
66