#!/bin/csh -f

switch (`uname -s``uname -r`)
case SunOS5*:
	set SOLARIS
	set RSYNC = /usr/local/bin/rsync
	set AUTOSEP = "_"	# separator used in automount map filenames
	set LSOF = /usr/local/bin/lsof
	set WHOAMI = `/usr/ucb/whoami`
	breaksw
case Linux[2345]*:
#	set LINUX2
	set LINUX
	set LSOF = /usr/sbin/lsof
	if (! -e $LSOF) set LSOF = /usr/bin/lsof
	set AUTOSEP = "."	# separator used in automount map filenames
	set RSYNC = /usr/bin/rsync
	set WHOAMI = `/usr/bin/whoami`
	breaksw
default:
	exec echo ${0}: does not run on `uname -s` `uname -r`
endsw

set DEBUG

while($#argv)
	switch ("$1")
	case -d:
		set DEBUG = /bin/echo
#		shift
#		breaksw
# fall through to verbose
	case -v:
		set VERBOSE
		shift
		breaksw
	case -c:
		set CHECK
		shift
		breaksw
	case -force-automount-maps:
		set FAUTO
		shift
		breaksw
	case -me:		# NetID to send admin email to (@cs)
				# eg, "-me mcgrew"
		set ME = $2
		shift ; shift
		breaksw
	case -upto:
		set UPTO = $2
		shift ; shift
		breaksw
	default:
		set DQ = '"'
		exec echo ${0}: "What do I do with $DQ$1$DQ?"
	endsw
end

if (! $?UPTO) set UPTO = 999

if (! -e /etc/auto${AUTOSEP}master) exec echo automount not installed

if ($UPTO <= 1) exec echo done debugging to point 1

if ($?VERBOSE) echo `date +%T` $RSYNC

if (-e $RSYNC) then
   if ($?VERBOSE) echo "   " check
else
    if (-e /usr/local/bin/lynx && ! $?CHECK && $?SOLARIS) then
	echo `date +%T` Downloading rsync tar file
	/usr/local/bin/lynx -dump http://www.cs.rutgers.edu/~robtuck/rsync-2.6.9.solaris-lcsr.tar > /tmp/rsync.tar.$$
	echo `date +%T` Installing rsync
	( $DEBUG cd / ; $DEBUG tar -xf /tmp/rsync.tar.$$ )
	/bin/rm /tmp/rsync.tar.$$
    else
	echo $RSYNC needs to be installed
    endif
endif

if ($UPTO <= 2) exec echo done debugging to point 2

set THISHOST = `hostname | sed 's;\.[Rr][Uu][Tt][Gg][Ee][Rr][Ss]\.[Ee][Dd][Uu];;'`
#set THISIP = `host $THISHOST | grep "has address" | sed -e 's;.* ;;'`
# above does not work for spamfilter.cs; 5/3/12
set THISHOSTFULL = $THISHOST.rutgers.edu
set THISIP = `host $THISHOSTFULL | grep "has address" | sed -e 's;.* ;;'`
if ("$THISIP" == "") exec echo I cannot find the IP address of $THISHOSTFULL
set THISSUBNET = `echo $THISIP | sed -e 's;\.[0-9]*$;;'`

if ("$THISSUBNET" == "128.6.13") then
   set HOSTN = `echo $THISIP | sed 's;.*\.;;'`
   if ($HOSTN <= 127) then
      set THISSUBNET = "128.6.13.0"
   else
      set THISSUBNET = "128.6.13.128"
   endif
else if ("$THISSUBNET" == "128.6.168") then
   set HOSTN = `echo $THISIP | sed 's;.*\.;;'`
   if ($HOSTN <= 127) then
      set THISSUBNET = "128.6.168.0"
   else if ($HOSTN >= 192) then
      set THISSUBNET = "128.6.168.192"
   endif
endif

# Don't need this anymore; 1/29/18
goto nomore
switch ($THISSUBNET)
case 128.6.4:
case 128.6.62:
case 128.6.23:
case 128.6.48:
case 172.16.83:	# cannae.cs, saguntum.cs; 11/26/14
case 172.16.93: # drupal.cs; 9/21/15
case 172.16.90: # *-yp2.cs; 5/31/16
#     set HOLD = "backup.cs"
     set HOLD = "backup.lcsr"	# 2/22/17
     set AHOLD = "hold-4"
     breaksw
case 128.6.5:
     set HOLD = "hold-5"
     breaksw
case 128.6.13.0:
     set HOLD = "hold-13"
     breaksw
case 128.6.13.128:
#     set HOLD = "hold-14"
     set HOLD = "backup.lcsr"
     breaksw
case 128.6.25:
     set HOLD = "hold-25"
     breaksw
case 128.6.26:
     set HOLD = "hold-26"
     breaksw
case 128.6.60:
     set HOLD = "hold-60"
     breaksw
case 128.6.75:
     set HOLD = "hold-75"
     breaksw
case 128.6.168.0:
     set HOLD = "hold-168"
     breaksw
case 128.6.168.192:
     set HOLD = "hold-170"
     breaksw
default:
# don't bother with this anymore
#	echo I do not know name for hold on $THISSUBNET
#	echo " (Guessing default)"
#	set HOLD = "backup.cs"
# Now running into hosts which cannot deal with above
#	set HOLD = "backup.cs.rutgers.edu"
# fix this elsewhere
#	set HOLD = "backup.cs"
	set HOLD = "backup.lcsr"# 2/22/17
	set AHOLD = "hold-4"
endsw

nomore:
set HOLD = "backup.lcsr"

if ($UPTO <= 3) exec echo done debugging to point 3

set RCONF = /etc/rsyncd.conf

if ("$DEBUG" != "") echo ""
if ($?VERBOSE) echo `date +%T` $RCONF

if (-e $RCONF) then
    grep "\[$THISHOST-backup\]" $RCONF > /dev/null
    if ($status) then
	echo $RCONF makes no mention of "[$THISHOST-backup]"
	set NEEDRCONF
    endif
   grep -w $THISHOST $RCONF > /dev/null
   if ($status) then
      echo $RCONF makes no mention of $THISHOST
      set RCONFERR
   endif
   if ($?HOLD) then
      grep -w $HOLD $RCONF > /dev/null
      if ($status) then
	 echo $RCONF makes no mention of $HOLD
	 set RCONFERR
      endif
   endif
   if ($?VERBOSE && ! $?RCONFERR) echo "   " check
#else
# rsyncd.conf may exist but not be set up for d2d yet; 3/16/16
endif
if (! -e $RCONF || $?NEEDRCONF) then
    if ( ! $?CHECK) then
	echo `date +%T` Generating $RCONF
	cat > /tmp/rsyncd.conf.$$ <<EOF
pid file = /var/run/rsyncd.pid
[hostname-backup]
        path = /
        comment = hostname backup
        uid = root
        gid = root
        auth users = root
        hosts allow = hold.rutgers.edu
        read only = true
        list = false
        secrets file = /rsync-backup/secret/rsyncd.secrets
        use chroot = true
	ignore nonreadable = true
EOF
	if ("$DEBUG" == "") then
	    if (-e $RCONF) then
		set TSTAMP = "ORIG"
		if (-e $RCONF.$TSTAMP) set TSTAMP = `date +%y%m%d`
		if (-e $RCONF.$TSTAMP) set TSTAMP = `date +%y%m%d.%H%M`
		cp -p $RCONF{,.$TSTAMP}
	    endif
	    sed -e "s;hostname;$THISHOST;" \
		-e "s;hold;$HOLD;" /tmp/rsyncd.conf.$$ >> $RCONF
	    chmod 644 $RCONF
	else
	    if (-e $RCONF) echo Would add the following to ${RCONF}:
	    sed -e "s;hostname;$THISHOST;" \
		-e "s;hold;$HOLD;" /tmp/rsyncd.conf.$$
	endif
	/bin/rm /tmp/rsyncd.conf.$$
    else
	echo $RCONF needs to be set up
    endif
endif

if ($UPTO <= 4) exec echo done debugging to point 4

set SYSTEMCTL = /usr/bin/systemctl
# Ubuntu 16.04 (seqam-w.cs) has systemctl here; 10/17/16
if (! -e $SYSTEMCTL) set SYSTEMCTL = /bin/systemctl

if (-e $SYSTEMCTL) then
    if ($?VERBOSE) echo `date +%T` rsyncd.service
#    /usr/bin/systemctl list-unit-files rsyncd.service | grep enabled > /dev/null
# older systemctl does not allow above format; 5/25/16
#    $SYSTEMCTL list-unit-files | grep rsyncd.service | grep enabled > /dev/null
    $SYSTEMCTL list-unit-files | egrep 'rsyncd.service|rsync.service' | grep enabled > /dev/null
    if ($status) then
# Figure out service name
	set RSYNCSNAME = `$SYSTEMCTL list-unit-files | egrep 'rsyncd.service|rsync.service' | awk '{print $1}'`
	if ("$RSYNCSNAME" == "") then
	    if (-e /usr/bin/service) then
		/usr/bin/service rsync status |& grep -v ":" > /dev/null
		# could be either    * rsync is running
		# or		 rsync: unrecognized service
		# if service knows about it, check that later
		if ($status) then
		    echo Could not determine name for rsyncd service
		    goto skipinit
		else
		    goto checkinit
		endif
	    endif
	endif
	if ( ! $?CHECK) then
	    echo `date +%T` Enabling $RSYNCSNAME
	    $DEBUG $SYSTEMCTL enable $RSYNCSNAME
	    echo `date +%T` Starting rsyncd
	    $DEBUG $SYSTEMCTL start $RSYNCSNAME
	else
	    echo $RSYNCSNAME needs to be enabled
	endif
    else
	if ($?VERBOSE) echo "   " check
    endif
    goto skipinit
endif

checkinit:

if ($UPTO <= 5) exec echo done debugging to point 5

set RSYNCD = /etc/init.d/rsyncd
if (! -e $RSYNCD && -e /etc/init.d/rsync) set RSYNCD = /etc/init.d/rsync

if ("$DEBUG" != "") echo ""
if ($?VERBOSE) echo `date +%T` $RSYNCD

which chkconfig >& /dev/null
if (! $status) then
    chkconfig --list xinetd |& grep ":on" > /dev/null
    if (! -e $RSYNCD && ! $status) then
	if (-e /etc/xinetd.d/rsync) then
	    chkconfig --list rsync | grep -w on > /dev/null
	    if (! $status) then
		echo "   " rsync appears to be run by xinetd.  we will trust that works.
echo "     (but you might check for only_from in /etc/xinetd.d/rsync)"
		goto skiprsyncd
	    endif
	endif
    endif
endif

set EDRSYNC = /etc/default/rsync
if (-e $EDRSYNC) then
    set RSENABLE = `sed 's;#.*;;' $EDRSYNC | grep RSYNC_ENABLE | sed 's;.*=;;'`
    if ("$RSENABLE" == "false" || "$RSENABLE" == "") then
	if (! $?CHECK) then
	    awk '{if(index($1,"RSYNC_ENABLE=")==1){print "#" $0;print "RSYNC_ENABLE=true";next}print}' $EDRSYNC > /tmp/rsync.$$
	    diff $EDRSYNC /tmp/rsync.$$ > /dev/null
	    if (! $status) echo RSYNC_ENABLE=true >> /tmp/rsync.$$
	    if ("$DEBUG" == "") then
		echo `date +%T` Updating $EDRSYNC
		set TSTAMP = "ORIG"
		if (-e $EDRSYNC.$TSTAMP) set TSTAMP = `date +%y%m%d`
		if (-e $EDRSYNC.$TSTAMP) set TSTAMP = `date +%y%m%d.%H%M`
		cp -p $EDRSYNC{,.$TSTAMP}
		cp -p /tmp/rsync.$$ $EDRSYNC
		chmod 644 $EDRSYNC
	    else
		echo Would make the following change to ${EDRSYNC}:
		diff -C0 $EDRSYNC /tmp/rsync.$$
	    endif
	else
	    echo rsync needs to be enabled in $EDRSYNC
	endif
	/bin/rm -f /tmp/rsync.$$
	goto skiprsyncd
    endif
endif

if (-e $RSYNCD) then
   if ($?VERBOSE) echo "   " check
else
    if ( ! $?CHECK) then
	echo `date +%T` Generating $RSYNCD
	cat  <<EOF | sed -e "s;/usr/local/bin/rsync;$RSYNC;" -e "s;/sbin/sh;/bin/sh;" > /tmp/rsyncd.$$
#!/sbin/sh
#
#

rsync_cmd=/usr/local/bin/rsync
rsync_pid_file=/var/run/rsyncd.pid

case "\$1" in
'start')
	echo 'starting rsync daemon'
        \$rsync_cmd --daemon
        ;;

