#!/bin/sh


for opt in $*
do
	case $opt in
	-noclip)
		noclip=yes
		;;
	-noprg)
		noprg=yes
		;;
	-noclean)
		noclean=yes
		;;
	-strip)
		dostrip=yes
		;;
	-clip)
		noprg=yes
		nogzip=yes
		nogd=yes
		noclean=yes
		;;
	-release)
		RFLAG='-r'
		LANG=C
				dostrip=no
				release=yes
		[ -n "$CLIPROOT" ] || CLIPROOT=/usr/local/clip
		;;
	esac
done

if [ -n "$MAKE" ]
then
	MAKE="$MAKE"
elif [ -x /usr/local/bin/gmake  ]
then
	MAKE=/usr/local/bin/gmake
elif [ -x /usr/bin/gmake ]
then
	MAKE=/usr/bin/gmake
elif [ -x /usr/bin/make ]
then
	MAKE=/usr/bin/make
else
	MAKE=make
fi

LC_ALL=C
LC_MESSAGES=C
PATH="$HOME/bin:$PATH"
if [ -z "$CLIP_NAMES" ]
then
	CLIP_NAMES=yes
		export CLIP_NAMES
fi

export MAKE LANG LC_ALL LC_MESSAGES PATH

pwd=`pwd`

if [ -z "$CLIPROOT" ]
then
	CLIPROOT=`cd ..;pwd`/cliproot
	echo "force CLIPROOT=$CLIPROOT"
else
	echo "use CLIPROOT=$CLIPROOT"
fi

if [ -z "$noclean" ]
then
	./clean.sh
fi

export CLIPROOT
#export -n DESTDIR
_DESTDIR="$DESTDIR"
unset DESTDIR
DESTDIR="$_DESTDIR"
mkdir -p ${DESTDIR}${CLIPROOT}/include
mkdir -p ${HOME}/bin

if [ -z "$noclip" ]
then

cd clip || exit 1
#	./configure ${RFLAG} ${CLIP_CONFIGURE_FLAGS}
#	$MAKE install || exit 2
	./configure ${RFLAG} ${CLIP_CONFIGURE_FLAGS} || exit 2
	$MAKE install DESTDIR=${DESTDIR}
cd $pwd

fi

#. clip/clipcfg.sh || exit 2

echo "-v0
-O
-r
-l" > ${DESTDIR}${CLIPROOT}/cliprc/clipflags

#cd cliplibs

#$MAKE install

#(
	cd cliplibs
	CLIPROOT=${DESTDIR}${CLIPROOT}
	LD_LIBRARY_PATH=${DESTDIR}/usr/lib:$LD_LIBRARY_PATH
	export CLIPROOT  LD_LIBRARY_PATH
	$MAKE
	$MAKE install DESTDIR=''
#)

cd $pwd

#if [ -z "$noprg" ]
#then

	cp -R prg/include ${DESTDIR}${CLIPROOT}/
	cp -R prg/locale.po ${DESTDIR}${CLIPROOT}/

#cd  prg || exit 1
#	$MAKE &&\
#	$MAKE install &&\
#		${CLIPROOT}/bin/clip_msgmerge &&\
#		${CLIPROOT}/bin/clip_msgfmt  || exit 3

#	(
		cd prg
#		CLIPROOT=${DESTDIR}${CLIPROOT}
#		LD_LIBRARY_PATH=${DESTDIR}/usr/lib:$LD_LIBRARY_PATH
#		export CLIPROOT  LD_LIBRARY_PATH
		$MAKE
		$MAKE install
#		${DESTDIR}${CLIPROOT}/bin/clip_msgmerge
#		${DESTDIR}${CLIPROOT}/bin/clip_msgfmt
#	)

#fi

cd $pwd
mkdir -p ${DESTDIR}${CLIPROOT}/doc
echo cp -R example ${DESTDIR}${CLIPROOT}/doc/
cp -R example ${DESTDIR}${CLIPROOT}/doc/

#if test -d tdoc
#then
#	(cd tdoc ; $MAKE install DESTDIR=${DESTDIR})
#fi

cd  ${DESTDIR}${CLIPROOT}/ || exit 1
echo	rm -rf `find . -name CVS -print`
	rm -rf `find . -name CVS -print`
cd $pwd



if [ -n "$release" ]
then
	echo mkdir -p ${DESTDIR}/usr/lib
	mkdir -p ${DESTDIR}/usr/lib

	cd ${DESTDIR}${CLIPROOT}/lib
	if [ -n "$(ls *.so)" ]
	then
		for n in *.so
		do
			echo ln -sf ${CLIPROOT}/lib/$n ${DESTDIR}/usr/lib
			rm -f ${DESTDIR}/usr/lib/$n
			ln -sf ${CLIPROOT}/lib/$n ${DESTDIR}/usr/lib
			echo done
		done
	fi
fi

#exit 0

cd $pwd
if [ -n "$dostrip" -a -z "$release" ]
then

strip ${DESTDIR}${CLIP_ROOT}/bin/*
strip ${DESTDIR}${CLIP_ROOT}/lib/*${DLLREALSUFF}

fi

