#!/bin/sh

set -e

if test "$1" = "--help"; then
	cat <<EOT
Simply launch github-debian-upstream in a source Debian directory. If
debian/control or debian/copyright contains a valid GitHub link, this
tool will generate debian/upstream/metadata file. It adds automatically
the file using "git add" and displays result. You can then modify it.
Don't forget to launch a "git commit".

Copyright (C) 2019 Xavier Guimard <yadd@debian.org>
EOT
	exit
fi

if test "$1" = "--version"; then
	echo 0.1
fi

if test -e debian/upstream/metadata; then
	echo "debian/upstream/metadata found" >&2
	exit 1
fi

upstream=`perl -ne 'if(s@.*github.com/([^/]+)/([^/#]+).*$@$1/$2@){print;exit}' debian/copyright`

if test "$upstream" = ""; then
	echo "Trying control" >&2
	upstream=`perl -ne 'if(s@.*github.com/([^/]+)/([^/#]+).*$@$1/$2@){print;exit}' debian/control`
fi
if test "$upstream" = ""; then
	echo Not found >&2
	exit 1
fi

name=${upstream#*/}

mkdir -p debian/upstream
cat >debian/upstream/metadata <<EOF
---
Archive: GitHub
Bug-Database: https://github.com/$upstream/issues
Contact: https://github.com/$upstream/issues
Name: $name
Repository: https://github.com/$upstream.git
Repository-Browse: https://github.com/$upstream
EOF

git add debian/upstream/metadata || true
cat debian/upstream/metadata
echo
echo "git commit debian/upstream/metadata -m 'Add upstream/metadata'"

