#!/bin/bash

set -x
set -e
set -u

export LC_ALL=C
export LANG=C

if [ -r /etc/jenkins/debian_glue ] ; then
  . /etc/jenkins/debian_glue
fi

[ -n "${DEBEMAIL:-}" ] || DEBEMAIL="jenkins-debian-glue Autobuilder <jenkins@`hostname -f`>"
export DEBEMAIL

if [ ! -d source ] ; then
  echo "Please run the script in the jenkins workspace." >&2
  echo 'NOTE: Make sure to set the "Local module directory (optional)" option in the Subversion configuration to "source"' >&2
  exit 1
fi

if [ -z "${BUILD_NUMBER:-}" ] ; then
  echo "No BUILD_NUMBER defined, please run it in jenkins." >&2
  exit 1
fi

if [ -z "${SVN_REVISION:-}" ] ; then
  echo "No SVN_REVISION defined, please run it with a subversion repository." >&2
  exit 1
fi

if ! [ -x "$(which svn2cl)" ] ; then
  echo "Error: svn2cl not available, please install subversion-tools <= 1.6.18dfsg-1 or svn2cl." >&2
  exit 1
fi

if ! [ -x "$(which svn)" ] ; then
  echo "Error: svn not available, please install subversion." >&2
  exit 1
fi

if ! [ -x "$(which dpkg-parsechangelog)" ] ; then
  echo "Error: dpkg-parsechangelog not available, please install dpkg-dev." >&2
  exit 1
fi

JENKINS_DEBIAN_GLUE_VERSION=$(dpkg --list jenkins-debian-glue 2>/dev/null | awk '/^ii/ {print $3}')
if [ -n "${JENKINS_DEBIAN_GLUE_VERSION:-}" ] ; then
  echo "*** Running jenkins-debian-glue version $JENKINS_DEBIAN_GLUE_VERSION ***"
fi

echo "***  source package build phase ***"
rm -f ./* || true

ORIG_DIR=$(pwd)
cd source/${branch:-}

if ! [ -r debian/changelog ] ; then
  echo "Error: could not find debian/changelog (not a Debian package or wrong tag/branch?)" >&2
  exit 1
fi

SINCE_REVISION=$(svn info debian/changelog 2>/dev/null | awk '/Last Changed Rev:/ {print $4}')

if [ -z "${SINCE_REVISION:-}" ] ; then
  echo "Error: could not detect svn revision which modified debian/changelog." >&2
  exit 1
fi

# required for dpkg-parsechangelog
if ! [ -r debian/changelog ] ; then
  echo "Error: debian/changelog could not be read. Is this really a Debian package?" >&2
  exit 1
fi

# package name
PACKAGE=$(dpkg-parsechangelog --count 1 | awk '/^Source: / {print $2}')

# get newest version number from changelog
PREVIOUS_VERSION=$(dpkg-parsechangelog --count 1 | awk '/^Version: / {print $2}')

build_snapshot() {

  DISTRIBUTION=$(dpkg-parsechangelog --count 1 | awk '/^Distribution/ {print $2}')

  TIMESTAMP=$(date -u +%Y%m%d%H%M%S)

  if [ "$DISTRIBUTION" = "UNRELEASED" ] ; then
    # we do NOT raise the version number, if we detect an unreleased version
    SNAPSHOT_VERSION="${PREVIOUS_VERSION}~${TIMESTAMP}.svn${SVN_REVISION}.${BUILD_NUMBER}"
  else
    # calculate new snapshot version
    SNAPSHOT_VERSION="$(increase-version-number "$PREVIOUS_VERSION")~${TIMESTAMP}.svn${SVN_REVISION}.${BUILD_NUMBER}"
  fi

  # generate changelog
  SVN_CHANGELOG_ORIG=$(mktemp)
  SVN_CHANGELOG_EDIT=$(mktemp)
  # build GNU like changelog with svn2cl as helper tool
  # timeout after 15 seconds without progress, we might be running into e.g.
  # "xsltApplyXSLTTemplate: A potential infinite template recursion was detected."
  if ! timeout -k 16 16 svn2cl --reparagraph --stdout --include-rev -r HEAD:$SINCE_REVISION > "$SVN_CHANGELOG_ORIG" ; then
    echo '  * Sorry, generating changelog using svn2cl failed. :(
    Make sure the svn2cl command works on the jenkins host.
    For example validating server certificates is a common error.
' > "$SVN_CHANGELOG_ORIG"
  fi

  ## disclaimer: use separate sed calls to make it easier to adjust if necessary
  # reduce changelog to just the first line of the commit message
  sed -n '/^\t*\s*\* \[r/,/^\t*\s*$/p' "$SVN_CHANGELOG_ORIG" > "$SVN_CHANGELOG_EDIT"

  # get rid of author information that might be present from git-svn users
  sed -i '/^\t\s\sFrom: /d' "$SVN_CHANGELOG_EDIT"

  # replace tabs at beginning of lines with two spaces
  sed -i 's/^\t/  /' "$SVN_CHANGELOG_EDIT"

  # get rid of lines with just 4 space chars only
  sed -i '/^    $/d' "$SVN_CHANGELOG_EDIT"

  # get rid of empty lines
  sed -i '/^$/d' "$SVN_CHANGELOG_EDIT"

  # get rid of of the timestamp lines, looking like:
  # 2011-12-02 21:25  mprokop
  sed -ri '/^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}/d' "$SVN_CHANGELOG_EDIT"

  cat > debian/changelog.new << EOF
$PACKAGE ($SNAPSHOT_VERSION) unstable; urgency=low

  ** SNAPSHOT build **

EOF

  cat "$SVN_CHANGELOG_EDIT" >> debian/changelog.new

  cat >> debian/changelog.new << EOF

 -- $DEBEMAIL  $(date -u -R)

EOF

  # append original changelog
  cat debian/changelog >> debian/changelog.new

  # finally install new changelog
  mv debian/changelog.new debian/changelog
  rm -f "$SVN_CHANGELOG_ORIG" "$SVN_CHANGELOG_EDIT"
}

case "${branch:-}" in
  tags/*) echo "Building a tag version, not modifying the package" ;;
  *) build_snapshot;;
esac

cd $ORIG_DIR

debian_only="$(svn propget mergeWithUpstream source/${branch:-}/debian)"

if [ "${debian_only:-}" = "1" ] ; then
  (
    cd "source/${branch:-}"
    echo "mergeWithUpstream detected, using svn-buildpackage to create source package"
    svn-buildpackage --svn-download-orig -S \
     --svn-builder 'dpkg-buildpackage' -d \
     --svn-move-to="${ORIG_DIR}" --svn-dont-purge -uc -us \
     --svn-ignore-new -rfakeroot
  )
else
  if [ -n "${PRE_SOURCE_HOOK:-}" ] ; then
    echo "*** Found environment variable PRE_SOURCE_HOOK, set to ${PRE_SOURCE_HOOK:-} ***"
    sh ${PRE_SOURCE_HOOK:-}
  fi

  dpkg-source --tar-ignore=\.svn -b source/${branch:-}
fi

# revert to original debian/changelog to avoid highly increasing version numbers with each build
( cd source/${branch:-} ; svn revert debian/changelog )

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