#!/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
# set_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
		Set zone information for one or all domains hosted on an account
		to one or all hosts
		Used for failover / changeover
		Inputs may be given as command line parameters or interactively.
		
		Usage:
		./set_dns
		Command line options: 
		-h		Print these instructions and exit
		-u username	CPanel account username
		-H hostname	Optional Host name (nickname)
					If not given send to DNSMASTER and all DNSSLAVEs
		-z mydomain	Optional domain / zone name
					If not given set all zones on this account
		-c		cron mode - if successful remove this job from crontab
				For this, use exact syntax: /path/to/scripts/set_dns -u username -H HOSTNAME -c
EODOC
	exit $DOC_REQUEST
}

# Constants specific to this script:

# Initialise own input variables:
host=''
user=''
domain=''
cronjob='n'

function set_zones {

	host_ip=$SERVER
	# Get remote server ip address from hosts file
	if [ ! "$host_ip" ]; then
		cecho -c $error "Unknown host name (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

	method=$DNSUPDATE
	# Get update method for nameserver from hosts file
	if [ ! "$method" ]; then
		cecho -c $error "No DNS update method for $host in $HOSTS_FILE"
		exit $E_NO_DNS_METHOD
	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

	$PERL -I$SYNC_DIR $PROGRAM_DIR/set_dns.pl $ZONES_DIR $SYSAUTH_DIR $ZONE_FILENAME $host $host_ip $method $allzones
	checkerror $? $E_SET_DNS_FAILED
}

# Start real script

# Check for help request and input variables in the command line options:
while getopts ":hH:u:z:c" opt
do
	case $opt in
		h)	showdocs
			;;
		H)	host=$OPTARG
			;;
		u)	user=$OPTARG
			;;
		z)	domain=$OPTARG
			;;
		c)	cronjob='y'
			;;
		*)	$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
	set_zones $user $host
else
	config -u $user
	for host in $DNS_MASTER $DNS_SLAVE
	do
		config -u $user -H $host
		set_zones $user $host
	done
	#get host nicknames (eg. MYHOST) of all dns servers - master and slave (from roles file)
	if [ ! $host ]; then
		cecho -c $error "No DNS_ records found in $ROLES_FILE"
		exit $E_NO_HOST
	fi
fi

if [ "$cronjob" == "y" ]; then
	# Cron job completed successfully - now remove it
	$CRONTAB -l |$GREP -v "$PROGRAM_DIR/set_dns -u $user -H $host -c" >$CRONTAB_TMP
	# Fetch all cron jobs other than this one (NB - careful with syntax when writing cron in set_status)
	$CRONTAB $CRONTAB_TMP
	checkerror $? $E_WRITE_CRONTAB_FAILED
	# Rewrite crontab without this job
fi

cecho -c $ok "Set DNS done!" ;
