#!/bin/bash

function valid_ip()
{
    local  ip=$1
    local  stat=1

    if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
        OIFS=$IFS
        IFS='.'
        ip=($ip)
        IFS=$OIFS
        [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
            && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
        stat=$?
    fi
    return $stat
}

function valid_mac()
{
    local mac=$1

    if [[ $mac =~ ^[0-9a-z]{2}:[0-9a-z]{2}:[0-9a-z]{2}:[0-9a-z]{2}:[0-9a-z]{2}:[0-9a-z]{2}$ ]]; then
        return 0
    fi

    return 1
}


if [ $# -lt 2 ]
then
	echo "Usage: $0 add HOSTNAME IP HWADDR | delete HOSTNAME | set-clone HOSTNAME | set-restore HOSTNAME | set-normal HOSTNAME" >&2
	exit 1
fi

if [[ $2 = *[[:space:]]* ]]
then
	echo "Hostname must not contain whitespaces"
	exit 1
fi
HOSTNAME="$2"

if [ "$1" == "status" ]
then
	echo -n "$HOSTNAME is booting "
	TARGET="`readlink /srv/tftp/pxelinux/pxelinux.cfg/$HOSTNAME`"
	if [ "$TARGET" == "/pxelinux.cfg/local" ]
	then
		echo "from local harddisk"
	elif [ "$TARGET" == "/pxelinux.cfg/live" ]; then
		echo "to live Clonezilla environment"
        elif [ "$TARGET" == "/pxelinux.cfg/clone" ]; then
                echo "to Clonezilla to clone its harddisk"
        elif [ "$TARGET" == "/pxelinux.cfg/live" ]; then
                echo "to Clonezilla to restore its harddisk"
	fi

	exit 0
fi

if [ "$1" == "add" ]
then
	if [ $# -lt 4 ]
	then
		echo "Missing arguments" >&2
		exit 1
	fi

	if ! valid_ip $3; then
		echo "Invalid IP: $3" >&2
		exit 1
	fi
	IP="$3"

	if ! valid_mac $4; then
		echo "Invalid MAC: $4" >&2
		exit 1
	fi
	HWADDR="$4"

	grep -q "include \"/etc/dhcpd.d/$HOSTNAME\";" /etc/dhcpd.d/all || echo "include \"/etc/dhcpd.d/$HOSTNAME\";" >> /etc/dhcpd.d/all
	[ -e "/etc/dhcpd.d/$HOSTNAME" ] || cat /etc/dhcpd.client.conf | sed "s/HOSTNAME/$HOSTNAME/g" | sed "s/HWADDR/$HWADDR/g" > "/etc/dhcpd.d/$HOSTNAME"
	grep -q "$HOSTNAME A $IP" /var/named/zotac.zone || (echo "$HOSTNAME A $IP" >> /var/named/zotac.zone && rndc reload zotac)

	ln -sf /srv/tftp/pxelinux/pxelinux.cfg/default /srv/tftp/pxelinux/pxelinux.cfg/$HOSTNAME
fi

if [ "$1" == "delete" ]
then
	rm -f "/etc/dhcpd.d/$HOSTNAME"
	sed -i "/include \"\/etc\/dhcpd.d\/$HOSTNAME\";/d" /etc/dhcpd.d/all
	sed -i "/$HOSTNAME A $IP/d" /var/named/zotac.zone
	rndc reload zotac

	rm -f /srv/tftp/pxelinux/pxelinux.cfg/$HOSTNAME
fi

if [ "$1" == "set-live" ]
then
	sed -i -E "s/^(\s*)#filename \"\/pxelinux.0\";/\\1filename \"\/pxelinux.0\";/" "/etc/dhcpd.d/$HOSTNAME"
	ln -sf /pxelinux.cfg/default /srv/tftp/pxelinux/pxelinux.cfg/$HOSTNAME
fi

if [ "$1" == "set-clone" ]
then
	sed -i -E "s/^(\s*)#filename \"\/pxelinux.0\";/\\1filename \"\/pxelinux.0\";/" "/etc/dhcpd.d/$HOSTNAME"
	ln -sf /pxelinux.cfg/clone /srv/tftp/pxelinux/pxelinux.cfg/$HOSTNAME
fi

if [ "$1" == "set-restore" ]
then
	sed -i -E "s/^(\s*)#filename \"\/pxelinux.0\";/\\1filename \"\/pxelinux.0\";/" "/etc/dhcpd.d/$HOSTNAME"
	ln -sf /pxelinux.cfg/restore /srv/tftp/pxelinux/pxelinux.cfg/$HOSTNAME
fi

if [ "$1" == "set-local" ]
then
	#sed -i -E "s/^(\s*)filename \"\/pxelinux.0\";/\\1#filename \"\/pxelinux.0\";/" "/etc/dhcpd.d/$HOSTNAME"
	ln -sf /pxelinux.cfg/local /srv/tftp/pxelinux/pxelinux.cfg/$HOSTNAME
fi

systemctl restart dhcpd4@eth1
