#!/bin/sh
set -e

# Usage: daupload-release [-A] [-S] CHANGESFILE
#        daupload-proposed [-A] [-S] CHANGESFILE
#        daupload-dev [-A] [-S] CHANGESFILE

# After a package has been built for all Debian/Ubuntu versions and
# platforms (typically using "da sbuildhack DSCFILE"), this script
# uploads the package into the release, proposed, or beta apt
# repository.  If -A is specified, only one architecture per
# Debian/Ubuntu version is uploaded.  If -S is specified, only the
# source package is uploaded.

# Upon completion, moves all of the generated files into the "built"
# subdirectory.

# This script will skip dists listed in ./nobuild.

: ${DA_SCRIPTS_DIR="$(dirname "$0")"}
. "$DA_SCRIPTS_DIR"/debian-versions.sh

if [ -n "$DEBIAN_CODES_OVERRIDE" ]; then
    echo "Will override DEBIAN_CODES with: $DEBIAN_CODES_OVERRIDE"
    echo -n "Continue? [y/N] "
    read a
    [ "$a" = "y" ]
    DEBIAN_CODES="$DEBIAN_CODES_OVERRIDE"
fi

case "$(basename "$0")" in
    daupload-release)
	echo "You're uploading to PRODUCTION.  This may not be a good idea." >&2
	echo -n "Continue? [y/N]"
	read a
	[ "$a" = "y" ]
	release=""
	;;
    daupload-proposed)
        release="-proposed"
	;;
    daupload-dev)
        release="-development"
        ;;
    daupload-bleeding)
	release="-bleeding"
	;;
    *)
	echo "Unknown release." >&2
	exit 1
	;;
esac

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

A=0
if [ "$1" = "-A" ]; then A=1; shift; fi
S=0
if [ "$1" = "-S" ]; then S=1; shift; fi

change=$1
cd "$(dirname "$change")"
change=$(basename "$change")
base=${change%_source.changes}

check () {
    [ -s "$1" ] || missing="$missing$1 "
}
uncheck () {
    ! [ -s "$1" ] || missing="$missing-$1 "
}

# If ./nobuild exists, filter out its contents from DEBIAN_CODES.
if [ -e nobuild ]; then
    newcodes=
    for code in $DEBIAN_CODES; do
	if ! fgrep -q "$code" nobuild; then
	    newcodes="$newcodes $code"
	fi
    done
    DEBIAN_CODES=$newcodes
fi

missing=
[ -s "$change" ]
if [ $S -ne 1 ]; then
    for code in $DEBIAN_CODES; do
	tag=$(gettag "$code")
	check "${base}${tag}_amd64.changes"
    done
    if [ $A -eq 1 ]; then
	for code in $DEBIAN_CODES; do
	    tag=$(gettag "$code")
	    uncheck "${base}${tag}_i386.changes"
	done
    else
	for code in $DEBIAN_CODES; do
	    tag=$(gettag "$code")
	    check "${base}${tag}_i386.changes"
	done
    fi
fi

if [ -n "$missing" ]; then
    echo "Missing $missing" >&2
    echo -n "Continue? [y/N]"
    read a
    [ "$a" = "y" ]
fi

for code in $DEBIAN_CODES; do
    v dareprepro include "${code}${release}" "$change"
done

if [ $S -ne 1 ]; then
    for code in $DEBIAN_CODES; do
	tag=$(gettag "$code")
	[ $A -eq 1 ] || v dareprepro include "${code}${release}" "${base}${tag}_i386.changes"
	v dareprepro include "${code}${release}" "${base}${tag}_amd64.changes"
    done
fi

if [ -n "$DEBIAN_CODES_OVERRIDE" ]; then
    echo "Codes overridden, skipping copy of files.  Cleanup by hand."
    exit 0
fi

changes=$change
for code in $DEBIAN_CODES; do
    tag=$(gettag "$code")
    [ $A -eq 1 ] || changes=$changes\ "${base}${tag}"_i386.changes
    changes=$changes\ "${base}${tag}"_amd64.changes
done

files=${change%.changes}.build\ $changes
origfiles=
for change in $changes; do
    files=$files\ $(perl -0ne '$_ =~ /\nFiles: *\n(( [^\n]*\n)*)/; print $1;' "$change" | awk '{print $5}' | grep -v '\.orig\.tar\.gz$' || :)
    origfiles=$origfiles\ $(perl -0ne '$_ =~ /\nFiles: *\n(( [^\n]*\n)*)/; print $1;' "$change" | awk '{print $5}' | grep '\.orig\.tar\.gz$' || :)
done

built=built/
[ -d "$built" ] || mkdir "$built"
mv -i $files "$built"
if [ -n "$origfiles" ]; then
    for orig in $origfiles; do
        [ -e "$built/$orig" ] || cp -ai "$orig" "$built/"
    done
fi
