#!/bin/sh

# Usage: dasource [-r REPOS] [PACKAGENAME ...]

# Creates or updates a checkout and source package for each listed
# PACKAGENAME, or for all regular packages if no PACKAGENAME is
# specified.

# If REPOS is specified, it is used in preference to the canonical
# Athena repository trunk.

# Package subdirectories are created in the cwd.  The source checkout
# will reside in PACKAGENAME/PACKAGENAME-VERSION and the source
# package will be created in PACKAGENAME.

set -e

die() {
  echo "$@" >&2
  exit 1
}

usage() {
  die "dasource [-r repos] [packagename ...]"
}

: ${DEBATHENA_APT=/mit/debathena/apt}
basedir=$(dirname "$0")
repo=svn+ssh://svn.mit.edu/athena/trunk

while getopts r: opt; do
  case "$opt" in
  r)
    type="$OPTARG"
    ;;
  \?)
    usage
    ;;
  esac
done

do_package() {
  pkgname=$1
  pkgpath=$2
  if [ ! -d $pkgname ]; then
    echo "Creating directory $pkgname"
    mkdir $pkgname
  fi

  # Work around a bad interaction between dpkg-source and AFS.
  # (dpkg-source invokes perl's Tempfile which checks if the parent
  # directory is writable using mode bits).  This will not actually
  # grant any outside access to the directory since AFS ignores
  # non-user mode bits.
  if fs whichcell . >/dev/null 2>&1; then
    chmod 777 $pkgname
  fi

  # Check out or update the package source.
  pattern="$pkgname/$pkgname-[0-9]*[0-9]"
  if [ $(echo $pattern | wc -w) -gt 1 ]; then
    die "More than one checkout under $pkgname!"
  elif [ -d $pattern ]; then
    dir=$(echo $pattern)
    echo "Updating and cleaning $dir"
    (cd $dir && svn update && svn revert -R . &&
     svn st | awk '/^?/ {print $2}' | xargs rm -vrf)
  else
    dir=$pkgname/$pkgname
    echo "Checking out $repo/$pkgpath into $dir"
    svn co $repo/$pkgpath $dir
  fi

  # Extract the changelog version and strip off the epoch and Debian component.
  changever=$(cd $dir && dpkg-parsechangelog | sed -n 's/Version: //p')
  sver=$(echo $changever | sed -re 's/^[0-9]+://p')
  upver=$(echo $sver | sed -re 's/-[^-]*$//')

  # Rename the source checkout if necessary.
  correctdir=$pkgname/${pkgname}-$upver
  if [ $dir != $correctdir ]; then
    echo "Renaming $dir to $correctdir"
    mv $dir $correctdir
    dir=$correctdir
  fi

  # Add autoconf goo if it's an Athena source directory.
  case $pkgpath in
  athena/*)
    (cd $dir && $basedir/daconfiscate)
    ;;
  esac

  # Generate debian/control from debian/control.in if control.in exists
  ([ -f "$dir/debian/control.in" ] && cd $dir && DEB_AUTO_UPDATE_DEBIAN_CONTROL=yes debian/rules debian/control || :)
  [ -f "$dir/debian/control" ] || die "Can't find or generate debian/control!"

  # Create a suitable orig tarball if necessary.
  (cd $dir && $basedir/daorig)

  # Build an unsigned package, ignoring version control subdirs in the source.
  echo "Creating source package"
  (cd $dir && debuild -S -i -I.svn -sa -us -uc)
}

if [ ! -r packages ]; then
  die "Can't read packages file; create with gen-packages"
fi

if [ $# -gt 0 ]; then
  # Build specific source packages.
  for pkgname; do
    pkgpath=$(sed -nre "s/^$pkgname[[:space:]]+//p" packages)
    [ -n "$pkgpath" ] || die "Can't find package $pkgname"
    do_package $pkgname $pkgpath
  done
else
  # Build all source packages.
  exec <packages
  while read pkgname pkgpath; do
    do_package $pkgname $pkgpath
  done
fi
