#!/bin/sh
# $NetBSD: pre-build,v 1.25 2002/11/03 22:59:21 hubertf Exp $
#
# Clean up system to be ready for bulk pkg build
#
# (c) 2000 Hubert Feyrer, All Rights Reserved.
#

#set -v		# Debug

# Pull in USR_PKGSRC, CVS_USER:
if [ -f "$BULK_BUILD_CONF" ]; then
    . $BULK_BUILD_CONF
else
    . `dirname $0`/build.conf
fi

PRUNEDISTFILES=${PRUNEDISTFILES:-"no"}
PRUNEPACKAGES=${PRUNEPACKAGES:-"no"}
PRUNELINKS=${PRUNEPACKAGES:-"no"}

PKGLINT_PKG_DIR=${USR_PKGSRC}/pkgtools/pkglint

# extract the name of the files used for the build log and broken build log.
# these have defaults set by bsd.bulk-pkg.mk and may be overridden in /etc/mk.conf
export BROKENF=`( cd ${PKGLINT_PKG_DIR} ; ${BMAKE} show-var VARNAME=BROKENFILE )`;
if [ "$BROKENF" = "" ]; then
	echo "Had problems determining the name of the .broken files"
	exit 1
fi
BLDLOG=`( cd ${PKGLINT_PKG_DIR} ; ${BMAKE} show-var VARNAME=BUILDLOG )`;
if [ "$BLDLOG" = "" ]; then
	echo "Had problems determining the name of the .make files"
	exit 1
fi
STARTFILE=`( cd ${PKGLINT_PKG_DIR} ; ${BMAKE} show-var VARNAME=STARTFILE )`;
if [ "$BLDLOG" = "" ]; then
	echo "Had problems determining the name of the .start file"
	exit 1
fi



#
# Clean out everything and its mother
#
PKG_DBDIR=`(cd ${PKGLINT_PKG_DIR}; ${BMAKE} show-var VARNAME=PKG_DBDIR)` 

echo Removing all installed packages in ${PKG_DBDIR}

if [ -d ${PKG_DBDIR} ]; then
	cd ${PKG_DBDIR}
	for pkg in *
	do
		if `env PKG_DBDIR=${PKG_DBDIR} pkg_info -qe $pkg`; then 
			echo pkg_delete -r $pkg
			env PKG_DBDIR=${PKG_DBDIR} pkg_delete -r $pkg
		fi
	done
	# this should have removed everything.  Now force any broken pkgs
	# to deinstall
	for pkg in *
	do
		if `env PKG_DBDIR=${PKG_DBDIR} pkg_info -qe $pkg`; then 
			echo pkg_delete -f $pkg
			env PKG_DBDIR=${PKG_DBDIR} pkg_delete -f $pkg
		fi
	done
	
	# We've tried our best to get rid of the pkgs, now do it the hard way
	# If it wasn't for stuff in $X11BASE, I wouldn't have hard feelings
	# about this!
	rm -fr *
fi

LOCALBASE=`( cd ${PKGLINT_PKG_DIR} ; ${BMAKE} show-var VARNAME=LOCALBASE )`;
X11BASE=`( cd ${PKGLINT_PKG_DIR} ; ${BMAKE} show-var VARNAME=X11BASE )`;
	
# General cleanout - easy!
cd $LOCALBASE && rm -fr * && rm -fr .??*

# Stuff known to be broken
rm -fr $X11BASE/share/gimp
rm -fr $X11BASE/share/gnome
rm -fr $X11BASE/share/kde
rm -fr $X11BASE/share/netscape
rm -fr /var/tmp/inst*
rm -fr $X11BASE/lib/libgimp*	# gimp doesn't build with old libs around
rm -fr /nonexistent		# broken useradd on pop3d
rm -fr /home/majordom		# mail/majordomo pkg
rm -fr /home/nut		# sysutils/ups-net
rm -fr /var/wwwoffle		# www/wwwoffle


# Clean up state files
cd ${USR_PKGSRC}
rm -f $BROKENF */*/$BROKENF
rm -f $BLDLOG */*/$BLDLOG
rm -f $STARTFILE




#
# Install cvs package and do a cvs update here
#
if [ "$CVS_USER" != "" ]; then
	if [ ! -f /usr/bin/cvs ]; then
		echo Installing required cvs pkgs for CVS update
		( cd ${USR_PKGSRC}/devel/cvs ; ${BMAKE} bulk-install )
	fi
	if [ ! -f /usr/bin/ssh ]; then
		echo Installing required ssh pkgs for CVS update
		( cd ${USR_PKGSRC}/security/ssh ; ${BMAKE} bulk-install )
	fi
	echo Performing CVS update - this will take some time
	su - ${CVS_USER} -c 'stty sane ; setenv CVS_RSH ssh ; cd '${USR_PKGSRC}' ; cvs -q update -Pd '"${CVS_FLAGS}"
	echo CVS update done.
fi


#
# Remove old/broken distfiles and binary packages
#
DISTDIR=`( cd ${PKGLINT_PKG_DIR} ; ${BMAKE} show-var VARNAME=DISTDIR )`;
PACKAGES=`( cd ${PKGLINT_PKG_DIR} ; ${BMAKE} show-var VARNAME=PACKAGES )`;

case "$PRUNEDISTFILES" in
yes|YES)
	echo "Removing old/broken distfiles"
	( cd ${PKGLINT_PKG_DIR} ; ${BMAKE} PRECLEAN=yes bulk-install )
	lintpkgsrc -K $PACKAGES -P $USR_PKGSRC -M $DISTDIR -o -m -r
	echo done.
	;;
*)
	echo "Skipping distfile pruning."
	;;
esac

case "$PRUNEPACKAGES" in
yes|YES)
	echo "Removing old (out of date) binary packages"
	( cd ${PKGLINT_PKG_DIR} ; ${BMAKE} PRECLEAN=yes bulk-install )
	lintpkgsrc -K $PACKAGES -P $USR_PKGSRC -M $DISTDIR -p -r
	echo done.
	;;
*)
	echo "Skipping packages pruning."
	;;
esac

case "$PRUNELINKS" in
yes|YES)
	echo "Checking for and removing orphaned packages links"
	find $PACKAGES -type l -print |  \
	while read f 
	do
		if [ ! -d $f -a ! -f $f ]; then
			echo "Removing orphaned link:  \"$f\""
			rm $f
		fi
	done
	echo done.
	;;
*)
	echo "Skipping pruning of packages links."
	;;
esac


if [ -f mk/bulk/pre-build.local ]; then
	. mk/bulk/pre-build.local
fi

mkdir ${PKG_DBDIR}

touch $STARTFILE