'stop')
	kill \`cat \$rsync_pid_file\` >/dev/null 2>&1
        ;;

*)
        echo "Usage: \$0 { start | stop }"
        ;;
esac
exit 0
EOF
	if ("$DEBUG" == "") then
	    cp /tmp/rsyncd.$$ $RSYNCD
	    ln -s $RSYNCD /etc/rc3.d/S99rsyncd
	else
	    cat /tmp/rsyncd.$$
	    $DEBUG ln -s $RSYNCD /etc/rc3.d/S99rsyncd
	endif
	/bin/rm /tmp/rsyncd.$$
	$DEBUG chmod 755 $RSYNCD
	echo `date +%T` Starting rsyncd
	$DEBUG $RSYNCD start
	$DEBUG sleep 5			# give it time to grab its port
    else
	echo $RSYNCD needs to be installed
    endif
endif

skipinit:

if ("$WHOAMI" == "root") then
  if (-e $LSOF) then
#    set RSYNCPORT = `$LSOF | grep "^rsync .*TCP" | sed 's;.*:\(.*\) .*;\1;' | sort -u`
#    set RSYNCPORT = `$LSOF | grep "^rsync .*TCP" | sed 's;.*:\(.*\) .*;\1;' | sort -u | egrep "rsync|873"`
    set RSYNCPORT = `$LSOF |& grep "^rsync .*TCP" | sed 's;.*:\(.*\) .*;\1;' | sort -u | egrep "rsync|873"`
    if ("$RSYNCPORT" != "rsync" && "$RSYNCPORT" != "873") echo rsync daemon does not appear to have rsync port
# If this occurs, restart process which is holding port -- most likely ypbind,
# then restart rsync daemon
  else
    echo Not checking rsync daemon port -- cannot find lsof
  endif
else
    echo Not checking rsync daemon port -- not root
endif

skiprsyncd:

if (-e /usr/bin/service) then
    /usr/bin/service rsync status | grep "is running" > /dev/nul
    if ($status) then
	if ! ($?CHECK) then
	    echo `date +%T` starting rsync
	    $DEBUG /usr/bin/service rsync start
	else
	    echo Need to start rsync service
	endif
    endif
endif


if ($UPTO <= 6) exec echo done debugging to point 6

set STRUCTURES = /etc/rsync-backup/structures

if ("$DEBUG" != "") echo ""
if ($?VERBOSE) echo `date +%T` $STRUCTURES

grep ^slide: /etc/group > /dev/null
if (! $status) set SLIDEGRP = slide
if (! $?SLIDEGRP) then
    grep ^wheel: /etc/group > /dev/null
    if (! $status) set SLIDEGRP = wheel
endif
if (! $?SLIDEGRP) then
    grep ^sudo: /etc/group > /dev/null
    if (! $status) set SLIDEGRP = sudo
endif
if (! $?SLIDEGRP && -e /usr/bin/ypcat) then
    ypcat group | grep ^slide: > /dev/null
    if (! $status) set SLIDEGRP = slide
endif
if (! $?SLIDEGRP) then
    echo "************************************"
    echo Could not determine privileged group
    echo "************************************"
endif

if ($UPTO <= 7) exec echo done debugging to point 7

if (-e $STRUCTURES) then
   if ($?VERBOSE) echo "   " check
else
    if ( ! $?CHECK) then
	echo `date +%T` Generating $STRUCTURES
	set STRDIR = `dirname $STRUCTURES`
	if (! -e $STRDIR) then
	    echo `date +%T` Making $STRDIR
	    $DEBUG mkdir $STRDIR
	    $DEBUG chmod 750 $STRDIR
#	    $DEBUG chgrp slide $STRDIR
	    if ($?SLIDEGRP) then
		$DEBUG chgrp $SLIDEGRP $STRDIR
	    endif
	endif
	if ($?SOLARIS) then
#	    df -t ufs | grep / | awk '{print $NF}' | grep ^/ > /tmp/structures.$$
# exclude /etc/mnttab.  It produces frequent errors and is not worth
# backing up.
	    df -t ufs | grep / | awk '{print $NF}' | grep ^/ | \
		    sed 's;^/$;/ - /etc/mnttab;' > /tmp/structures.$$
# Add a clue as to the format of the file; 6/24/11
	    echo "# filesystem mount point     exclusions (relative to mount point)" > /tmp/structures.$$
	    echo "#    (see rsync man page)" >> /tmp/structures.$$
	    echo "" 			     >> /tmp/structures.$$
	    df -t ufs | grep / | awk '{print $NF}' | grep ^/ | \
		    sed 's;^/$;/ - /etc/mnttab;' >> /tmp/structures.$$
#	else if ($?LINUX2) then
	else if ($?LINUX) then
#	    grep -w ext3 /etc/mtab | awk '{print $2}' > /tmp/structures.$$
#	    set STRS = `grep -w ext3 /etc/mtab | awk '{print $2}' | sort`
# dump ext4 structures too; 8/24/11
#	    set STRS = `grep -w "ext[34]" /etc/mtab | awk '{print $2}' | sort`
# xfs too; 11/2/15
	    set STRS = `egrep -w "ext[34]|xfs" /etc/mtab | awk '{print $2}' | sort`
# /tmp is real filesystem here; avoid dumping it.
	    set TMPSTR = `df /tmp | awk '{if(NF>1)print $NF}' | grep /`
# Add a clue as to the format of the file; 6/24/11
	    echo "# filesystem mount point     exclusions (relative to mount point)" > /tmp/structures.$$
	    echo "#    (see rsync man page)" >> /tmp/structures.$$
	    echo "" 			     >> /tmp/structures.$$
	    foreach STR ( $STRS )
		if ("$STR" != "/tmp") then
		    if ("$STR" == "$TMPSTR") then
			echo $STR - /tmp >> /tmp/structures.$$
		    else
			echo $STR >> /tmp/structures.$$
		    endif
		endif
	    end
	    set EXCEPT = "(files in /tmp are excluded)"
	endif
	if (! $?EXCEPT) set EXCEPT
	if ("$DEBUG" == "") then
	    cp /tmp/structures.$$ $STRUCTURES
	    chmod 644 $STRUCTURES
	    cat <<EOF

  $STRUCTURES has been set up with all filesystems
  on this machine.  You should probably edit that list if you want
  to exclude some things.  $EXCEPT

EOF
	else
	    cat /tmp/structures.$$
	endif
	/bin/rm /tmp/structures.$$
    else
	echo $STRUCTURES needs to be set up
    endif
endif

if ($UPTO <= 8) exec echo done debugging to point 8

set BCONF = /etc/rsync-backup/backup.conf

if ("$DEBUG" != "") echo ""
if ($?VERBOSE) echo `date +%T` $BCONF

if (-e $BCONF) then
   if ($?VERBOSE) echo "   " check
else
    if ( ! $?CHECK) then
	echo `date +%T` Generating $BCONF
	cat >> /tmp/backup.conf.$$ <<EOF
#client configuration file for rsync backup
admin_email = user@cs.rutgers.edu
EOF
    if (! $?ME) set ME = `/usr/bin/who am i | awk '{print $1}'`
	if ("$DEBUG" == "") then
	    sed -e "s;user;$ME;" /tmp/backup.conf.$$ > $BCONF
	    chmod 644 $BCONF
	else
	    sed -e "s;user;$ME;" /tmp/backup.conf.$$
	endif
	$DEBUG chmod 644 $BCONF
	if ("$DEBUG" == "") then
	    echo ""
	    echo " " $BCONF has been set up so reports 
	    echo " " will be mailed to `grep "@" $BCONF | awk '{print $NF}'`
	    echo ""
	endif
	/bin/rm /tmp/backup.conf.$$
    else
	echo $BCONF needs to be set up
    endif
endif

if ($UPTO <= 9) exec echo done debugging to point 9

set AUTOB = /etc/auto${AUTOSEP}backup
set AUTOBB = auto${AUTOSEP}backup
set AUTOM = /etc/auto${AUTOSEP}master

grep "^\+dir:/etc/auto.master.d" $AUTOM > /dev/null
if(! $status) then
    set AUTOM = /etc/auto.master.d/backup.autofs
    set AUTOB = /etc/auto.master.d/auto${AUTOSEP}backup
#    goto doautob
endif

if ($UPTO <= 10) exec echo done debugging to point 10

grep "This file is automatically generated by the config system" $AUTOM >& /dev/null
set NOTCONFIGSYS = $status
if ($?FAUTO && ! $NOTCONFIGSYS) then
    echo Ignoring that you are apparently using the config system
    set NOTCONFIGSYS = 1
endif

if ("$DEBUG" != "") echo ""
if ($?VERBOSE) echo `date +%T` $AUTOM

grep $AUTOBB $AUTOM >& /dev/null
if ($status) then
    if (! $?CHECK) then
#	if ($?LINUX2) then
	if ($?LINUX) then
	    echo `date +%T` Updating $AUTOM
	    if (-e $AUTOM) cp -p $AUTOM /tmp/autom.$$
	    cat >> /tmp/autom.$$ <<EOF
/rsync-backup	$AUTOB	nosuid,intr,bg
EOF
	    if ("$DEBUG" == "") then
		set AUTOMD = `dirname $AUTOM`
		if (! -e $AUTOMD) then
		    echo `date +%T` Creating $AUTOMD
		    /bin/mkdir $AUTOMD
		    /bin/chown --reference=/etc $AUTOMD
		    /bin/chmod --reference=/etc $AUTOMD
		endif
		if (-e $AUTOM) /bin/mv $AUTOM{,.`date +%y%m%d`}
		cp /tmp/autom.$$ $AUTOM
		chmod 644 $AUTOM
		set RAUTOFS
	    else
		cat /tmp/autom.$$
	    endif
	    /bin/rm -f /tmp/autom.$$
	else if ($?SOLARIS) then
	    echo `date +%T` Updating $AUTOM
	    cp -p $AUTOM /tmp/autom.$$
	    cat >> /tmp/autom.$$ <<EOF
/-		$AUTOB	-nosuid,intr,hard,bg
EOF
	    if ("$DEBUG" == "") then
		/bin/mv $AUTOM{,.`date +%y%m%d`}
		cp /tmp/autom.$$ $AUTOM
		chmod 644 $AUTOM
		set RAUTOFS
	    else
		cat /tmp/autom.$$
	    endif
	    /bin/rm -f /tmp/autom.$$
	else
	    echo $AUTOM set up needs to be automated
	endif
    else
	if ($NOTCONFIGSYS) then
	    if (-e $AUTOM) then
		echo $AUTOM makes no mention of $AUTOBB
	    else
		echo $AUTOM needs to be set up
	    endif
	    set AUTOMERR
	else
	    echo ""
	    echo " " automount maps are controlled by the config system.
	    echo " " Please configure it to add $AUTOB here.
	    echo "    ("Edit etcauto${AUTOSEP}master.conf, etcauto${AUTOSEP}backup.conf, 
	    echo "    " `domainname`/mkautofs.conf")"
	    echo ""
	    goto skipautob
	endif
    endif
else
   if ($?VERBOSE && ! $?AUTOMERR) echo "   " check
endif

doautob:

if ("$DEBUG" != "") echo ""
if ($?VERBOSE) echo `date +%T` $AUTOB

if (-e $AUTOB) then
    grep -w $HOLD $AUTOB > /dev/null
#   if ($status) then
    set STATUS = $status
    set ASTATUS = 1
#    if ($?AHOLD) then
# What do we use this for anymore? ; daw, 9/8/17
    if ($?AHOLD && 0) then
	grep -w $AHOLD $AUTOB > /dev/null
	set ASTATUS = $status
    endif
    if ($STATUS && $ASTATUS) then
      echo $AUTOB makes no mention of $HOLD
      set AUTOBERR
    endif
    if ($?VERBOSE && ! $?AUTOBERR) echo "   " check
    grep -w $THISHOST $AUTOB > /dev/null
    if ($status) then
      echo $AUTOB makes no mention of $THISHOST
      set AUTOBERR
    endif
    if ($?VERBOSE && ! $?AUTOBERR) echo "   " check
else
    if ( ! $?CHECK) then
#	if ($?LINUX2) then
	if ($?LINUX) then
	    echo `date +%T` Generating $AUTOB
	    cat > /tmp/autob.$$ <<EOF
secret	-rw	${HOLD}.rutgers.edu:/backup/secret/$THISHOST
logs	-rw	${HOLD}.rutgers.edu:/backup/logs/$THISHOST
backups	-rw	${HOLD}.rutgers.edu:/backup/backups/$THISHOST
EOF
	    if ("$DEBUG" == "") then
		cp /tmp/autob.$$ $AUTOB
		chmod 644 $AUTOB
		set RAUTOFS
		set RAUTOFS
	    else
		cat /tmp/autob.$$
	    endif
	    /bin/rm -f /tmp/autob.$$
	else if ($?SOLARIS) then
	    echo `date +%T` Generating $AUTOB
	    cat > /tmp/autob.$$ <<EOF
/rsync-backup/secret	${HOLD}:/backup/secret/$THISHOST
/rsync-backup/logs	${HOLD}:/backup/logs/$THISHOST
/rsync-backup/backups	${HOLD}:/backup/backups/$THISHOST
EOF
	    if ("$DEBUG" == "") then
		cp /tmp/autob.$$ $AUTOB
		chmod 644 $AUTOB
		set RAUTOFS
	    else
		cat /tmp/autob.$$
	    endif
	    /bin/rm -f /tmp/autob.$$
	else if (! $NOTCONFIGSYS) then
	    echo ""
	    echo " " automount maps are controlled by the config system.
	    echo " " Please configure it to add $AUTOB here.
	    echo "    ("Edit etcauto${AUTOSEP}master.conf, etcauto${AUTOSEP}backup.conf, 
	    echo "    " `domainname`/mkautofs.conf")"
	    echo ""
	else
	    echo $AUTOB set up needs to be automated
	endif
    else
	echo $AUTOB needs to be set up
    endif
endif

if ($?RAUTOFS) then
    if (-e /usr/bin/service && ! -e /bin/systemctl) then
# here's how to do this under Ubuntu; 9/25/15
	echo `date +%T` Reloading autofs
	$DEBUG /usr/bin/service autofs reload
    else
	echo `date +%T` Restarting autofs
	if (-e /etc/init.d/autofs) then
	    $DEBUG /etc/init.d/autofs stop
	    $DEBUG /etc/init.d/autofs start
	else
	    $DEBUG /usr/sbin/service autofs restart
	endif
    endif
endif

if ($UPTO <= 11) exec echo done debugging to point 11

# under ubuntu, need to create mountpoint?

if ($?VERBOSE) echo `date +%T` /rsync-backup
if (-e /rsync-backup) then
    if ($?VERBOSE) echo "   " check
else
    if (! $?CHECK) then
	$DEBUG /bin/mkdir /rsync-backup
	if (-e /usr/bin/service && ! -e /bin/systemctl) then
# here's how to do this under Ubuntu; 9/25/15
	    echo `date +%T` Reloading autofs
	    $DEBUG /usr/bin/service autofs reload
	else
	    echo `date +%T` Restarting autofs
	    if (-e /etc/init.d/autofs) then
		$DEBUG /etc/init.d/autofs stop
		$DEBUG /etc/init.d/autofs start
	    else
		$DEBUG /usr/sbin/service autofs restart
	    endif
	endif
    else
	echo /rsync-backup may need to be built
    endif
endif

skipautob:

if ($UPTO <= 12) exec echo done debugging to point 12

set HOSTSALLOW = /etc/hosts.allow

if (-e $HOSTSALLOW) then
   if ($?VERBOSE) echo `date +%T` $HOSTSALLOW

   grep "ALL:ALL@ALL:deny" $HOSTSALLOW > /dev/null
   if (! $status) then
      set HOLDIP = `host $HOLD.rutgers.edu | grep "has address" | sed -e 's;.* ;;'`
      grep "rsync:${HOLDIP}:allow" $HOSTSALLOW > /dev/null
      if ($status) then
	 echo $HOSTSALLOW does not seem to allow rsync from $HOLDIP
	 set HOSTSALLOWERR
      endif
   endif
#   endif
   if ($?VERBOSE && ! $?HOSTSALLOWERR) echo "   " check
endif

if ("$DEBUG" != "") echo ""
if ($?VERBOSE) echo `date +%T` Done

