#!/bin/bash
#
# Module: vyatta-system-nameservers
#
# **** License ****
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# This code was originally developed by Vyatta, Inc.
# Portions created by Vyatta are Copyright (C) 2008 Vyatta, Inc.
# All Rights Reserved.
#
# Author: Mohit Mehta
# Date: September 2008
# Description: CLI back-end script for setting/deleting system nameservers
#
# **** End License ****
#

print_usage()
{
    echo "Usage:"
    echo -e "\t$0 update <ip of name-server>"
    echo -e "\t$0 delete <ip of name-server>"
}

restart_dnsmasq ()
{
  # restart dnsmasq if dns-forwarding is configured
  if [ -d /opt/vyatta/config/active/service/dns/forwarding ]; then
     /opt/vyatta/sbin/vyatta-dns-forwarding.pl --update-dnsforwarding >&/dev/null
  fi
}

restart_ntp ()
{
  # restart ntp if ntp is configured
  if [ -f /etc/ntp.conf ] && grep -q "^server" /etc/ntp.conf; then
     /usr/sbin/invoke-rc.d ntp restart >&/dev/null
  fi
}


update_system_nameservers ()
{
  nameserver=$1
  touch /etc/resolv.conf
  # if name-server already in /etc/resolv.conf then exit
  if grep -q "$nameserver\($\|[[:space:]]\)" /etc/resolv.conf; then
     exit 0
  else
  # find last instance of cli inserted nameserver
  # insert currently received nameserver immediately after that
  # this is done to keep system set nameservers priority over dhcp received nameservers
     cli_ns_array=($(awk '{if (!$3) print $2}' /etc/resolv.conf))
     cli_ns_array_len=${#cli_ns_array[*]}
     line_num=0
     if [ $cli_ns_array_len -gt 0 ]; then
        grepped_ns_line=`grep "${cli_ns_array[$cli_ns_array_len-1]}$" -n /etc/resolv.conf`
        echo ${grepped_ns_line%%:*} > /etc/resolv_tmp.conf
        line_num=`cat /etc/resolv_tmp.conf`
     fi
     head -$line_num /etc/resolv.conf > /etc/resolv_tmp.conf
     echo "nameserver      $nameserver" >> /etc/resolv_tmp.conf
     total_lines=`cat /etc/resolv.conf | wc -l`
     rest_lines=`expr $total_lines - $line_num`
     tail -$rest_lines /etc/resolv.conf >> /etc/resolv_tmp.conf
     mv -f /etc/resolv_tmp.conf /etc/resolv.conf
  fi
  restart_dnsmasq
  restart_ntp
}

delete_system_nameserver ()
{
  nameserver=$1
  touch /etc/resolv.conf
  # remove specified nameserver
  sed -i "/$nameserver$/d" /etc/resolv.conf
  restart_dnsmasq
  restart_ntp
}


#
# main
#

case "$1" in
    update)
        if [ $# -ne 2 ]; then
                print_usage
                exit 1
        fi
        update_system_nameservers $2
        exit 0
        ;;

    delete)
        if [ $# -ne 2 ]; then
                print_usage
                exit 1
        fi
        delete_system_nameserver $2
        exit 0
        ;;


    *)
        print_usage
        exit 1
        ;;

esac