#! /bin/bash
#   pbuilder -- personal Debian package builder
#   Copyright (C) 2001-2006 Junichi Uekawa
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

# pbuilder by Junichi Uekawa <dancer@debian.org> 2001 Aug 25

set -e 

# export this so that programs know which command line
# operation is currently done
export PBCURRENTCOMMANDLINEOPERATION="$1"
umask 0022

case "$1" in
    --create|create)
	shift;
	/usr/lib/pbuilder/pbuilder-createbuildenv "$@"
	;;
    --update|update)
	shift ;
	/usr/lib/pbuilder/pbuilder-updatebuildenv "$@"
	;;
    --build|build)
	shift ;
	/usr/lib/pbuilder/pbuilder-buildpackage "$@"
	;;
    --clean|clean)
	shift;
	. /usr/lib/pbuilder/pbuilder-checkparams
	if [ -n "$BASEBUILDPLACE" ]; then 
	    echo "Cleaning [$BASEBUILDPLACE]"
	    clean_subdirectories "$BASEBUILDPLACE" || true
	fi
	if [ -n "$APTCACHE" ]; then
	    echo "Cleaning [$APTCACHE]"
	    clean_subdirectories "$APTCACHE" || true
	fi
	;;
    --login|login)
	shift 
	. /usr/lib/pbuilder/pbuilder-checkparams
	. /usr/lib/pbuilder/pbuilder-runhooks
	extractbuildplace
	trap umountproc_cleanbuildplace exit
	loadhooks
	recover_aptcache
	trap saveaptcache_umountproc_cleanbuildplace exit

	if [ "${INTERNAL_BUILD_UML}" = "yes" ]; then
	    echo " -> entering the shell"
	else
	    echo " -> entering the shell
File extracted to: $BUILDPLACE
"
	fi
	executehooks "F"
	${CHROOTEXEC} bin/bash

	save_aptcache

	trap umountproc_cleanbuildplace exit

	# saving the place afterwards
	if [ "${SAVE_AFTER_LOGIN}" = "yes" ]; then
	    if [ "${INTERNAL_BUILD_UML}" = "yes" ]; then
		echo "E: Invalid combination of internal-build-uml and save-after-login flags"
	    else
		echo " -> Saving the results, modifications to this session will persist"
		unloadhooks
		umountproc
		trap cleanbuildplace exit
		${CHROOTEXEC} /usr/bin/apt-get clean || true
		create_basetgz
	    fi
	fi
	;;
    --execute|execute)
	# try to execute arbitrary program.
	shift
	. /usr/lib/pbuilder/pbuilder-checkparams
	. /usr/lib/pbuilder/pbuilder-runhooks
	EXECPROGRAM="$1"
	shift
	if [ ! -f "${EXECPROGRAM}" ]; then
	    echo "Command line parameter [$EXECPROGRAM] does not exist" >&2
	    exit 1;
	fi;
	extractbuildplace

	trap umountproc_cleanbuildplace exit

	loadhooks
	recover_aptcache
	trap saveaptcache_umountproc_cleanbuildplace exit

	RUNNAME="$BUILDPLACE/run"
	cat "$EXECPROGRAM" > "$RUNNAME"
	chmod a+x "$RUNNAME"
	executehooks "F"
	${CHROOTEXEC} /run "$@"

	save_aptcache
	trap umountproc_cleanbuildplace exit

	# saving the place afterwards
	if [ "${SAVE_AFTER_LOGIN}" = "yes" ]; then
	    if [ "${INTERNAL_BUILD_UML}" = "yes" ]; then
		echo "E: Invalid combination of internal-build-uml and save-after-login flags"
	    else
		echo " -> Saving the results, modifications to this session will persist"
		unloadhooks
		umountproc
		trap cleanbuildplace exit
		${CHROOTEXEC} /usr/bin/apt-get clean || true
		create_basetgz
	    fi
	fi
	;;
    --dumpconfig|dumpconfig)
	shift
	. /usr/lib/pbuilder/pbuilder-checkparams
	. /usr/lib/pbuilder/pbuilder-runhooks
	extractbuildplace
	trap umountproc_cleanbuildplace exit
	loadhooks
	executehooks "F"
	echo " -> start dump config"
	echo "  -> set"
	set;
	echo "  -> env"
	env;
	echo " -> end dump config"
	;;
    --debuild|debuild)
	shift
	"$0" execute "$@" /usr/lib/pbuilder/pdebuild-internal
	;;
    *)
	. /usr/lib/pbuilder/pbuilder-modules
	showhelp
	;;
esac

