#! /bin/sh

[ -f Filelist ] || { echo "Run from WebStone directory." 1>&2; exit 1; }

umask 2
HERE=`pwd`/..
THERE=$HERE/extr/WebStone2.5
PERMFILE=$HERE/WebStone/Filelist
DISTFILE=$HERE/Dist/WS25_src.tgz
unset BUILD EXE

if [ "$1" = "-b" ]
then
	[ -n "$2" ] || { echo "usage: mkdist [ -b archname ]"; exit 1; }
	THERE=$HERE/extr/$2/WebStone2.5
fi

[ -d $HERE/Dist ] || mkdir $HERE/Dist
[ -d $HERE/extr ] || ln -s /tmp  $HERE/extr
rm -rf $THERE
mkdir -p $THERE

rm -f conf/filelist
cp conf/filelist.standard conf/filelist
sed 's/	.*//' $PERMFILE | cpio -pudm $THERE

chmod ug=rwx,o=rx `find $THERE -type d -print`

cd $THERE
cat $PERMFILE |
while read file modes
do
	chmod $modes `eval echo $file`
done

if [ "$1" = "-b" ]
then
	case $2 in
		WIN*|NT40)
			DISTFILE=$HERE/Dist/WS25NT40.tgz
			LGMDIR=WS25_NT40_LGM
			PERMFILE=$PERMFILE.nt
			(cd $THERE; pax -r -f $HERE/NTarch.tar)
			;;

		*)	DISTFILE=$HERE/Dist/WS25_$2.tgz
			LGMDIR=WS25_$2_LGM
			PERMFILE=$PERMFILE.bin
			cd $THERE/src
			[ -f Makefile ] && make clobber
			./configure
			make install
			;;
	esac
	mv $THERE/lgm $THERE/$LGMDIR
	cp $HERE/WebStone/README.DynamicWorkload $THERE/LGMDIR
	rm $THERE/$LGMDIR/keepme
	mkdir $THERE/lgm
	echo keep me > $THERE/lgm/keepme
	(cd $THERE; pax -w $LGMDIR) | gzip > $HERE/Dist/$LGMDIR.tgz
fi

cd $THERE/man
make

cd $THERE/doc

cd $THERE/..

{ sed '
s/	.*//
s/^/WebStone2.5\//' $PERMFILE; } |
pax -w |
gzip > $DISTFILE

# End
