#!/bin/sh
#
# NOTE: The original of this file is under RCS control
#
#       Update $Source: /work/sysadmin/local/bin_scripts/RCS/build_standalone,v $
#       and copy to /usr/local/bin_scripts.
#
# Shell script to copy all files for stand alone operation of falls
#
# 06 Jan 98  Henry Grebler	Allow comments in include files.
# 08 Sep 97  Henry Grebler	Improve error checking.
# 02 Jun 97  David Wilson	Don't display voluminous tar listing
# 17 Sep 96  David Wilson	Add RCS source to comment headings
# 06 Aug 96  David Wilson	Relocate config files to standalone sub-dir
# 04 Dec 95  Henry Grebler	Only on suns does tar have -I.
# 06 Mar 95  David Wilson	Separated out user directories lists
#                               Use config file instead of symbolic link
# 01 Mar 95  David Wilson	Initial version of script
#				Use tar to copy files listed in standalone.xxx
#				Use find to fix directory permissions

#=============================================================================#

Usage () {
	cat <<EOF
Usage:	`basename $0` [-v]

		-v	verbose
EOF
	exit 1
}

# --------------------- init ----------------------
init() {
host="`uname -n`"
# Allow user to set directory as envar
config=${CONFIG_DIR-/work/sysadmin/standalone}/standalone.$host.config

unset destdir
unset includefiles
if [ -f $config ]; then
   . $config
   if [ "$destdir" = "" ]; then
     echo "ERROR: 'destdir' has not been assigned a value."
     echo ERROR: Please edit $config before continuing.
     error=true
   fi
   if [ "$includefiles" = "" ]; then
     echo "ERROR: 'includefiles' has not been assigned a value."
     echo ERROR: Please edit $config before continuing.
     error=true
   fi
else
   echo WARNING: Creating configuration file $config
   cp /work/sysadmin/standalone/standalone.template.config $config
   chgrp staff $config
   chmod ug=rwx,o= $config
   echo WARNING: Please edit $config before continuing.
   error=true
fi

	includefiles=`echo "$includefiles" | grep -v '^#'`
}

# ----------------- checkcontents ----------------------

checkcontents () {
	cd /
	files=`grep -v '^#' $includefile`
	errors=`ls -d $files 2>&1 > /dev/null`
	if [ "$errors" = "" ]; then return; fi
	cat <<EOF
Errors in include file: $includefile

$errors

EOF
	error=true

}

# ----------------- checkroot ---------------------
checkroot() {
if [ "`whoami`" != "root" ]; then
   echo
   echo ERROR: This script needs to be run from "root"
   error=true
fi
}

# ----------------- mkincludargs ---------------------
mkincludeargs() {
unset includeargs
for includefile in $includefiles; do
   if [ "$verbose" = true ]; then
	echo Processing $includefile
   fi
   if [ -f $includefile ]; then
      checkcontents
      includeargs="$includeargs $files"
   else
      echo ERROR: Cannot find include file $includefile
      echo $includefile should contain a list of files and
      echo directories one path per line.  The leading slash must be absent
      echo on every line.
      error=true
   fi
done
}

# ----------------- confirm ---------------------
confirm() {
echo About to delete and recreate the standalone directory tree $destdir
echo 'Ok to proceed? ("yes" or "no"):'
read reply
echo
case "$reply" in
   [Yy]|[Yy][Ee][Ss])
      ;;
   *)
      echo Rebuild of $destdir cancelled.
      exit 1
      ;;
esac
}

# ----------------- main line ---------------------
error=false

case $1 in
	-v)	verbose=true;;
	*)	Usage;;
esac

init
checkroot
mkincludeargs

if $error; then
   exit 1
fi

if [ -d $destdir ]; then
   confirm
fi

echo Removing old standalone directory tree $destdir
rm -rf $destdir

echo Creating new standalone directory tree $destdir
mkdir $destdir
(cd /;tar cf - $includeargs)|(cd $destdir;tar xpf -)

echo Fixing directory permissions on directory tree $destdir
find $destdir -type d -exec chmod ug=rwx,o=rx {} \;
find $destdir -type d -exec chgrp staff {} \;
