#! /bin/sh


# This script will build a client directory tree

echo "Enter directory name to get files from:[/usr/local/bcnu] \c"
read home
if [ -z "$home" ]
	then
		BCNUHOME="/usr/local/bcnu"
	else
		BCNUHOME=$home
fi

CLIENTBUILD="$BCNUHOME/bcnuclient.tar"

cd $BCNUHOME

. $BCNUHOME/etc/bcnuenv

echo "Creating temporary structure in /tmp"
if [ -d /tmp/bcnu ]
	then
		rm -rf /tmp/bcnu
fi
mkdir /tmp/bcnu
cp -r $BCNUHOME/etc 	/tmp/bcnu
cp -r $BCNUHOME/scripts /tmp/bcnu
cp -r $BCNUHOME/agent 	/tmp/bcnu
cp -r $BCNUHOME/platform 	/tmp/bcnu
cp -r $BCNUHOME/contrib 	/tmp/bcnu
cp -r $BCNUHOME/custom 	/tmp/bcnu

echo "Do you wish to copy source,cgi scripts and documentation to client:[no] \c"
read yorn
if [ "y" = "$yorn" -o "yes" = "$yorn" ]
	then
		cp -r $BCNUHOME/docs 	/tmp/bcnu
		rm -r /tmp/bcnu/docs/sample
		rm -r /tmp/bcnu/docs/docsrc
		cp -r $BCNUHOME/src	/tmp/bcnu
		cp -r $BCNUHOME/cgi-bin	/tmp/bcnu

		cd /tmp/bcnu/src
		make clean >/dev/null
fi
sed "s/^/#/g" /tmp/bcnu/etc/agents  >/tmp/bcnu/etc/agents.distrib
sed "s/^/#/g" /tmp/bcnu/etc/bcnunet >/tmp/bcnu/etc/bcnunet.distrib
sed "2,\$s/^/#/g" /tmp/bcnu/etc/bcnulocal >/tmp/bcnu/etc/bcnulocal.distrib

mv /tmp/bcnu/etc/bcnuenv /tmp/bcnu/etc/bcnuenv.distrib
mv /tmp/bcnu/etc/bcnud_server /tmp/bcnu/etc/bcnud_server.distrib
mv /tmp/bcnu/etc/logpatterns /tmp/bcnu/etc/logpatterns.distrib
mv /tmp/bcnu/etc/services /tmp/bcnu/etc/services.distrib
mv /tmp/bcnu/etc/hostinfo /tmp/bcnu/etc/hostinfo.distrib

cd /tmp/bcnu
ln -s scripts/client_setup Setup

cd /tmp/bcnu/etc
rm agents bcnunet bcnud.pid bcnulocal

cd /tmp/bcnu/platform
rm bcnu bcnud bcnumsg 2>/dev/null
chmod 700 bcnu*

mkdir /tmp/bcnu/bin
mkdir /tmp/bcnu/data
mkdir /tmp/bcnu/data/hosts
mkdir /tmp/bcnu/data/agents
mkdir /tmp/bcnu/data/files
chmod 777 /tmp/bcnu/data/files
mkdir /tmp/bcnu/logs
chmod 777 /tmp/bcnu/logs
mkdir /tmp/bcnu/retry
chmod 777 /tmp/bcnu/retry
mkdir /tmp/bcnu/flags
chmod 777 /tmp/bcnu/flags
mkdir /tmp/bcnu/www
mkdir /tmp/bcnu/www/hosts
mkdir /tmp/bcnu/clients

echo "Creating tar archive [${CLIENTBUILD}.Z] for distribution"
cd /tmp
tar FFcf ${CLIENTBUILD} bcnu/*
rm ${CLIENTBUILD}.Z 2>/dev/null
compress ${CLIENTBUILD}
echo "\n\nClient build complete\n"
echo "If Installing: Copy ${CLIENTBUILD}.Z file to client"
echo "cd to the directory where bcnu will be installed and untar it"
echo "\n\tcd /usr/local;zcat bcnuclient.tar.Z | tar xf -\n"
echo "Then run\n\tcd /usr/local/bcnu;./Setup"
echo "If Upgrading: Copy scripts/upgrade_bcnu file to client and run"
