#!/bin/sh

set -eu

SCRDIR=`dirname $0`

if [ ! -f ${SCRDIR}/salsarc ] ; then
  echo "Missing ${SCRDIR}/salsarc"
  exit 1
fi

. ${SCRDIR}/salsarc

DEBPKGNAME=`dpkg-parsechangelog | awk '/^Source:/ {print $2}'`
SHORTDESC=`grep "^Description: " debian/control | head -n 1 | sed 's/^Description: //'`
VCSGIT=`grep "^Vcs-Git: " debian/control | sed -e 's/^Vcs-Git: //' -e 's#[githps]\+://anonscm.debian.org/#ssh://git.debian.org/#'`
SALSA_GROUP=`echo $VCSGIT | sed -e 's#.*://salsa.debian.org/\([^/]\+\)/.*#\1#'`

#echo $VCSGIT
#echo $SALSA_GROUP

SALSA_GROUP_ID=$(curl --silent -f -XGET --header "PRIVATE-TOKEN: $SALSA_TOKEN" "$SALSA_URL/groups?all_available=false" | jq ".[] | select(.path == \"$SALSA_GROUP\") | .id")

set -x
curl "$SALSA_URL/projects?private_token=$SALSA_TOKEN" \
     --data "path=${DEBPKGNAME}&namespace_id=${SALSA_GROUP_ID}&description=${SHORTDESC}&visibility=public"

  git remote add origin git@salsa.debian.org:${SALSA_GROUP}/${DEBPKGNAME}.git
  git push origin master
  git push --all --set-upstream
  git push --tags
