#!/bin/sh

pwd=`pwd`
DESTDIR=$pwd/tmp
rm -rf $DESTDIR
mkdir -p $DESTDIR

dir=clip-prg
#cd $dir || exit 2

if [ -f Makefile.in -a -x ./configure ]
then
	./configure
fi

cleanup()
{
	cd $pwd
	rm -rf $DESTDIR
	exit $1
}


# 	cumulative install
echo "cumulative install"
$MAKE || cleanup 1
$MAKE install DESTDIR="" || cleanup 1
#$MAKE install || cleanup 1
#	package install
mkdir -p ${DESTDIR}/${SCLIPROOT}/lib \
	 ${DESTDIR}/${SCLIPROOT}/doc \
	 ${DESTDIR}/${SCLIPROOT}/include \
	 ${DESTDIR}/${SCLIPROOT}/bin \
	 ${DESTDIR}/${SCLIPROOT}/cliprc \
	 ${DESTDIR}/${SCLIPROOT}/etc \
	 ${DESTDIR}/${SCLIPROOT}/charsets \
	 ${DESTDIR}/${SCLIPROOT}/keymaps \
	 ${DESTDIR}/${SCLIPROOT}/locale.pot
cp ${CLIPROOT}/include/clipcfg.sh ${CLIPROOT}/include/Makefile.inc ${DESTDIR}/${SCLIPROOT}/include/
cp ${CLIPROOT}/bin/clip_msgfmt ${CLIPROOT}/bin/clip_msgmerge ${DESTDIR}/${SCLIPROOT}/bin/
cp ${CLIPROOT}/bin/clip_cp ${DESTDIR}/${SCLIPROOT}/bin
echo "install to ${DESTDIR}${SCLIPROOT}"
$MAKE install DESTDIR="" CLIPROOT=${DESTDIR}/${SCLIPROOT} || cleanup 2
rm ${DESTDIR}/${SCLIPROOT}/include/clipcfg.sh ${DESTDIR}/${SCLIPROOT}/include/Makefile.inc
rm ${DESTDIR}/${SCLIPROOT}/bin/clip_msgfmt ${DESTDIR}/${SCLIPROOT}/bin/clip_msgmerge
rm ${DESTDIR}/${SCLIPROOT}/bin/clip_cp

cd $pwd

mkdir -p ${DISTROOT}
cd ${DESTDIR}

rmempty()
{
	for i in $*
	do
		rmdir $i 2>/dev/null && continue
		(cd $i 2>/dev/null && rmempty *)
	done
}

rm -rf `find . -path '*CVS*'`
rmempty *

echo "creating archive:"
echo "tar cvf - * | $COMPRESS_PRG  > ${DISTROOT}/${dir}_${VER}.tar.$COMPRESS_SUFF"
tar cvf - * | $COMPRESS_PRG  > ${DISTROOT}/${dir}_${VER}.tar.$COMPRESS_SUFF

cleanup 0
