#!/bin/bash   

ABOUTME='
This script runs through a ms-dns boot file.
It throws all appropriate domains to tld server to check delegation
Output is each domain that fails delegation checks.
'




#################
# Configurables #
#################

VERSION="0.1"
# BATCH value means in format addsecs.htm/newzones.txt can process. anything else to turn off
OUTFORMAT="notBATCH"		
# Each TLD needs a 'root' server which should respond with proper zone names.
DOTCOMTLD="a.gtld-servers.net"
DOTNETTLD="a.gtld-servers.net"
DOTORGTLD="a7.nstld.com"
DOTCODOTIL=""
# Proces zones we secondary for? No for no, anything else is yes.
PROCESSSECONDARIES="No"
# If loopdelat is more than zero, sleep that amount seconds between each loop/tld/dig
LOOPDELAY="0"

#################
# End Confs	#
#################

LINE=0


function usage() {
	
        echo "Usage: `basename $0` [-i] ms-dnsboot [-o outfile] [-c configfile] [-l loopdelayseconds] [-f] [-F] [-s] [-S] [-v] [-V] [-q] [-?] [-h] [-H]"
	echo "-i inputfile	: Input file in ms-dns format. NO STDIN AVAILABLE"
	echo "-o outputfile	: Outputfile, else to stdout"
	echo "-c configfile	: Config file with variable definitions"
	echo "-l seconds	: Loopdelay In seconds between loops/domains/digs"
	echo "-f 		: Format for non Batch (default)"
	echo "-F		: Format for BATCH"
	echo "-s		: Process secondary zones"
	echo "-S		: Do not Process secondary zones (default)"
	echo "-v		: Be verbose. May be No-Op"
	echo "-V		: Display version"
	echo "-q		: Quiet Mode, may be a no-op"
	echo "-W		: A WhoamI? message"
	echo "-?|-h|-H		: This message"
	echo "If -i is not specified this program will take first non-option argument as filename to process" 
	echo "EXAMPLES:"
	echo "`basename $0` -i my-ms-dnsboot"
	echo "`basename $0` -i my-ms-dnsboot -F -o batchzones.txt -l 3"
	echo "`basename $0` mybootfile" 
        exit 60;
}

function aboutme(){

	echo "$ABOUTME"
	version
	usage;
}

function version () {

	echo "`basename $0` version: $VERSION"
	usage
}

function outputdomain(){

	if [[ "$OUTPUTFILE" != "" ]]; then
		touch "$OUTPUTFILE"
		if ! [ -f "$OUTPUTFILE" ]; then
			echo "Could not open $OUTPUTFILE for writing" > /dev/stderr
			exit 1;
		fi
		OUTPUTTOFILE=">> $OUTPUTFILE";
	fi	

	case "$OUTFORMAT" in
	BATCH) echo "\"delete\",\"$1\"" $OUTPUTTOFILE;;
	*) echo "$1" $OUTPUTTOFILE;;
	esac;
}

if [[ "$#" < "1" ]]; then usage; fi

#getopts to parse cmd-line params and perform actions

while getopts i:o:c:l:fFsSvVqW?hH opt; do
        case "$opt" in
        c) . $OPTARG;; #read include file that contains variables
        i) INPUTFILE="$OPTARG";;
        l) LOOPDELAY=$OPTARG;;
        q) quiet="-q";;
        v) quiet= ;;
        V) version ;;
	o) OUTPUTFILE="$OPTARG";;
	s) PROCESSSECONDARIES="Yes";;
	S) PROCESSSECONDARIES="No";;
	f) OUTFORMAT="notBATCH";;
	F) OUTFORMAT="BATCH";;
	W) aboutme;;
        ?) usage;;
	h) usage;;
	H) usage;;

        esac;
done

if [[ "$INPUTFILE" == "" ]]; then
	INPUTFILE="$1"
fi 

while(true) ; do

	((LINE++))
	
	
	INPUT=`printline $LINE < $INPUTFILE`

	if (( $? > 0 )); then 
	exit;
	fi 
	
	
	TYPE=`echo "$INPUT" | cut -f1 | grep -vE '^;' | grep -vE '^\.' | grep -vE '[[:space:]]'`
	if [[ "$TYPE" == "secondary" ]] && [[ "$PROCESSSECONDARIES" == "No" ]]; then continue; fi
	OUTPUT=`echo "$INPUT" | cut -f2 | grep -vE '^;' | grep -vE '^\.' | grep -vE '[[:space:]]'`
	if [[ "$OUTPUT" == "" ]]; then continue; fi
	
	NAMESERVER=""	
	
	if(("`echo "$OUTPUT" | grep -iqE '\.org$'; echo $?`" == 0)); then
		NAMESERVER=$DOTORGTLD;
	elif(("`echo "$OUTPUT" | grep -iqE '\.net$|\.com$'; echo $?`" == 0)); then
		NAMESERVER=$DOTCOMTLD;
	fi

	if [[ "$NAMESERVER" != "" ]]; then
		dig @"$NAMESERVER" "$OUTPUT" | grep -iqE 'nameserver[1-4]*\.ttec\.com' 
		if(( $? > 0)); then outputdomain "$OUTPUT"; fi 
	fi
	if (( "$LOOPDELAY" > 0 )); then
		sleep $LOOPDELAY
	fi
done

