#! /bin/sh

if [ "$1" = "beta" ]
	then
		BETA=1
		echo "Building BETA distribution for bcnu"
		echo
fi

VERSION=`cat /usr/local/bcnu/etc/bcnu_version`

# This script will build a distribution for bcnu 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

cd $BCNUHOME

. $BCNUHOME/etc/bcnuenv

echo "Creating temporary structure in /tmp"
if [ -d /tmp/bcnu ]
	then
		rm -rf /tmp/bcnu
fi
mkdir /tmp/bcnu

echo "Creating distribution.."
cd $BCNUHOME
find . -depth -print | \
sed "\+^./data/+d" | \
sed "\+^./retry/+d" | \
sed "\+^./logs/+d" | \
sed "\+^./flags/+d" | \
sed "\+^./clients/+d" | \
sed "\+^./work/+d" | \
sed "\+.old+d" | \
cpio -o >/tmp/bcnu.cpio

cd /tmp/bcnu
echo "Building.."
cpio -icld </tmp/bcnu.cpio
rm /tmp/bcnu.cpio

printf "."

rm /tmp/bcnu/bcnuclient.tar.Z 2>/dev/null

#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

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

cd /tmp/bcnu/src
make clean >/dev/null

printf "."

rm -r /tmp/bcnu/bin
mkdir /tmp/bcnu/bin

rm -r /tmp/bcnu/clients
rm -r /tmp/bcnu/docs/sample
rm -r /tmp/bcnu/docs/docsrc
rm -r /tmp/bcnu/docs/dwnld
rm -r /tmp/bcnu/old* 2>/dev/null

printf "."

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

cd /tmp/bcnu/www
rm *.htm  2>/dev/null
rm hosts/* 2>/dev/null
cp $BCNUHOME/www/index.html .
ln -s index.html index.htm

printf "."

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

if [ -z "$BETA" ]
then
	echo "Creating tar archive [/tmp/bcnu-$VERSION.tgz] for distribution"
	cd /tmp
	tar FFcf bcnu-$VERSION.tar bcnu/*
	gzip bcnu-$VERSION.tar
	mv bcnu-$VERSION.tar.gz bcnu-$VERSION.tgz
	printf "."

	# remove binaries for src dist - leave sample agents behind
	cd /tmp/bcnu/platform && rm bcnu*
	
	echo "Creating tar archive [/tmp/bcnusrc-$VERSION.tgz] for distribution"
	cd /tmp
	tar FFcf bcnusrc-$VERSION.tar bcnu/*
	gzip bcnusrc-$VERSION.tar
	mv bcnusrc-$VERSION.tar.gz bcnusrc-$VERSION.tgz

	echo "\n\nDistribution for Version $VERSION complete\n"
else
	# remove binaries for src dist - leave sample agents behind
	cd /tmp/bcnu/platform && rm bcnu*
	
	echo "Creating tar archive [$BCNUHOME/docs/dwnld/bcnubeta-$VERSION.tgz] for distribution"
	cd /tmp
	tar FFcf bcnubeta-$VERSION.tar bcnu/*
	gzip bcnubeta-$VERSION.tar
	mv bcnubeta-$VERSION.tar.gz $BCNUHOME/docs/dwnld/bcnubeta-$VERSION.tgz

	echo "\n\nBeta Distribution for Version $VERSION complete\n"

fi
