#  empdebuild : test suite for an emdebian version of pdebuild.
#
#  Emdebian chroot builder - initially supporting the creation of a
#  chroot capable of running emdebian-tools to reduce the number of
#  cross dependencies that need to be installed on the build system.
#
#  Copyright (C) 2006, 2007  Neil Williams <codehelp@debian.org>
#
#  This package 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 3 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, see <http://www.gnu.org/licenses/>.
#

. /usr/lib/pbuilder/pbuilder-modules

OURVERSION=`perl -e 'use Emdebian::Tools; print &tools_version();'`

ARCH=`perl -e 'use Debian::DpkgCross; use Cache::Apt::Lookup; use Cache::Apt::Config; \
&read_config(); \
my $arch = &get_architecture(); \
echo (qq/No default architecture. Please use --arch ARCH\n/) if (! defined(&check_arch("$arch"))); \
print $arch;';`

WORKDIR=`perl -e 'use Cwd; use Emdebian::Tools; use Config::Auto; use Debian::DpkgCross; \
&read_config; \
my $w = &get_workdir; \
$w = cwd if (! -d $w); \
$w =~ s/\/$//; \
print $w;';`
WORKPLACE="${WORKDIR}/pbuilder"
BASETGZ="${WORKDIR}/emdebian.tgz"

#pbuilder base values
DEBIAN_BUILDARCH=$ARCH
# cross-building chroot is same arch as host.
BUILDRESULT="$WORKPLACE/result/"
# tidy up // to /
BUILDRESULT=`echo $BUILDRESULT | tr -s \/`
APTCACHE="$WORKPLACE/aptcache/"
# tidy up // to /
APTCACHE=`echo $APTCACHE | tr -s \/`
AUTO_DEBSIGN=yes
APTCACHEHARDLINK="no"
BUILDPLACE="$WORKPLACE/build"

# the default is to add a PID in the buildplace.
BASEBUILDPLACE="$BUILDPLACE"
if [ "${PRESERVE_BUILDPLACE}" != "yes" ]; then
    BUILDPLACE="$BUILDPLACE/$$"
fi

function checkarch ()
{
	ARCH=$ARCH perl -e 'use Debian::DpkgCross; $arch = $ENV{ARCH}; \
		die "Unsupported architecture: $arch\n" if (not defined (&check_arch($arch)));'
}

function extractembuildplace () {
    # after calling this function, umountproc, and cleanbuildplace
    # needs to be called. Please trap it.
    if [ ! \( "${PRESERVE_BUILDPLACE}" = "yes" -a -d "$BUILDPLACE" \) ]; then
		cleanbuildplace
		echo "Building the build Environment"
		if ! mkdir -p "$BUILDPLACE"; then
		    echo "E: failed to build the directory to chroot"
		    exit 1
		fi
		echo " -> extracting base tarball [${BASETGZ}]"
		if [ ! -f "$BASETGZ" ]; then
		    echo "E: failed to find $BASETGZ, have you created your base tarball yet?"
		    exit 1
		fi
		if ! (cd "$BUILDPLACE" && tar xfzp "$BASETGZ"); then
		    echo "E: failed to extract $BASETGZ to $BUILDPLACE"
		    exit 1
		fi
		echo " -> creating local configuration"
		if [ $CROSS -a $CROSS != $ARCH ]; then
			hostname -f > "$BUILDPLACE/etc/mailname"
		else
			echo "emdebian-$ARCH" > "$BUILDPLACE/etc/mailname"
		fi
    fi
    copy_local_configuration
    mountproc
    mkdir -p "$BUILDPLACE/tmp/buildd"

    if [ "$OVERRIDE_APTLINES" = "yes" ]; then
		installaptlines
    fi
}

function create_emdebiantgz() {
    # create base.tgz
    (
	if ! cd "$BUILDPLACE"; then
	    echo "Error: unexpected error in chdir to $BUILDPLACE" >&2
	    exit 1;
	fi
	while test -f "${BASETGZ}.tmp"; do
	    echo "  -> Someone else has lock over ${BASETGZ}.tmp, waiting"
	    sleep 10s
	done
	echo " -> creating base tarball [${BASETGZ}]"
	if ! sudo tar cfz "${BASETGZ}.tmp" * ; then
	    echo " -> failed building base tarball"
	    rm -f "${BASETGZ}.tmp"
	    exit 1;
	fi
	mv "${BASETGZ}.tmp" "${BASETGZ}"
    )
}

function check_dirs()
{
	if [ ! -d $BUILDPLACE ] ; then
		mkdir -p $BUILDPLACE
	fi
	if [ ! -d $BUILDRESULT ] ; then
		mkdir -p $BUILDRESULT
	fi
	if [ ! -d $APTCACHE ] ; then
		mkdir -p $APTCACHE
	fi
}

function copy_host_configuration () {
    echo " -> copying local configuration"
    if [ $CROSS -a $CROSS != $ARCH ]; then
	    for a in hosts hostname resolv.conf; do
			sudo rm -f "$BUILDPLACE/etc/$a"
			if [ ! -f "/etc/$a" ]; then
			    echo "E: /etc/$a does not exist, your setup is insane. fix it" >&2
			sudo cp $( readlink -f "/etc/$a" ) "$BUILDPLACE/etc/$a";
			fi
		done
	else
	# sandboxes need a different hostname
	mkdir -p "$BUILDPLACE"/etc/
	cat > "$BUILDPLACE"/etc/hostname << EOF
emdebian-$ARCH
EOF
    fi
}

