#!/bin/bash

set -e

if [ -z "$1" -o "$1" = "--help" ]
then
  echo "upload - Uploads, tags and pushes"
  echo
  echo "Usage: dht upload foo.changes"
  echo
  echo "Signs the .changes file and the corresponding .dsc file in a temporary"
  echo "location (to avoid touching the original files), uploads them to the archive"
  echo "using \"dput ssh-upload\" and tags them in the repository and pushes the tag."
  echo
  echo "Checks that the distribution is not UNRELEASED and that the tag does"
  echo "not exist already (by tagging first)."
  echo
  exit 0
fi


if [ "$1" = "--manpage" ]
then
cat <<'__END__'
Usage: dht upload foo.changes

Signs the `.changes` file and the corresponding `.dsc` file in a temporary
location (to avoid touching the original files), uploads them to the archive
using `dput ssh-upload` and tags them in the repository and pushes the tag.

Checks that the distribution is not `UNRELEASED` and that the tag does
not exist already (by tagging first).
__END__
	exit 0;
fi

changes="$@"

root="$(realpath --relative-to=$PWD "$(git rev-parse --show-toplevel)")"
tmpdir=$root/uploads

if [ -e $tmpdir ]
then
	echo "$tmpdir exists, please remove"
	exit 1
fi
trap 'rm -rf "$tmpdir"' EXIT
mkdir $tmpdir

for c in $changes
do
	src="$(grep ^Source "$c"|grep-dctrl -s Source -n '' )"
	dht tag $root/p/$src
	dcmd cp -v "$c" "$tmpdir"
	debsign "$tmpdir"/"$(basename "$c")"
	dput ssh-upload "$tmpdir"/"$(basename "$c")"
done
git push --tags
