#!/bin/sh

sourcename="simplestreams"
TEMP_D=""

fail() { echo "$@" 1>&2; exit 1; }
cleanup() {
   [ -z "$TEMP_D" ] || rm -Rf "$TEMP_D"
}

if [ "$1" = "-h" -o "$1" = "--help" ]; then
   cat <<EOF
Usage: ${0##*/}
   build a deb of from trunk directory
   any options are passed straight through to debuild

   Example:
    * ${0##*/} -us -uc

   Its not significantly different than what you'd get by modifying
   the debian/changelog to have the current revno, and then running
     debuild --no-tgz-check
EOF
exit
fi

bname=${0##*/}

start_d=$PWD
top_d=$(cd "$(dirname "${0}")"/.. && pwd)

# grab the first line in the changelog
line1=$(head -n 1 ${top_d}/debian/changelog)
# hopefully this pulls the version info there
# resulting in something like: 0.25~trunk~bzrREVNO-1
clogver_o=$(echo "$line1" | sed 's,.*(\([^)]*\)).*,\1,')

revno=$(bzr revno) || fail "failed to get revno"
clogver=$(echo "$clogver_o" | sed "s,REVNO,$revno,")

# upstream ver takes off the '-1' which is debian ver
uver=${clogver%-[0-9]}

TEMP_D=$(mktemp -d "${TMPDIR:-/tmp}/${bname}.XXXXXX")

trap cleanup EXIT

echo "building upstream version $uver, debian ver=${clogver##*-}"

dir="${sourcename}-$uver"
tarball="${sourcename}_$uver.orig.tar.gz"

bzr export --format=tgz --root="$dir" --revision="${revno}" "${TEMP_D}/$tarball"
echo "created ${tarball}"

cd "$TEMP_D"
tar -xzf $tarball ||
   fail "failed to extract '$tarball'"

sed -i "s,REVNO,$revno," "$dir/debian/changelog" ||
   fail "failed to replace REVNO in debian/changelog"

cd "$dir"
debuild "$@" || fail "debuild failed"

cd "$TEMP_D"
for f in *; do
   [ -f "$f" ] || continue
   cp "$f" "$start_d/" || fail "failed copy $f"
   echo "wrote $f"
done
exit
