# Codenames for Debian suites according to their alias. Update these when
# needed.
UNSTABLE_CODENAME="sid"
TESTING_CODENAME="jessie"
STABLE_CODENAME="wheezy"

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

# List of Ubuntu suites. Update these when needed.
UBUNTU_SUITES=("lucid" "precise" "quantal" "raring" "saucy" "trusty")
case "$dist" in
    t)
        dist="trusty"
        ;;
    s)
        dist="saucy"
        ;;
    r)
        dist="raring"
        ;;
    q)
        dist="quantal"
        ;;
    p)
        dist="precise"
        ;;
    l)
        dist="lucid"
        ;;
esac

# Mirrors to use. Update these to your preferred mirror.
DEBIAN_MIRROR="ftp.pl.debian.org"
UBUNTU_MIRROR="ftp.vectranet.pl"

# Optionally use the changelog of a package to determine the suite to use if
# none set.
if [ -z "${dist}" ] && [ -r "debian/changelog" ]; then
    dist=$(dpkg-parsechangelog | awk '/^Distribution: / {print $2}')
    # Use the unstable suite for Debian experimental packages.
    if [ "${dist}" == "experimental" ]; then
        dist="$UNSTABLE_CODENAME"
    fi
fi

# 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)"}

# Optionally change Debian codenames in $dist to their aliases.
case "$dist" in
    $UNSTABLE_CODENAME)
        dist=$UNSTABLE_CODENAME
        ;;
    $TESTING_CODENAME)
        dist=$TESTING_CODENAME
        ;;
    $STABLE_CODENAME)
        dist=$STABLE_CODENAME
        ;;
esac

# 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)"}

NAME="$dist"

if [ ! -z "$pro" ]; then
    NAME="$NAME-$pro"
fi

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

if [ -n "$main" ]; then
    COMPONENTS="main restricted"
    NAME="$NAME-main"
else
    COMPONENTS="main restricted universe multiverse"
fi

# Edit the paths here, I usually keep it in my /home
BASETGZ="/home/${USER}/packaging/pbuilder/$NAME-base.tgz"
BASEPATH="/home/${USER}/packaging/pbuilder/$NAME.cow"
DISTRIBUTION="$dist"
BUILDRESULT="/home/${USER}/packaging/pbuilder/$NAME/result/"
APTCACHE="/var/cache/apt/archives/"
BUILDPLACE="/home/${USER}/packaging/pbuilder/buildplace"
PBUILDERROOTCMD="sudo -E"

if $(echo ${DEBIAN_SUITES[@]} | grep -q $dist); then
    # Debian configuration
    MIRRORSITE="http://$DEBIAN_MIRROR/debian/"
    COMPONENTS="main contrib non-free"
    DEBOOTSTRAPOPTS=(
    "${DEBOOTSTRAPOPTS[@]}"
    '--keyring=/usr/share/keyrings/debian-archive-keyring.gpg'
    )
elif $(echo ${UBUNTU_SUITES[@]} | grep -q $dist); then
    # Ubuntu configuration
    MIRRORSITE="http://archive.ubuntu.com/ubuntu/"
    OTHERMIRROR="deb http://$UBUNTU_MIRROR/ubuntu/ $dist $COMPONENTS|deb http://$UBUNTU_MIRROR/ubuntu/ $dist-updates $COMPONENTS|deb http://$UBUNTU_MIRROR/ubuntu/ $dist-proposed $COMPONENTS|deb http://archive.ubuntu.com/ubuntu/ $dist-updates $COMPONENTS|deb http://archive.ubuntu.com/ubuntu/ $dist-proposed $COMPONENTS"
else
    echo "Unknown distribution: $dist"
    exit 1
fi


# Get our magical hook: bzr branch lp:~kubuntu-members/pbuilder/pbuilder-hooks
# Change the directory of course :)
HOOKDIR="/home/${USER}/packaging/pbuilder/pbuilder-hooks"
# Editor of choice
export EDITOR="vim"
# Make debhelper more verbose
export DH_VERBOSE=1
PKGNAME_LOGFILE_EXTENTION="_$(dpkg --print-architecture).build"
PKGNAME_LOGFILE=yes
# Use parallel gzip to compress stuff
COMPRESSPROG=pigz
# Allow to install packages from unsigned repositories - mostly PPAs
ALLOWUNTRUSTED=yes

APTCACHEHARDLINK=no

# Always include the source in the changes file
DEBBUILDOPTS="-sa"

if [ -z "$main" ]; then
    export DEB_BUILD_OPTIONS="parallel=5"
    export CCACHE_DIR=/home/quintasan/packaging/pbuilder/ccache/
    export CCACHE_NLEVELS=1
    export CCACHE_COMPRESS=1
        if [ "$dist" != "lucid" ]; then
        EXTRAPACKAGES="eatmydata vim"
        export LD_PRELOAD=/usr/lib/libeatmydata/libeatmydata.so
    else
        EXTRAPACKAGES="vim"
    fi
else
    export DEB_BUILD_OPTIONS="parallel=5"
    export CCACHE_DISABLE=1
fi

unset CC
unset CXX

# Put at bottom
[ -e ~/.pbuilderrc.local ] && . ~/.pbuilderrc.local || true