#!/bin/tcsh -f
# JLdL 18Apr08.
#
# Copyright (C) 2008 by Jorge L. deLyra <delyra@fma.if.usp.br>.
# This program may be copied and/or distributed freely. See the
# _ terms and conditions in /usr/share/doc/<package>/copyright.
#
# This script will disable the current IP addresses of all hosts
# _ which are currently registered in the dynahostnis service;
# _ this is done in order to prevent any addresses which are not
# _ actually in use from remaining authorized after a reboot or
# _ halt; note that the script does nothing in the "stop" case;
# _ it is better to use it when coming up into multi-user mode,
# _ rather than when shutting down the system for a reboot or
# _ for a halt, specially if it is an emergency one, since it
# _ may take a little while to run.
#
# Set the basic path, since this will be executed by init.
setenv PATH /sbin:/usr/sbin:/bin:/usr/bin
#
# Get the action key from the command line.
set cla = "$1"
#
# Record the name this script was called with.
set name = `basename $0`
#
# Deal with the command-line argument in the standard way.
switch ( $cla )
case "start":
    #
    # Define the location of the configuration file.
    set conffile = /etc/dynahost/$name.conf
    #
    # If it is not found, then exit without error,
    # _ but first issue a warning.
    if ( ! -f $conffile ) then
	echo "${name}: WARNING: missing configuration file"
	exit 0
    endif
    #
    # Define a variable with the TAB character.
    set tab = "`echo '\t'`"
    #
    # Get all the registered remote hosts from the configuration file.
    set rhosts = `cat $conffile | \
		    grep -v '^[ $tab]*#' | \
		    tr "$tab" ' ' | \
		    tr -s ' ' | \
		    sed -e 's|^ *||g' | \
		    cut -d ' ' -f 2`
    #
    # Write out an informative message.
    echo "${name}: disabling all hosts in the dynahostnis system"
    /usr/lib/dynahost/disable-nishosts $rhosts
    #
    # Write out an informative message.
    echo "${name}: disabling all mail relays in the dynahostnis system"
    /usr/lib/dynahost/disable-nismailrelays $rhosts
    #
    breaksw
case "stop":
    breaksw
default:
    echo "Usage: /etc/init.d/$name {start|stop}"
    breaksw
endsw
#
# Make sure that we exit without error.
exit 0
