#!/bin/sh
#	build_henryg - like build_standalone but just for me
#
#
# Shell script to copy all henryg files for stand alone operation
#
# 28 Mar 98  Henry Grebler	Initial version (from build_standalone)

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

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

		-v	verbose
EOF
	exit 1
}

# --------------------- init ----------------------
init() {
host="`uname -n`"


	cat <<EOF


Editing abandoned - I don't need to change anything.

I just need to 

create $HOME/standalone.$host.config

then:

I'l change the default for this next bit.

(export CONFIG_DIR=$HOME; build_standalone)

YES I DO - I need to avoid deleting the previous tree!

EOF

# Allow user to set directory as envar

#config=${CONFIG_DIR-/work/sysadmin/standalone}/standalone.$host.config

CONFIG_DIRS="$CONFIG_DIR $HOME /work/sysadmin/standalone"


	for dir in $CONFIG_DIRS
	do
		if [ -f "$dir/standalone.$host.config" ]
		then
			config=$dir/standalone.$host.config
			cat <<EOF
Using '$dir' as config dir and
'$dir/standalone.$host.config' as the config file.
EOF
			break
		fi
	done

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

   cat <<EOF
WARNING: configuration file $config does not exist

Create it as follows:

   cp /work/sysadmin/standalone/standalone.template.config $config
   chgrp staff $config
   chmod ug=rwx,o= $config

then edit $config before continuing.

EOF

   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
}

# --------------------------------------
confirm_add() {
echo About to add to 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


if [ $# -ne 0 ]
then
	case $1 in
		-v)	verbose=true;;
		*)	Usage;;
	esac
fi

init
#checkroot
mkincludeargs

if $error; then
   exit 1
fi

########### if [ -d $destdir ]; then
###########    confirm
########### fi

if [ -d $destdir ]; then
   confirm_add
fi

echo NOT 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 -)

if [ "`whoami`" = "root" ]; then
	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 {} \;
else
	cat <<EOF

You might want to fix directory permissions on directory tree $destdir
find $destdir -type d -exec chmod ug=rwx,o=rx {} \;
find $destdir -type d -exec chgrp staff {} \;

EOF

fi
	echo Done.
