:
# NAME:
#	bouncedns - scheduled restart of named
#
# SYNOPSIS:
#	bouncedns start
#	bouncedns [reload] [-f]
#
# DESCRIPTION:
#	This script can be run regularly to check if a restart or
#	reload of 'named' has been requested.
#
#	The first command line causes it to look for the flag file
#	named by the variable "NDBOUNCE", "DNS_DIR"/.restart_dns or
#	"DNS_DIR"/.reload_dns and if present performs the appropriate
#	action.  If the flag file contains the word 'reload' that is
#	the action, otherwise it is 'restart'.
#
#	If the first arg is '-f' or 'force' it performs the action
#	immediately after ensuring that the flags file has been
#	removed.  The .reload_dns flag file is alway removed, the
#	.restart_dns file is only removed if the action performed was a
#	restart. 
#
#	If run with no arg it simply creates the flag file.
#
# FILES:
#	"DNS_DIR"/.bounce_dns	bounce flag
#	"DNS_DIR"/.reload_dns	reload flag
#	/etc/rc_d/named		named start/stop script
#
# AUTHOR:
#	Simon J. Gerraty <sjg@crufty.net>

# RCSid:
#	$Id: bouncedns,v 1.20 2023/06/11 18:35:13 sjg Exp $
#
#	@(#) Copyright (c) 1994 Simon J. Gerraty
#
#	This file is provided in the hope that it will
#	be of use.  There is absolutely NO WARRANTY.
#	Permission to copy, redistribute or otherwise
#	use this file is hereby granted provided that 
#	the above copyright notice and this notice are
#	left intact. 
#      
#	Please send copies of changes and bug-fixes to:
#	sjg@crufty.net
#

# a dance to find RC_D
Mydir=`dirname $0`
for d in $RC_D $Mydir $Mydir/../etc/rc_d /etc/rc_d
do
	test -s $d/dnsfuncs.sh || continue
	RC_D=`cd $d && 'pwd'`; export RC_D
	. $d/dnsfuncs.sh
	break
done

case "$1" in
reload)
	action=reload
	shift
	;;
*)	case $0 in
	*reload*)
		action=reload
		;;
	esac
	;;
esac
action=${action:-restart}
NDC=${NDC:-${RC_D:-/etc/rc_d}/named}
RNDC=${RNDC:-$NDC}

case "$1" in
"")
	umask 002
	touch ${NDBOUNCE:-.${action}_dns} || exit 1
	echo "The $action will happen at next scheduled time"
	echo "Use -f for imediate $action, or run '$NDC $action'"
	exit 0
	;;
-f|force) ;;
*)
	x=exit
	for a in $NDBOUNCE .bounce_dns .restart_dns .reload_dns
	do
		test -f $a || continue
		case "$a" in
		*reload*)
			action=reload;;
		*)	action=restart;;
		esac
		x=:
		break
	done
	$x 0
	;;
esac

NDBOUNCE=${NDBOUNCE:-.${action}_dns}
case $action in
restart)
	rm -f $NDBOUNCE .bounce_dns .restart_dns .reload_dns;;
*)	rm -f $NDBOUNCE .reload_dns;;
esac
# we want the output in mail...
case "$action" in
*start) $NDC $action;;
*) $RNDC $action;;
esac
