#!/bin/sh

if test ! -f config.dist ; then
	echo "dist: config.dist missing!"
fi

# defaults!

distdir=$1
distlsm=$1
distlocal=''

if test -d $distdir/SOURCES ; then
	distdir=$distdir/SOURCES	
	if test -d $1/pub/local ; then
		distlocal=$1/pub/local
	fi
fi

if test -d $1/pub/lsm ; then
	distlsm=$1/pub/lsm
fi

config="config.dist config.pkg build"
exclude="*.o *.a config.cc config.cache config *.tar.gz *~ core Makefile"
exclude_exe="true"
exclude_files=".EXCLUDE"

. ./config.dist

root=`pwd`
dist=$package'-'$version
cd ..

if test ! $dist = $root ; then
	rm -f $dist
	ln -s $root $dist
fi

xlist=$dist/.EXCLUDE
echo -n >$xlist
for file in $exclude_files ; do
	echo $dist/$file >>$xlist
done

flist=""

for cfg in $include $config ; do
	flist=$flist' '$dist/$cfg
done

for dir in $examine ; do
	flist=$flist' '$dist/$dir
	if test $exclude_exe = "true" ; then
		find $dist/$dir -perm +1 -type f -follow -print >>$xlist
	fi
	for deny in $exclude ; do
		find $dist/$dir -follow -name $deny -print >>$xlist
	done
done

target=$dist/$dist.tar.gz
if test -d $distdir ; then
	rm -f $distdir/${package}*.tar.gz
	target=$distdir/$dist.tar.gz
	if test ! -z $distlocal ; then
		rm -f $distlocal/${package}*.tar.gz
	fi
	if test -f $dist/$package'.lsm' ; then
		cp -f $dist/$package'.lsm' $distlsm
	fi
fi

tar -X $xlist -cvhzf $target $flist


if [ ! $dist = $root ] ; then
	rm $dist
fi

if test ! -z $distlocal ; then
	cd $distlocal
	ln -sf ../../SOURCES/$dist.tar.gz .
fi

