#!/bin/bash

# Take a source tree from git and build the master source package from it.
# The tweak-source script then takes this as input.

# The MIT License
#
# Copyright (c) 2011,2012 by Michael Prokop <mika@debian.org>
# Copyright (c) 2012-2026 by Christoph Berg <myon@debian.org>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

set -eux

PACKAGE="$(dpkg-parsechangelog -SSource)"
if [ "${JOB_NAME:-}" ]; then
  case $JOB_NAME in
    $PACKAGE-*) ;;
    *) echo "JOB_NAME=$JOB_NAME does not match package name $PACKAGE(-*)"
      exit 1
      ;;
  esac
fi

# snapshot builds: merge with upstream git
if [ "${upstream_branch:-}" ]; then
  git -C ../upstream archive upstream/$upstream_branch | tar --extract --exclude="debian/*"
  GIT_REVISION=$(git -C ../upstream show -s --format=%h upstream/$upstream_branch)
  is_snapshot="snapshot"
fi

# remove artifacts from last build (-r because sometimes uscan leaves temp directories behind)
rm -rf ../*.*

# get orig tarball and unpack it in case it's a debian/ only checkout
origtargz --tar-only --path ../tarballs --path=$HOME/tarballs || orig_fail="$?"

CL_DISTRIBUTION=$(dpkg-parsechangelog -SDistribution)
case $CL_DISTRIBUTION in
  *UNRELEASED*) is_unreleased=UNRELEASED ;;
esac

if [ "${is_snapshot:-}" ] || [ "${is_unreleased:-}" ]; then
  if [ "${orig_fail:-}" ]; then
    if test -d debian/patches; then
      QUILT_PATCHES=debian/patches quilt push -a || [ $? = 2 ]
    fi
    echo "1.0" > debian/source/format
  fi
  DATE="$(date -u +%Y%m%d.%H%M)"
  if [ -z "${GIT_REVISION:-}" ]; then
    GIT_REVISION=$(echo ${GIT_COMMIT:-xxxxxxx} | cut -c 1-7)
  fi
  REVISION="~$DATE.g$GIT_REVISION"
  DPKG_SOURCE_FLAGS="--include-binaries --auto-commit"

  sed -i -e"1s/)/$REVISION)/" debian/changelog

elif [ "${orig_fail:-}" ]; then
  echo "Orig tarball is missing, bailing out"
  exit 1
fi

sed -ne '1,/^ --/p' debian/changelog

# build source package
dpkg-parsechangelog -SVersion > ../version.txt
dpkg-source -i -I ${DPKG_SOURCE_FLAGS:-} --build .

# save current tarball for next time
cd ..
rm -rf tarballs
if ls *.orig.tar.* >/dev/null 2>&1; then
  mkdir tarballs
  ln *.orig.tar.* tarballs
fi

if [ "${GIT_COMMIT:-}" ]; then
  # remove everything except .git to save space
  find source/ -mindepth 1 -maxdepth 1 -not -name .git | xargs -r rm -rf
  if [ -d upstream ]; then
    find upstream/ -mindepth 1 -maxdepth 1 -not -name .git | xargs -r rm -rf
  fi
fi

# vim:foldmethod=marker ts=2 ft=sh ai expandtab sw=2
