#!/usr/bin/env bash

apt install -y cowbuilder dh-golang ccache pbuilder

DIST=daedalus
# DIST=$(lsb_release --short --codename)
ARCH=$(dpkg --print-architecture)
echo "DIST: " "${DIST}"
echo "ARCH: " "${ARCH}"
DEP_DIR=/srv/pbuilder/deps
HOOK_DIR=/srv/pbuilder/hook.d
PACKAGE_LIST=/srv/pbuilder/deps/Packages

mkdir -p ${DEP_DIR}
mkdir -p ${HOOK_DIR}
touch ${PACKAGE_LIST}

mkdir -p /var/cache/pbuilder/"${DIST}-${ARCH}"/{aptcache,result}

cat > /etc/pbuilderrc <<-'EOF'
	set -x
	# this is your configuration file for pbuilder.
	# the file in /usr/share/pbuilder/pbuilderrc is the default template.
	# /etc/pbuilderrc is the one meant for overwriting defaults in
	# the default template
	#
	# read pbuilderrc.5 document for notes on specific options.

	# Codenames for Debian suites according to their alias. Update these when
	# needed.
	UNSTABLE_CODENAME="$(debian-distro-info --devel)"
	TESTING_CODENAME="${debian-distro-info --testing}"
	STABLE_CODENAME="${debian-distro-info --stable}"
	STABLE_BACKPORTS_SUITE="$STABLE_CODENAME-backports"

	UNSTABLE_DEVUAN="ceres"
	TESTING_DEVUAN="excalibur"
	STABLE_DEVUAN="daedalus"
	OLDSTABLE_DEVUAN="chimaera"
	OLDOLDSTABLE_DEVUAN="beowulf"
	OLDOLDOLDSTABLE_DEVUAN="ascii"

	OLDSTABLE_BACKPORTS_DEVUAN="$OLDSTABLE_DEVUAN-backports"
	OLDSTABLE_PROPOSED_BACKPORTS_DEVUAN="$OLDSTABLE_DEVUAN-proposed-backports"
	OLDSTABLE_PROPOSED_SECURITY_DEVUAN="$OLDSTABLE_DEVUAN-propsed-security"
	OLDSTABLE_PROPOSED_UPDATES_DEVUAN="$OLDSTABLE_DEVUAN-proposed-updates"
	OLDSTABLE_PROPOSED_DEVUAN="$OLDSTABLE_DEVUAN-proposed"
	OLDSTABLE_SECURITY_DEVUAN="$OLDSTABLE_DEVUAN-security"
	OLDSTABLE_UPDATES_DEVUAN="$OLDSTABLE_DEVUAN-updates"

	STABLE_BACKPORTS_DEVUAN="$STABLE_DEVUAN-backports"
	STABLE_PROPOSED_BACKPORTS_DEVUAN="$STABLE_DEVUAN-proposed-backports"
	STABLE_PROPOSED_SECURITY_DEVUAN="$STABLE_DEVUAN-propsed-security"
	STABLE_PROPOSED_UPDATES_DEVUAN="$STABLE_DEVUAN-proposed-updates"
	STABLE_PROPOSED_DEVUAN="$STABLE_DEVUAN-proposed"
	STABLE_SECURITY_DEVUAN="$STABLE_DEVUAN-security"
	STABLE_UPDATES_DEVUAN="$STABLE_DEVUAN-updates"

	TESTING_PROPOSED_SECURITY_DEVUAN="$TESTING_DEVUAN-propsed-security"
	TESTING_PROPOSED_UPDATES_DEVUAN="$TESTING_DEVUAN-proposed-updates"
	TESTING_PROPOSED_DEVUAN="$TESTING_DEVUAN-proposed"
	TESTING_SECURITY_DEVUAN="$TESTING_DEVUAN-security"
	TESTING_UPDATES_DEVUAN="$TESTING_DEVUAN-updates"

	# List of Devuan suites.

	DEVUAN_SUITES=(
	$UNSTABLE_DEVUAN
	$TESTING_DEVUAN
	$STABLE_DEVUAN

	$OLDSTABLE_BACKPORTS_DEVUAN
	$OLDSTABLE_PROPOSED_BACKPORTS_DEVUAN
	$OLDSTABLE_PROPOSED_SECURITY_DEVUAN
	$OLDSTABLE_PROPOSED_UPDATES_DEVUAN
	$OLDSTABLE_PROPOSED_DEVUAN
	$OLDSTABLE_SECURITY_DEVUAN
	$OLDSTABLE_UPDATES_DEVUAN

	$STABLE_BACKPORTS_DEVUAN
	$STABLE_PROPOSED_BACKPORTS_DEVUAN
	$STABLE_PROPOSED_SECURITY_DEVUAN
	$STABLE_PROPOSED_UPDATES_DEVUAN
	$STABLE_PROPOSED_DEVUAN
	$STABLE_SECURITY_DEVUAN
	$STABLE_UPDATES_DEVUAN

	$TESTING_PROPOSED_SECURITY_DEVUAN
	$TESTING_PROPOSED_UPDATES_DEVUAN
	$TESTING_PROPOSED_DEVUAN
	$TESTING_SECURITY_DEVUAN
	$TESTING_UPDATES_DEVUAN
	    "experimental" "unstable" "testing" "stable")

	# List of Debian suites.
	DEBIAN_SUITES=($UNSTABLE_CODENAME $TESTING_CODENAME $STABLE_CODENAME $STABLE_BACKPORTS_SUITE
	    "experimental" "unstable" "testing" "stable")

	# List of Ubuntu suites. Update these when needed.
	UBUNTU_SUITES=("xenial" "wily" "vivid" "utopic" "trusty")

	# Mirrors to use. Update these to your preferred mirror.
	DEBIAN_MIRROR="deb.debian.org"
	UBUNTU_MIRROR="mirrors.kernel.org"
	DEVUAN_MIRROR="deb.devuan.org"

	# Optionally set a default distribution if none is used. Note that you can set
	# your own default (i.e. ${DIST:="unstable"}).
	#: ${DIST:="$(lsb_release --short --codename)"}
	DIST="$(lsb_release --short --codename)"

	# Optionally set the architecture to the host architecture if none set. Note
	# that you can set your own default (i.e. ${ARCH:="i386"}).
	#: ${ARCH:="$(dpkg --print-architecture)"}
	ARCH="$(dpkg --print-architecture)"

	echo "DIST: " "$DIST"
	echo "ARCH: " "$ARCH"

	NAME="$DIST"
	if [ -n "${ARCH}" ]; then
	    NAME="$NAME-$ARCH"
	    DEBOOTSTRAPOPTS=("--arch" "$ARCH" "${DEBOOTSTRAPOPTS[@]}")
	fi


	BASETGZ="/var/cache/pbuilder/$NAME-base.tgz"
	DISTRIBUTION="$DIST"
	BUILDRESULT="/var/cache/pbuilder/$NAME/result/"
	APTCACHE="/var/cache/pbuilder/$NAME/aptcache/"
	BUILDPLACE="/var/cache/pbuilder/build/"
	PBUILDERSATISFYDEPENDSCMD="/usr/lib/pbuilder/pbuilder-satisfydepends-apt"
	CCACHEDIR=/var/cache/pbuilder/ccache
	EATMYDATA=yes

	echo "DIST: " "$DIST"
	echo "ARCH: " "$ARCH"

	if $(echo ${DEVUAN_SUITES[@]} | grep -q $DIST); then
	    # Devuan configuration
	    MIRRORSITE="http://${DEVUAN_MIRROR}/merged/"
	    COMPONENTS="main contrib non-free"

	elif $(echo ${DEBIAN_SUITES[@]} | grep -q $DIST); then
	    # Debian configuration
	    MIRRORSITE="http://$DEBIAN_MIRROR/debian/"
	    COMPONENTS="main contrib non-free"
	    if $(echo "$STABLE_CODENAME stable" | grep -q $DIST); then
		OTHERMIRROR="$OTHERMIRROR | deb $MIRRORSITE $STABLE_BACKPORTS_SUITE $COMPONENTS"
	    fi

	elif $(echo ${UBUNTU_SUITES[@]} | grep -q $DIST); then
	    # Ubuntu configuration
	    MIRRORSITE="http://$UBUNTU_MIRROR/ubuntu/"
	    COMPONENTS="main restricted universe multiverse"
	else
	    echo "Unknown distribution: $DIST"
	    exit 1
	fi

	OTHERMIRROR="deb [trusted=yes] file:///srv/pbuilder/deps ./"
	BINDMOUNTS=/srv/pbuilder/deps
	HOOKDIR=/srv/pbuilder/hook.d
	EXTRAPACKAGES="apt-utils eatmydata ccache"
