#!/usr/bin/env bash

usage() {
    echo "Usage: $0 [-b|--build] [-u|--upload] [-d|--distribution <all|unstable|stable>] SWH_PACKAGE"
    echo "E.g.: make-package -b swh-core"
    exit 1
}

# command line parsing

build="no"
upload="no"
package=""
distribution="all"
while (( "$#" )); do
    case "$1" in
        -b|--build) build="yes" ;;
        -u|--upload) upload="yes" ;;
        -d|--distribution) shift; distribution=$1;;
        *) package="$1";;
    esac
    shift
done
if [ "$build,$upload" = "no,no" -o -z "$package" ] ; then
    usage
fi
if [ $distribution != 'unstable' -a $distribution != 'stable' -a $distribution != 'all' ]; then
    usage
fi

set -e

CURDIR=$(readlink -f "$package")
PACKAGEDIR=$(readlink -f "packages")
BASENAME="$(basename "$CURDIR")"
MODULE="${BASENAME//-/.}"

REPOSITORY=https://debian.softwareheritage.org/

DESTINATION=pergamon.internal.softwareheritage.org
DESTDIR=/srv/softwareheritage/repository

TEMP=$(mktemp -d)

trap "{ rm -rf $TEMP; }" EXIT

cd "$CURDIR"

VERSION=$(python3 -c 'from setuptools_scm import get_version; print(get_version())')

SID_VERSION=${VERSION}-1
SID_CHANGES_FILE=${BASENAME}_${SID_VERSION}_amd64.changes
SID_LOGFILE=${BASENAME}_${SID_VERSION}_amd64.build

BPO_VERSION=${SID_VERSION}~bpo10~swh+1
BPO_CHANGES_FILE=${BASENAME}_${BPO_VERSION}_amd64.changes
BPO_LOGFILE=${BASENAME}_${BPO_VERSION}_amd64.build

SBUILD="sbuild -As --force-orig-source --build-dep-resolver=aptitude --build-failed-commands %SBUILD_SHELL --no-run-lintian"

if [ "$build" = "yes" ] ; then
    # Generate source tarball and put it in the right place
    python3 setup.py sdist -d $TEMP
    mv $TEMP/$MODULE-$VERSION.tar.gz $TEMP/${BASENAME}_${VERSION}.orig.tar.gz

    # Extract source tarball and overlay Debian packaging
    cd $TEMP
    tar xf ${BASENAME}_${VERSION}.orig.tar.gz
    mv $MODULE-$VERSION $BASENAME-$VERSION
    cd $BASENAME-$VERSION
    cp -r $CURDIR/debian .

    if [ "$distribution" = "all" -o "$distribution" = "unstable" ]; then
        # Generate changelog for unstable
        dch -v "${SID_VERSION}" "Deploy ${VERSION}"
        dch --force-distribution --distribution unstable-swh -r ""

        # Build unstable package with original source
        $SBUILD \
          --extra-repository="deb [trusted=yes] ${REPOSITORY} unstable main" \
          --extra-repository="deb http://incoming.debian.org/debian-buildd/ buildd-unstable main" \
          --extra-repository="deb [trusted=yes] http://www.apache.org/dist/cassandra/debian 40x main"

        # Copy package to staging directory
        dcmd cp ../${SID_CHANGES_FILE} ${PACKAGEDIR}
        cp -L ../${SID_LOGFILE} ${PACKAGEDIR}
    fi

    if [ "$distribution" = "all" -o "$distribution" = "stable" ]; then
        # Generate changelog for backports
        dch -bv "${BPO_VERSION}" "Rebuild for buster-backports-swh"
        dch -r --distribution buster-backports-swh --force-distribution ""

        # Build backport package
        $SBUILD \
            --extra-repository="deb [trusted=yes] ${REPOSITORY} buster-swh main" \
            --extra-repository="deb http://deb.debian.org/debian buster-backports main" \
            --extra-repository="deb [trusted=yes] http://www.apache.org/dist/cassandra/debian 40x main"

        # Copy package to staging directory
        dcmd cp ../${BPO_CHANGES_FILE} ${PACKAGEDIR}
        cp -L ../${BPO_LOGFILE} ${PACKAGEDIR}
    fi
fi

cd "$CURDIR"

if [ "$upload" = "yes" ] ; then
    changefiles=()
    if [[ "${VERSION}" == *dev* || "${VERSION}" == *post* ]]; then
        echo "Uploading a dev version is not allowed! Please tag and rebuild."
        exit 2
    fi

    if [ "$distribution" = "all" -o "$distribution" = "unstable" ]; then
        changefiles+=(${PACKAGEDIR}/${SID_CHANGES_FILE})
    fi

    if [ "$distribution" = "all" -o "$distribution" = "stable" ]; then
        changefiles+=(${PACKAGEDIR}/${BPO_CHANGES_FILE})
    fi

    # Sign and send packages
    for changefile in "${changefiles[@]}"; do
        debsign ${changefile}
        dcmd scp ${changefile} ${DESTINATION}:${DESTDIR}/incoming
        ssh ${DESTINATION} "umask 002; reprepro -vb ${DESTDIR} processincoming incoming"
    done

    git push --tags
fi