#! /bin/bash
# bash is necessary to use lib{iberty,gomp} syntax; dash doesn't have this.

# Make dist tarball of installed compiler
#
# Usage: tarball gcc-4.3-crunch
# usually just after running "../s/install"
#
# Makes (e.g.)	../tarballs/gcc-4.3-crunch_4.3.2-20090319.tgz
# and		../tarballs/gcc-4.3.2-patches.tar.gz

set -e		# Exit if anything fails unexpectedly

cname=$1
case "$cname" in
gcc-?.?-[a-z]*) ;;
*)	echo "Usage: tarball gcc-4.3-crunch" 1>&2
	exit 1 ;;
esac

cexec=/usr/local/bin/$cname
[ -f "$cexec" -a -x "$cexec" ] || {
	echo "$cexec not found" 1>&2
	exit 1
}

# Get the build date off modification time of the gcc-4.X-crunch binary.
builddate=`stat --format=%y $cexec | sed 's/-//g;s/ .*//'`
#builddate=20130712a

lname=$($cexec -v 2>&1 | sed -n '/^Target: /s///p')
lver=$($cexec --version | sed -n '1s/.* //p')
sver=$(echo $lver | sed 's/.[0-9]$//')
suffix=$(echo $cname | sed 's/gcc-//')

# Sanity check
case "$lver" in
?.?.?)	;;
*)	exit 1 ;;
esac

# Make dist tarball
# omitting gccbug, gcov, manuals, begging letters etc.

tar czf ../packages/gcc-${suffix}_${lver}-${builddate}_armel.tgz -C / \
        usr/local/bin/{cpp,gcc}-$suffix \
        usr/local/lib/gcc/$lname/$lver \
        usr/local/libexec/gcc/$lname/$lver \
	usr/local/share/man/man1/$cname.1 \
	--exclude="usr/local/lib/gcc/$lname/$lver/libs??c++*" \
	--exclude="usr/local/libexec/gcc/$lname/$lver/cc1plus" \
	|| {
	echo "Failed making tarball".
	exit 1
}

# Slurp up the patch directory too.
# Who knows, it might even correspond to the binary!
tar czhf ../packages/gcc-$lver-crunch-patches-$builddate.tar.gz -C .. \
	gcc-$lver-patches
