#!/bin/bash

# Usage (e.g.): sbuild --chroot-sources=/path/to/chroot-sources

# Adjust /etc/apt/sources.list.d/debathena.list using the
# DEBATHENA_RELEASE environment variable (set to either "production",
# "-proposed", or "-development"),
#
# If DEBATHENA_RELEASE is unset, assume we're building out of -development.
# DEBATHENA_BUILD_DIST is the target build distribution.  Note:
# SBUILD_BUILD_CONF_DISTRIBUTION is not set as of sbuild 0.60.6 or
# later.  An environment_filter in ~/.sbuildrc must be configured to
# allow passing of this variable.

: ${DEBATHENA_RELEASE=-development}
: ${DEBATHENA_MIRROR:=http://debathena.mit.edu/apt}
: ${DEBATHENA_BUILD_DIST:="$SBUILD_BUILD_CONF_DISTRIBUTION"}
if [ -z "$DEBATHENA_BUILD_DIST" ]; then
    echo "Warning: DEBATHENA_BUILD_DIST is unset or empty!" >&2
    if [ -r /etc/lsb-release ]; then
	echo "Attempting to guess dist from lsb-release..." >&2
	. /etc/lsb-release
	if [ -n "$DISTRIB_CODENAME" ]; then
	    DEBATHENA_BUILD_DIST="$DISTRIB_CODENAME"
	fi
    fi
    if [ -z "$DEBATHENA_BUILD_DIST" ] && echo "$SCHROOT_CHROOT_NAME" | \
	egrep -q "^[a-z]+-(amd64|i386)(-sbuild)?$"; then
	echo "Attempting to guess dist from chroot name..." >&2
	DEBATHENA_BUILD_DIST=$(echo "$SCHROOT_CHROOT_NAME" | cut -d\- -f 1)
    fi
fi
if [ -z "$DEBATHENA_BUILD_DIST" ]; then
    echo "*** ERROR: Cannot determine distribution to build.  Stop." >&2
    exit 1
fi
dist="$DEBATHENA_BUILD_DIST"
list=/etc/apt/sources.list.d/debathena.list

rm -f "$list"

cat >>"$list" <<EOF
deb $DEBATHENA_MIRROR $dist debathena debathena-config
deb-src $DEBATHENA_MIRROR $dist debathena debathena-config
EOF

if [ "$DEBATHENA_RELEASE" = "-staging" ]; then
    cat >>"$list" <<EOF
deb $DEBATHENA_MIRROR ${dist}-staging debathena debathena-config
deb-src $DEBATHENA_MIRROR ${dist}-staging debathena debathena-config
EOF
    DEBATHENA_RELEASE="-development"
fi

if [ "$DEBATHENA_RELEASE" = "-proposed" ] || [ "$DEBATHENA_RELEASE" = "-development" ]; then
    cat >>"$list" <<EOF
deb $DEBATHENA_MIRROR ${dist}-proposed debathena debathena-config
deb-src $DEBATHENA_MIRROR ${dist}-proposed debathena debathena-config
EOF
fi

if [ "$DEBATHENA_RELEASE" = "-development" ]; then
    cat >>"$list" <<EOF
deb $DEBATHENA_MIRROR ${dist}-development debathena debathena-config
deb-src $DEBATHENA_MIRROR ${dist}-development debathena debathena-config
EOF
fi

apt-key add /afs/sipb.mit.edu/project/debathena/apt/debathena-archive-keyring.asc
dpkg --remove pkg-create-dbgsym

DEBIAN_FRONTEND=noninteractive apt-get update
