#!/bin/sh
set -e

name=bash
version=$(
    schroot -c "$1-sbuild" -- apt-cache showsrc "$name" | \
	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"
	)
)
daversion=${version}debathena4

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

REPREPRO="v reprepro -Vb /mit/debathena/apt"
REPREPROI="$REPREPRO --ignore=wrongdistribution"

dist=$(echo "$1" | sed 's/^\(.*\)-\([^-]*\)$/\1/')
arch=$(echo "$1" | sed 's/^\(.*\)-\([^-]*\)$/\2/')

case "$dist" in
    sarge) tag='~debian3.1' ;;
    etch) tag='~debian4.0' ;;
    dapper) tag='~ubuntu6.06' ;;
    edgy) tag='~ubuntu6.10' ;;
    feisty) tag='~ubuntu7.04' ;;
    gutsy) tag='~ubuntu7.10' ;;
esac

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