#!/bin/bash
# Ubuntu only

# Locate directroy of DNS zone files
if [ -d "/var/named/run-root/var/" ]; then
        DNS_ZONE_DIR="/var/named/run-root/var/"
        echo "DNS zone files located in $DNS_ZONE_DIR"
else
        if [ -d "/var/named/chroot/var/" ]; then
                DNS_ZONE_DIR="/var/named/chroot/var/"
                echo "DNS zone files located in $DNS_ZONE_DIR"
        else
                echo "Can't find directory for DNS zones files"
                exit;
        fi
fi

# Request input of the 1st new nameserver
echo "Before using this script, please make sure all Domains have exactly TWO NS Records in Plesk and that there are no double TLD Domains like .co.uk."
 
echo "Enter new primary nameserver: "
read NEW_NS1

# Check if an A-Record for the Domain of NS1 exists, if its DNS Zone is Managed by the hosting server, since Plesk will otherwise throw an error after deleting the NS Record.
VALIDATE_NEW_NS1=$(ls "$DNS_ZONE_DIR" | awk '{print $1}' | grep -v 'localhost.rev\|named.root\|run\|PROTO.localhost.rev\|make-localhost\|managed-keys.bind\|managed-keys.bind.jnl\|.in-addr.arpa' | grep `echo "$NEW_NS1" | awk -F. '{print $(NF-1)"."$NF }'`)
NS_DOMAIN1=(`echo $NEW_NS1 | awk -F. '{print $(NF-1)"."$NF }'`)
if [ -n "$VALIDATE_NEW_NS1" ]; then
        NEW_NS_LOCAL_IP1=$(dig -t a +short $NEW_NS1 @localhost)
    if [ -z "$NEW_NS_LOCAL_IP1" ]; then
            echo "DNS Management for $NS_DOMAIN1 is enabled on this server, but the zone is missing an A-Record for $NEW_NS1. Please create the missing A-Record or, if you are using external nameservers, disable DNS Management for the Domain in Plesk and try again."
            exit;
        fi
fi

# Request input of the 2nd new nameserver
echo "Enter new secondary nameserver: "
read NEW_NS2

# Check if an A-Record for the Domain of NS2 exists, if its DNS Zone is Managed by the hosting server, since Plesk will otherwise throw an error after deleting the NS Record.
VALIDATE_NEW_NS2=$(ls "$DNS_ZONE_DIR" | awk '{print $1}' | grep -v 'localhost.rev\|named.root\|run\|PROTO.localhost.rev\|make-localhost\|managed-keys.bind\|managed-keys.bind.jnl\|.in-addr.arpa' | grep `echo "$NEW_NS2" | awk -F. '{print $(NF-1)"."$NF }'`)
NS_DOMAIN2=(`echo $NEW_NS2 | awk -F. '{print $(NF-1)"."$NF }'`)
if [ -n "$VALIDATE_NEW_NS2" ]; then
        NEW_NS_LOCAL_IP2=$(dig -t a +short $NEW_NS2 @localhost)
      if [ -z "$NEW_NS_LOCAL_IP2" ]; then
            echo "DNS Management for $NS_DOMAIN2 is enabled on this server, but the zone is missing an A-Record for $NEW_NS2. Please create the missing A-Record or, if you are using external nameservers, disable DNS Management for the Domain in Plesk and try again."
        exit;
    fi
fi

# Request confirmation of the given input
while true; do
        read -p "Do you want to change the nameserver for all domains on this server to $NEW_NS1 and $NEW_NS2? " yn
    case $yn in
            [Yy]* ) USR_ACK='1'; break;;
        [Nn]* ) echo "Operation cancelled";;
        * ) echo "Please answer yes or no.";;
    esac
done

# Perform action according to the users choice
if [ $USR_ACK -eq 1 ]; then
  echo 'ok'
  for DOMAIN in $(ls "$DNS_ZONE_DIR" | cut -d" " -f1 | grep -v 'localhost.rev\|named.root\|run\|PROTO.localhost.rev\|make-localhost\|managed-keys.bind\|managed-keys.bind.jnl\|.in-addr.arpa'); do
        echo "$DNS_ZONE_DIR / $DOMAIN / $NEW_NS1 / $NEW_NS2"
        plesk bin dns --del $DOMAIN -ns '' -nameserver $(plesk bin dns --info $DOMAIN | grep NS | awk '{print $3}' | sed '1!d') &&
        plesk bin dns --add $DOMAIN -ns '' -nameserver $NEW_NS1 &&
        plesk bin dns --del $DOMAIN -ns '' -nameserver $(plesk bin dns --info $DOMAIN | grep NS | awk '{print $3}' | sed '1!d') &&
        plesk bin dns --add $DOMAIN -ns '' -nameserver $NEW_NS2
        done
else
  echo 'error'
fi