#! /bin/sh

# mkdeb.sh 0.42.1
#
# Quick hack to make a Debian package out of crosstool output
#
# Usage: mkdeb.sh <dir>
# where
# <dir> is the top directory of the crosstool output
# e.g. /opt/crosstool/gcc-4.1.0-glibc-2.4/arm-none-linux-gnueabi
# The output will be written into
# e.g. arm-none-linux-gnueabi-gcc-4.1.0-glibc-2.4-crosstool_0.42-1_i686.tgz
#
# Uses mktarball.sh
#
#	Martin Guy <martinwguy@yahoo.it>, 21 May 2006

#
# Preliminary stuff
#

crosstoolversion=0.42	# Sigh

progname=`basename "$0"`

usage () {
	echo "Usage: $progname crosstool-output-dir-root" 1>&2
	exit 1
}

# Check that some prerequisites are installed
for a in fakeroot alien
do
	which $a > /dev/null || {
		echo "$progname: To make .deb packages you must install \"$a\"."
		exit 1
	}
done

sourcedir=

#
# Process arguments
#

for a
do
	case "$a" in
	-*)	# There are no flags
		usage
		;;
	*)	if [ -z "$sourcedir" ]; then
			# First arg is source dir
			sourcedir="$a"
		else
			usage
		fi
		;;
	esac
done

# Check they gave the required args
[ -z "$sourcedir" ] && usage

# and that the source dir exists
[ ! -d $sourcedir ] && {
	echo "$0: \"$sourcedir\" does not exist." 1>&2
	exit 1
}

# Debian filename must be package_version-debversion_arch.deb
# For "package" we use gnuarchname-gccglibcversion
# For "version" we use the crosstool version it was built with
# "debversion" is set by "alien -k" as "-1"
# "arch" is the arch the compiler runs on (alien chooses "all" which is wrong)
# We get these from the sourcedir, where the last pathname component is gnuarch
# and the penultimate is gccglibc
# We allow for .../gcc-glibc/gnuarch as well as gcc-glibc/gnu-arch
case "$sourcedir" in
*/*/*)
	gcclibc=`echo "$sourcedir" | sed 's:.*/\([^/]*\)/\([^/]*\)$:\1:'`
	gnuarch=`echo "$sourcedir" | sed 's:.*/\([^/]*\)/\([^/]*\)$:\2:'`
	;;
*/*)
	gcclibc=`echo "$sourcedir" | sed 's:\([^/]*\)/\([^/]*\)$:\1:'`
	gnuarch=`echo "$sourcedir" | sed 's:\([^/]*\)/\([^/]*\)$:\2:'`
	;;
*)
	echo "$progname: source directory must be of the form */gnu-arch-name/gcc-glibc-version" 1>&1
	exit 1
esac
# Sanity check
if [ -z "$gcclibc" -o -z "$gnuarch" ]; then
	echo "$progname: Failed to split input pathname into gccglibc and gnuarch components."
	exit 1
fi

targetfile="$gnuarch-$gcclibc-crosstool-$crosstoolversion.tgz"

sh mktarball.sh "$sourcedir" "$targetfile" || exit 1

alienoutput=`fakeroot alien --to-deb -k --description="cross compiler generated by crosstool" "$targetfile"`
if [ $? != 0 ]; then
	echo "$progname: $targetfile built ok but failed to make .deb file." 1>&2
	exit 1
fi

# Ok! remove the temporary .tgz file
rm "$targetfile"

# alien creates *-1_all.deb and says "<file> generated".
# We rename it as an arch-dependent package
alienfile=`echo "$alienoutput" | sed 's/ generated//'`
if [ -z "$alienfile" -o ! -f "$alienfile" ]; then
	echo "$progname: I can't spot the .deb output file from alien!  Alien said:" 1>&2
	echo "$progname: \"$alienoutput\"" 1>&2
	exit 1
fi

arch=`arch`	# or `uname -m` is the same
debfile=`echo "$alienfile" | sed "s/_all/_$arch/"`

mv "$alienfile" "$debfile" || exit 1

exit 0
