#!/bin/sh

IP="$(cat $( grep -l "BOOTPROTO=static" /etc/net/ifaces/eth*/options | sed 's@\(.*\)/.*@\1/ipv4address@') < /dev/null | sed 's@/.*@@' | sort -rnk 4| head -n1)"

test -z "$IP" && exit 1

general="Применить"
client_gw="$IP"
client_search="$(grep "^search" /etc/resolv.conf | cut -d\  -f2)"
client_dns="$(grep "^nameserver" /etc/resolv.conf | cut -d\  -f2)"
client_time="3600"
i="${IP##*.}"; n="${IP%.*}"
if [ $i -lt 190 ]; then ip_s=$(((1+$i/10)*10));
else ip_s=$((($i/10-6)*10)); fi
ip_start="$n.$ip_s"
ip_end="$n.$(($ip_s+52))"
language="ru_RU;en_US;en_US"
iface="$(basename $(dirname $(grep -l "$IP" /etc/net/ifaces/eth*/ipv4address)))"

test -z "$iface" && exit 2

Shoo() { # constr?
echo "_message:begin"
echo "_objects:/"
test -z "$1" && echo "action:write" || { echo "action:constraints"; echo "orig_action:write"; }
echo "general:$general"
echo "client_gw:$client_gw"
echo "client_search:$client_search"
echo "client_dns:$client_dns"
echo "client_time:$client_time"
echo "ip_end:$ip_end"
echo "ip_start:$ip_start"
echo "language:$language"
echo "iface:$iface"
echo "_message:end"
}

{ Shoo constraints; Shoo; } | /usr/lib/alterator/backend3/dhcp