EOF

# Update local dep sources
# FIXME We add a sources list for the local dep files here because sometimes the one 
# specified in $OTHERMIRROR above does not appear soon enough to allow a successful 
# package build. This leads to a "source listed twice"" warning.
cat > ${HOOK_DIR}/D05deps <<-'EOF'
	#!/bin/bash
	echo "deb [trusted=yes] file:///srv/pbuilder/deps ./" > /etc/apt/sources.list.d/local.list
	(cd /srv/pbuilder/deps; apt-ftparchive packages . > Packages)
	apt update
EOF

chmod +x ${HOOK_DIR}/D05deps

# The debs created by pbuild are generated here so include them as well.
cat > ${HOOK_DIR}/D06deps <<-'EOF'
	LOCAL_REPO="/var/cache/pbuilder/result"

	#!/bin/bash
	echo "deb [trusted=yes] file://${LOCAL_REPO} ./" > /etc/apt/sources.list.d/pbuild-local.list
	(cd ${LOCAL_REPO}; apt-ftparchive packages . > Packages)
	apt update
EOF

chmod +x ${HOOK_DIR}/D06deps

#Update cow image on changes above
#DIST=${DIST} ARCH=${ARCH} git-pbuilder create --override-config --mirror http://deb.devuan.org
if [ -d /var/cache/pbuilder/base-"${DIST}-${ARCH}".cow ]; then
	DIST=${DIST} ARCH=${ARCH} git-pbuilder update 
else
	DIST=${DIST} ARCH=${ARCH} git-pbuilder create
fi


