#!/bin/bash
set -xe
package=$1
stamp=$2
suite=$3
psuite=$4
: ${DEBATHENA_APT=/mit/debathena/apt}
: ${DEBATHENA_BUILD_AREA=/afs/sipb.mit.edu/project/debathena/packages}
# Release to upload to
: ${DEBATHENA_RELEASE=-staging}
export DEBATHENA_RELEASE
. /mit/debathena/bin/debian-versions.sh
tag=$(gettag $suite)
[ -n "$1" ]
[ -n "$2" ]
trap 'echo "An error occured.  Press Enter to continue." >&2; echo -en "\a"; read ERROR; touch "$stamp.error"' EXIT
touch "$stamp.started"
dir=$(echo ${DEBATHENA_BUILD_AREA}/*/"${package#debathena-}")
if [ ! -e $dir ]; then
    dir=$(echo ${DEBATHENA_BUILD_AREA}/*/"${package}")
fi
[ -e "$dir" ]
cd "$dir"
# Skip the package if it's listed in nobuild
if fgrep -q "$suite" nobuild; then
    touch "$stamp.done"
    trap - EXIT
    exit
fi
# Deal with debathenified packges
debathenificator=$(echo ./debathenify-*)
if [ "$debathenificator" != "./debathenify-*" ]; then
    "$debathenificator" "${suite}-amd64" -A source binary upload
    "$debathenificator" "${suite}-i386" source binary upload
    touch "$stamp.done"
    trap - EXIT
    exit
fi
# For each of the releases in the previous suite, look for the source
# package versions and accept the last one we find.  Due to the
# current repo layout, this also happens to be the highest version.
# We may wish to explicitly select the highest version in the code.
extra=
if [ "$psuite" = "$suite" ]; then
    extra="-staging"
fi
for suffix in "" -proposed -development $extra; do
    vv=$(zcat ${DEBATHENA_APT}/dists/$psuite$suffix/*/source/Sources.gz | dpkg-awk -f - "Package:^$package\$" -- Version | sed -n 's/Version: // p')
    if [ -n "$vv" ]; then
        version="$vv"
    fi
done
# Find the .dsc in the built/ directory for the package
if [ -d "built/" ]; then
    dsc="built/${package}_$version.dsc"
fi
# Build the .dsc if necessary.
if ! [ -e "$dsc" ]; then
    if ! [ -e "${package}_$version.dsc" ]; then
        schroot -c "${psuite}-amd64-sbuild" -p -u root -- sh -ec "DEBATHENA_BUILD_DIST=$psuite $(readlink -f /mit/debathena/bin/chroot-sources) && apt-get update && apt-get source $package"
    fi
    if ! ([ -e "${package}_${version}_source.changes" ] || [ -e "${package}_${version}_amd64.changes" ] || [ -e "${package}_${version}_i386.changes" ]); then
        (cd "${package}-${version/-*/}" && dpkg-genchanges -S -sa >"../${package}_${version}_source.changes")
    fi
    dsc="${package}_$version.dsc"
fi
[ -e "$dsc" ]

if [ -e "${dsc%.dsc}_amd64.changes" ] || [ -e "${dsc%.dsc}_i386.changes" ]; then
    # If there is no ~debianX.Y tag in the .changes filename, this should be an
    # equivs package. Set variables and check that it is, in fact, an equivs
    # package. Note that it should be built already (since we build equivs
    # packages once and upload them to all releases).
    changes="${dsc%.dsc}_amd64.changes"
    buildlog="${dsc%.dsc}_amd64.build"
    if [ ! -e $changes ]; then
	changes="${dsc%.dsc}_i386.changes"
	buildlog="${dsc%.dsc}_i386.build"
    fi
    debfile="$(dpkg-awk -f "$changes" -- Files | awk '$5 ~ /_all\.deb$/ {print $5}')"
    [ -e "$debfile" ]
    dpkg --fsys-tarfile "$debfile" | tar xO ./usr/share/doc/"$package"/README.Debian | egrep -q '(The special dependencies used in this package are:|This is a Debathena manual configuration package)'
else
    # This is a normal (non-equivs) package, so we want to build it for this
    # release.
    [ -e "${dsc%.dsc}_source.changes" ]
    changes="${dsc%.dsc}_source.changes"
    A=1
    if (zcat ${DEBATHENA_APT}/dists/$psuite/*/binary-i386/Packages.gz | dpkg-awk -f - "Source:^$package\$" -- Architecture; zcat ${DEBATHENA_APT}/dists/$psuite/*/binary-i386/Packages.gz | dpkg-awk -f - "Package:^$package\$" -- Architecture) | grep -Fxv 'Architecture: all' | grep -q .; then
	A=0
    fi
    sbuildhack "${suite}-amd64" -A "$dsc"
    [ $A -eq 1 ] || sbuildhack "${suite}-i386" "$dsc"
    changes=$changes\ "$(basename "${dsc%.dsc}${tag}_amd64.changes")"
    [ $A -eq 1 ] || changes=$changes\ "$(basename "${dsc%.dsc}${tag}_i386.changes")"
fi

#while
#    echo "Type yes to upload: $changes"
#    echo -en "\a"
#    read UPLOAD
#    [ "$UPLOAD" != "yes" ]; do
#    :
#done

(
    flock -x 200
    files=
    for change in $changes; do
	if [ "$change" = "${dsc%.dsc}_source.changes" ]; then
	    dareprepro include "$suite$DEBATHENA_RELEASE" "$change"
	else
	    dareprepro include "$suite$DEBATHENA_RELEASE" "$change"
	    files=$files\ $change\ $(perl -0ne '$_ =~ /\nFiles: *\n(( [^\n]*\n)*)/; print $1;' "$change" | awk '{print $5}' | grep -v '\.orig\.tar\.gz$' || :)
	fi
    done
    ! [ -d "built/" ] || mv -i $files "built/"
) 200> /tmp/debathena-repository-lock

touch "$stamp.done"
trap - EXIT
