#!/bin/sh


dnsmasq_conf="/etc/dnsmasq.conf"
dnsmasq_conf_tftp="/etc/dnsmasq-tftp.conf"
dnsmasq_conf_notftp="/etc/dnsmasq-notftp.conf"


if [ "$1" == "start" ]; then
    systemctl stop dnsmasq
    rm -f $dnsmasq_conf
    ln -sf $dnsmasq_conf_tftp $dnsmasq_conf
    systemctl start dnsmasq
    exit $?
fi


if [ "$1" == "stop" ]; then
    systemctl stop dnsmasq
    rm -f $dnsmasq_conf
    ln -sf $dnsmasq_conf_notftp $dnsmasq_conf
    systemctl start dnsmasq
    exit $?
fi


if [ "$1" == "status" ]; then
    egrep 'pxelinux.0.disable' $dnsmasq_conf > /dev/null
    exit_value=$?
    if [ $exit_value -eq 0 ]; then
        echo "stopped"
        exit 1
    else
        echo "running"
        exit 0
    fi
    #exit $exit_value
fi


if [ "$1" == "nic" ]; then
    nic=$2
    sed -i "s@^interface=.*@interface=${nic}@g" $dnsmasq_conf_tftp
    sed -i "s@^interface=.*@interface=${nic}@g" $dnsmasq_conf_notftp
    systemctl restart dnsmasq
    exit 0
fi


exit 1
