#!/bin/sh
set -e

#  Copyright (C) 2009  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/>.

PROG=`basename $0`

usagehelp () {
	cat <<EOF
$PROG - build wrapper script for Emdebian Grip

Syntax: $PROG [COMMAND] [DPKG-OPTIONS]

Commands:
   --debuild:               use debuild instead of dpkg-buildpackage
-a|--arch ARCH:             cross-build using dpkg-buildpackage -a
-n|--dry-run:               print the commands but do not build
-V|--vendor VENDOR:         DEB_VENDOR support.

-?|-h|--help|-version:      print this help message and exit

Simple wrapper for dpkg-buildpackage that runs the built package
through emgrip. This results in both types of packages existing in
the parent directory.

The DEB_VENDOR environment variable is supported as well as the
-V|--vendor command line option.

DPKG-OPTIONS

Any unrecognised options are passed directly to dpkg-buildpackage,
there is no need for '--' .

EOF
}

CROSS_OPTS="nocheck"
DBO=$DEB_BUILD_OPTIONS
OPTS=""
VENDOR=$DEB_VENDOR
CMD="dpkg-buildpackage"
TMPDIR="/tmp/"
DRY=""
DCMD="mv"
while [ -n "$1" ]; do
case "$1" in
	--debuild)
		CMD="debuild"
		shift
	;;
	-a|--arch)
		shift
		ARCH=$1
		shift
	;;
	-n|--dry-run)
		DRY="echo "
		DCMD="ls"
		TMPDIR="../"
		shift
	;;
	-V|--vendor)
		shift
		VENDOR=$1
		shift
	;;
	--help|-h|-?)
		usagehelp
		exit;
	;;
	*)
		OPTS="$OPTS $1"
		shift
		ARCH=`echo $OPTS|sed -e 's/-a\(.*\)/\1/'`
		OPTS=`echo $OPTS|sed -e 's/-a\(.*\)//'`
	;;
esac
done

if [ ! -f debian/control ]; then
	echo "Unable to find debian/control."
	exit 1
fi
SRC=`grep Source: debian/control|cut -d: -f2|tr -d ' '`
VER=
if [ -x "/usr/bin/parsechangelog" ]; then
	VER=`parsechangelog|grep "^Version: "|cut -d: -f2|tr -d ' '`
else
	VER=`dcontrol ${SRC}/:sid@source|grep "^Version:"|cut -d: -f2|tr -d ' '`
	echo "dcontrol gives version as ${VER} - checking..."
	echo "If this fails, install libparse-debianchangelog-perl"
	if [ -n "$VER" ]; then
		grep $VER debian/changelog
	fi
fi
if [ -z "$VER" ]; then
	echo "ERROR: Unable to get the version string."
	exit 4
fi

if [ -z "$VENDOR" ]; then
	echo "ERROR: Failed to parse DEB_VENDOR name."
	echo "Either set DEB_VENDOR or pass the -V option."
	exit 7
fi
BUILD_ARCH=`dpkg-architecture -qDEB_BUILD_ARCH_CPU`
echo "Checking vendor settings for $VENDOR"
DBO="$DBO "
DBOV=`dpkg-vendor --vendor $VENDOR --query grip-build-option 2>/dev/null || true`
DBO="$DBO $DBOV"
NAME=`dpkg-vendor --vendor $VENDOR --query vendor-name || true`
if [ -z "$NAME" -o -z "$DBO" ]; then
	echo "ERROR: $VENDOR did not set a vendor-name or grip-build-option."
	exit 9
fi
AROPT=""
if [ -z "${ARCH}" ]; then
	ARCH=${BUILD_ARCH}
else
	AROPT="-a${ARCH}"
	DBO="${DBO} ${CROSS_OPTS}"
fi
echo "Using '$DBO' for $VENDOR on $ARCH"
export DEB_BUILD_OPTIONS="$DBO"
echo DEB_BUILD_OPTIONS=\"$DBO\"
$DRY $CMD -uc -us $AROPT $OPTS

if [ ! -f "../${SRC}_${VER}.dsc" ]; then
	echo "Unable to find ../${SRC}_${VER}.dsc - failed to build?"
	exit 2
fi
if [ -z $TEMPDIR ]; then
	DIR=/tmp
else
	DIR=$TEMPDIR
fi

export DEB_BUILD_OPTIONS="$DBO"
$DRY emgrip -o ${DIR} ../${SRC}_${VER}.dsc

if [ -f "${DIR}/${SRC}_${VER}_${ARCH}.changes" ];then
	$DRY dcmd $DCMD ${DIR}/${SRC}_${VER}_${ARCH}.changes ../
fi

if [ -f "${DIR}/${SRC}_${VER}em1+t1_${ARCH}.changes" ];then
	$DRY dcmd $DCMD ${DIR}/${SRC}_${VER}em1+t1_${ARCH}.changes ../
fi
