#!/bin/sh

# This script follows the interface of debathenificator scripts but
# does not itself use debathenificator because what we need to do
# doesn't fit the model.  Most of the code here is copied from
# debathenificator, but:
#
# 1. We don't do anything on platforms which already have a usable
#    version of cyrus-sasl2-mit.
#
# 2. The version check is modified to check the native version of
#    cybus-sasl2 (which we want to match) rather than cyrus-sasl2-mit
#    (which does not exist on most of the platforms we want to build
#    for).
#
# 3. cmd_source creates the source package by combining the tarfile
#    from the native cyrus-sasl2 package with the packaging materials
#    stored here (which are based on the now-defunct cyrus-sasl2-mit
#    but are heavily modified).

set -e

: ${DEBATHENA_APT=/mit/debathena/apt}

# Process arguments.
dist_arch=$1; shift
a=
if [ "$1" = "-A" ]; then a=-A; shift; fi
chroot=$dist_arch-sbuild

if [ -z "$dist_arch" -o $# -eq 0 ]; then
  echo 'No arguments!' >&2
  exit 2
fi

dist=$(echo "$dist_arch" | sed 's/^\(.*\)-\([^-]*\)$/\1/')
arch=$(echo "$dist_arch" | sed 's/^\(.*\)-\([^-]*\)$/\2/')
: ${release=-proposed}
. /mit/debathena/bin/debian-versions.sh
tag=$(gettag $dist)

# Create a chroot and define functions for using it.
sid=$(schroot -b -c "$chroot")
trap 'schroot -e -c "$sid"' EXIT
sch() { schroot -r -c "$sid" -- "$@"; }           # Run in the chroot
schq() { schroot -q -r -c "$sid" -- "$@"; }       # Run in the chroot quietly
schr() { schroot -r -c "$sid" -u root -- "$@"; }  # Run in the chroot as root
schr apt-get -qq -y update || exit 3

quote() {
  echo "$1" | sed 's/[^[:alnum:]]/\\&/g'
}

name=cyrus-sasl2-mit
daversionappend=$(dpkg-parsechangelog -l$(dirname "$0")/debian/changelog |
  sed -nre 's/Version: .*(debathena.*)$/\1/p')

cmd_source() {
  if [ "$a" != "-A" ]; then
    echo "Not building source package for $dist_arch." >&2
    return
  fi

  if [ ! -e "cyrus-sasl2_$version.dsc" ]; then
    sch apt-get -d source cyrus-sasl2
  fi

  if [ ! -e "${name}_$daversion.dsc" ]; then
    (
      tmpdir=$(mktemp -td "debathenify.$$.XXXXXXXXXX")
      trap 'rm -rf "$tmpdir"' EXIT
      pkgdir=$tmpdir/$name-$origversion
      mkdir "$pkgdir"
      cp -a $(dirname "$0")/debian "$pkgdir"
      cp -a "cyrus-sasl2_$origversion.orig.tar.gz" "$pkgdir"
      here=$(pwd)
      (cd $tmpdir && tar czf "${name}_$origversion.orig.tar.gz" \
        "$name-$origversion/cyrus-sasl2_$origversion.orig.tar.gz")
      perl -0pe "s/\@TARDIR\@/cyrus-sasl-$origversion/m or die" \
        -i "$pkgdir/debian/rules"
      cd "$tmpdir/$name-$origversion"
      schr apt-get -q -y build-dep cyrus-sasl2
      schr apt-get -q -y install dbs devscripts
      sch debuild -S -sa -us -uc -i -I.svn
      cp -a "$tmpdir/${name}_$daversion"* "$here"
      cp -a "$tmpdir/${name}_$origversion.orig.tar.gz" "$here"
    )
  fi
}

cmd_binary () {
  sbuildhack "$dist_arch" $a "${name}_$daversion.dsc"
}

v() {
    echo "$@"
    "$@"
}

cmd_upload () {
  REPREPRO="v reprepro -Vb $DEBATHENA_APT --ignore=wrongdistribution"
  REPREPROI="$REPREPRO --ignore=missingfield"

  if [ "$a" = "-A" ]; then
    $REPREPROI include "${dist}${release}" "${name}_${daversion}_source.changes"
  fi
  $REPREPRO include "${dist}${release}" "${name}_${daversion}${tag}_${arch}.changes"
}

case $dist in
sarge|dapper|edgy)
  echo "Native libsasl2-krb4-mit package exists; skipping $dist_arch." >&2
  exit
  ;;
esac

version=$(
  sch apt-cache showsrc cyrus-sasl2 | \
    sed -n 's/^Version: \(.*\)$/\1/ p' | (
      version='~~~'
      while read -r newversion; do
        if dpkg --compare-versions "$newversion" '>' "$version"; then
          version=$newversion
        fi
      done
      if [ "$version" = '~~~' ]; then
        echo "No version of $name found." >&2
        exit 1
      fi
      echo "$version"
    )
)
origversion=$(echo "$version" | sed 's/-[^-]*$//')
daversion=$origversion-0$daversionappend

if zcat "$DEBATHENA_APT/dists/$dist/debathena/binary-$arch/Packages.gz" | \
  dpkg-awk -f - "Source:^$name\$" "Version:^$(quote "$daversion$tag")\$" -- Architecture | \
  if [ "$a" = "-A" ]; then cat; else fgrep -vx 'Architecture: all'; fi | \
  grep -q .; then
  echo "$name $daversion already exists for $dist_arch." >&2
  exit 0
fi

for cmd; do
  "cmd_$cmd"
done
