#!/usr/bin/env bash

set -e
set -x
set -o pipefail

package=$(basename $(pwd))
module=${package//-/.}

if [ $# -ne 0 ]; then
  last_debian_rev=$1
else
  last_debian_rev=master
fi

git branch -D pristine-tar debian/upstream debian/unstable-swh debian/stretch-swh || true

for tag in `git tag -l debian/*`; do
  git tag -d $tag
done

for tag in `git tag -l --sort=v:refname v\*`; do
  ver=${tag/v/}
  firstver=${firstver:-${ver}}
  if [ -f ../packages/${package}_${ver}.orig.tar.gz ]; then
     continue
  fi

  git checkout $tag
  if [ -d swh ]; then
    git clean -dfx swh
  fi
  if [ -f yarn.lock ]; then
    (yarn install --frozen-lockfile && yarn build) || true
  fi
  find . -maxdepth 1 -type d -name '*.egg-info' -exec rm -r '{}' \+
  python3 setup.py egg_info
  pname=$(awk '/^Name:/{print $2}' *.egg-info/PKG-INFO)
  pver=$(awk '/^Version:/{print $2}' *.egg-info/PKG-INFO)
  python3 setup.py sdist -d ../packages
  mv ../packages/${pname}-${pver}.tar.gz ../packages/${package}_${ver}.orig.tar.gz
done

upstream_tag=$(git describe --abbrev=0)
ver=${upstream_tag/v/}

author_name=$(git tag -l --format="%(if)%(*objecttype)%(then)%(taggername)%(else)%(authorname)%(end)" "${upstream_tag}")
author_email=$(git tag -l --format="%(if)%(*objecttype)%(then)%(taggeremail)%(else)%(authoremail)%(end)" "${upstream_tag}")
# Strip <>
author_email=${author_email:1:-1}
author_date=$(git tag -l --format="%(if)%(*objecttype)%(then)%(taggerdate:iso)%(else)%(authordate:iso)%(end)" "${upstream_tag}")

export DEBEMAIL="${author_email}"
export DEBFULLNAME="${author_name}"
export GIT_AUTHOR_NAME="${author_name}"
export GIT_AUTHOR_EMAIL="${author_email}"
export GIT_AUTHOR_DATE="${author_date}"
export GIT_COMMITTER_NAME="${author_name}"
export GIT_COMMITTER_EMAIL="${author_email}"
export GIT_COMMITTER_DATE="${author_date}"

git checkout $upstream_tag
git clean -dfx
git checkout -b debian/upstream

git ls-tree --name-only HEAD | xargs rm -r
tar -x --strip-components 1 -f ../packages/${package}_${firstver}.orig.tar.gz
git add .
git commit --no-verify -m "Import upstream version ${firstver}"
git tag debian/upstream/${firstver}

git checkout --orphan pristine-tar
pristine-tar commit ../packages/${package}_${firstver}.orig.tar.gz

git checkout debian/upstream
git checkout -b debian/unstable-swh
git checkout ${last_debian_rev} -- debian
cat > debian/gbp.conf << EOF
[DEFAULT]
upstream-branch=debian/upstream
upstream-tag=debian/upstream/%(version)s
upstream-vcs-tag=v%(version)s
debian-branch=debian/unstable-swh
pristine-tar=True
EOF

rm debian/changelog
faketime "${author_date}" dch --create --package ${package} -v ${firstver}-1~swh1 'Initial release'

git tag -l --format="%(contents:subject)%(if)%(contents:body)%(then)%0a%(contents:body)%(end)" "${upstream_tag}" | sed -E -e '/^$/d' -e 's/^ *(- *)?//' | while read line; do
  faketime "${author_date}" dch "${line}"
done

faketime "${author_date}" dch -D unstable-swh --force-distribution ''

git add debian
git commit --no-verify -m "Updated debian directory for version ${firstver}"

git checkout -b debian/buster-swh
faketime "${author_date}" dch --bpo -D buster-swh --force-distribution ''
git add debian/changelog
sed -i s/unstable/buster/ debian/gbp.conf
git add debian/gbp.conf
git commit --no-verify -m "Updated debian buster backport directory for version ${firstver}"

for tag in `git tag -l --sort=v:refname v\* | tail -n +2`; do
  version=${tag/v/}
  echo $tag: $version
  ../bin/debpkg-bump-version $version ../packages/${package}_${version}.orig.tar.gz
done
