#!/bin/bash

if [ -z ${JENKINS_DEBIAN_GLUE_QUIET:-} ]; then
  set -x
fi
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

# Force the upgrade if subversion 1.7 (which introduced the upgrade command) or
# later is available. Otherwise, if the system has been upgraded, the 'svn info'
# will fail with an unhelpful error message
SVN_VERSION="$(svn --version | awk '/, version / {print $3}')"
if dpkg --compare-versions "$SVN_VERSION" ge 1.7 ; then
  echo "*** Executing 'svn upgrade' to make sure svn is up2date ***"
  svn upgrade
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}')

  if [ -n "${TIMESTAMP_FORMAT:-}" ] ; then
    echo "*** Using custom timestamp format as specified by TIMESTAMP_FORMAT ***"
    TIMESTAMP="$(date -u +"$TIMESTAMP_FORMAT")"
  else
    TIMESTAMP="$(date -u +%Y%m%d%H%M%S)"
  fi

  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="$(jdg-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) UNRELEASED; 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

# subversion >=1.9 fails hard, see github issue #164
debian_only="$(svn propget mergeWithUpstream source/${branch:-}/debian || true)"

if [ -n "${SBP_OPTS:-}" ] ; then
  echo "*** Found environment variable SBP_OPTS, set to ${SBP_OPTS} ***"
else
  echo "*** Using svn-buildpackage default options provided by jenkins-debian-glue ***"
  SBP_OPTS="-nc --svn-download-orig -S --svn-builder dpkg-buildpackage -d --svn-move-to=${ORIG_DIR} --svn-dont-purge -uc -us --svn-ignore-new -rfakeroot"
fi

if [ "${debian_only:-}" = "1" ] ; then
  (
    cd "source/${branch:-}"
    echo "mergeWithUpstream detected, using svn-buildpackage to create source package"
    svn-buildpackage ${SBP_OPTS:-}
  )
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:-}"
  (
    if [ -n "${SNAPSHOT_VERSION:-}" ] ; then
      CHANGES_FILE="${ORIG_DIR}/${PACKAGE}_${SNAPSHOT_VERSION}_source.changes"
    else
      CHANGES_FILE="${ORIG_DIR}/${PACKAGE}_${PREVIOUS_VERSION}_source.changes"
    fi

    cd "source/${branch:-}"
    dpkg-genchanges -S -u"${ORIG_DIR}" > "${CHANGES_FILE}"
  )
fi

if [ -n "${KEY_ID:-}" ] ; then
  echo "*** Found environment variable KEY_ID, set to ${KEY_ID:-}, signing source package ***"

  if ! [ -x "$(which debsign)" ] ; then
    echo "Error: debsign not available, please make sure the devscripts package is installed." >&2
    exit 1
  fi

  debsign -k"${KEY_ID:-}" *_source.changes
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
