#!/bin/sh

# ClonePanel - Manages duplicate accounts on two or more webservers,
# including snapshot backups, monitoring and failover dns.
# Copyright (C)2006 Chris Cheers, Internet Lynx.
# Contact chris[at]clonepanel[dot]com.
# Internet Lynx, PO Box 7117, Mannering Park, NSW 2259, Australia

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.

# 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.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

Version=0.33
# get_dns -h for built-in documentation

set -u
# Be strict about variable declaration

unset PATH
#avoid use of $PATH - limit script to system commands we choose

PROGRAM_DIR=$(/bin/echo $0 |/bin/sed -e "s/\/[a-zA-Z0-9_]*$//")
# Extract the working directory from command line
# NB - if your system doesn't have echo and sed in these locations
# then this line will need to be changed in all shell scripts

# Standard include files:
. $PROGRAM_DIR/includes

# Built-in documentation:
function showdocs {
	$CAT <<-EODOC
		Grab zone information for one or all domains hosted on an account.
		Used for initial setup of a domain or after making changes to dns in CPanel
		Inputs may be given as command line parameters or interactively.
		
		Usage:
		./get_dns
		Command line options: 
		-h		Print these instructions and exit
		-u username	CPanel account username
		-H hostname	Optional Host name (nickname)
					If not given use DNSMASTER
		-z mydomain	Domain / zone name
					If not given get all zones on this account
		-s		Set dns on all hosts if found to have changed

EODOC
	exit $DOC_REQUEST
}


# Initialise own input variables:
host=''
user=''
domain=''
setdns=''

# Start real script

# Check for help request and input variables in the command line options:
while getopts ":hH:u:z:s" opt
do
	case $opt in
		h)	showdocs
			;;
		H)	host=$OPTARG
			;;
		u)	user=$OPTARG
			;;
		s)	setdns=y
			;;
		z)	domain=$OPTARG
			;;
		*)	$ECHO "Unknown option. Use -h for instructions"; exit $E_UNKNOWN_OPT
			;;
	esac
done
shift $(($OPTIND - 1))

OPTIND=1
# reset getopts for next time

if [ ! $user ]; then
	cecho -n -c $prompt "Enter account username: "
	read user
	if [ ! $user ]; then
		cecho -c $error "Username must be specified"
		exit $E_BLANK_INPUTS
	fi
fi


# User-specific Include files (only after defining $user):
if [ "$host" ]; then
	config -u $user -H $host
else
	config -u $user
	host=$DNS_MASTER
	#get host nickname (eg. MYHOST) of dns master server (from roles file)
	if [ ! $host ]; then
		cecho -c $error "No DNS_MASTER found in $ROLES_FILE"
		exit $E_NO_HOST
	fi
	config -u $user -H $host
	cecho -c $warning "Host not given - defaults to DNS_MASTER which is $host"
fi

host_ip=$NS
# Get remote nameserver ip address from hosts file

cecho -c $info "Fetching zone information from $host_ip"

if [ ! "$host_ip" ]; then
	cecho -c $error "Unknown host name / no nameserver (check $HOSTS_FILE)"
	exit $E_NO_HOST
fi

if [ ! -d $USER_DIR ]; then
	cecho -c $error "Error: No account with that username."
	exit $E_NO_ACCOUNT
fi

if [ "$domain" ]; then
	allzones=`$FIND $ZONES_DIR -maxdepth 1 -mindepth 1 -type d -name $domain -printf "%f "`
else
	allzones=`$FIND $ZONES_DIR -maxdepth 1 -mindepth 1 -type d -printf "%f "`
fi
if [ ! "$allzones" ]; then
	cecho -c $error "Error: No zones found - add them first with ./zone"
	exit $E_NO_ZONE
fi

for zone in $allzones
do
	get_dns_response=`$PERL -I $PROGRAM_DIR $PROGRAM_DIR/get_dns.pl $ZONES_DIR $ZONE_FILENAME $host_ip $zone`
	checkerror $? $E_GET_DNS_FAILED
	$ECHO $get_dns_response

	if [ "`$ECHO $get_dns_response |$GREP "up to date"`" ]; then
		cecho -c $ok "get_dns : Zone file for $zone is up to date"
	else
		cecho -c $ok "get_dns : Successfully fetched new $zone zone info from $host"
		if [ ! "$setdns" ]; then
			cecho -n -c $prompt "Set dns now on all hosts? (y/n) "
			read setdns
		fi
		if [ "$setdns" == "y" ]; then
			$PROGRAM_DIR/set_dns -u $user -z $zone
		fi
	fi
done
