# #######################################################
# Last review            Fri 04 Jun 2010 02:12:36 AM CDT
# #######################################################

# vim: set ts=8 sw=4 tw=0 ft=sh :

#########################HOW TO USE #####################
# Create a base environment for Ubuntu hardy
# >> sudo DIST=hardy pbuilder create

# Create a base environment for Debian sid
# >> sudo DIST=sid pbuilder create

# Create a base environment for Ubuntu hardy under
# the i386 architecture
# >> sudo DIST=hardy ARCH=i386 pbuilder create

# Update a base environment for Ubuntu hardy
# >> sudo DIST=hardy pbuilder update

# Build a package using Ubuntu hardy as the base
# environment
# >> DIST=hardy pdebuild

# Build a package using Ubuntu hardy as the base
# environment under the i386 architecture
# >> DIST=hardy ARCH=i386 pdebuild
###########################################################


###########################################################
####DONT TOUCH BELOW UNLESS YOU KNOW WHAT ARE YOU DOING####
###########################################################
# https://wiki.ubuntu.com/PbuilderHowto

if [ -z "${PATH_PBUILDER}" ]; then
    PATH_PBUILDER="/var/cache/pbuilder" #without the last "/"
fi

# Codenames for Debian suites according to their alias. Update these when needed.
UNSTABLE_CODENAME="sid"
TESTING_CODENAME="squeeze"
STABLE_CODENAME="lenny"
STABLE_BACKPORTS_SUITE="${STABLE_CODENAME}-backports"

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

# List of Ubuntu suites. Update these when needed.
UBUNTU_SUITES=("xenial" "trusty" "saucy" "raring" "quantal" "precise")

# Mirrors to use. Update these to your preferred mirror.
DEBIAN_MIRROR="ftp.us.debian.org"
UBUNTU_MIRROR="us.archive.ubuntu.com"
#UBUNTU_MIRROR="mirrors.kernel.org" #sometimes it's out of sync

# 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 certain suite values.
    if $(printf "%s\\n" "experimental UNRELEASED" | grep "${DIST}" >/dev/null 2>&1); 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 release states in $DIST to their names.
case "${DIST}" in
    unstable) DIST="${UNSTABLE_CODENAME}" ;;
    testing)  DIST="${TESTING_CODENAME}"  ;;
    stable)   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 [ -n "${ARCH}" ]; then
    NAME="${NAME}-${ARCH}"
    DEBOOTSTRAPOPTS=("--arch" "${ARCH}" "${DEBOOTSTRAPOPTS[@]}")
fi

#################
# Base settings #
#################

###################################################################################
BASETGZ="${PATH_PBUILDER}/${NAME}/${NAME}-base.tgz"
# Optionally, set BASEPATH (and not BASETGZ) if using cowbuilder
# BASEPATH="$PATH_PBUILDER/$NAME/base.cow/"
DISTRIBUTION="${DIST}"
#use it only if you've followed http://viajemotu.wordpress.com/2010/08/10/notas-sobre-pbuilder/
if [ -d "${HOME}/code/ubuntu/results/${NAME}/" ]; then
    export BUILDRESULT="${HOME}/code/ubuntu/results/${NAME}/"
fi
#otherwise uncomment the next line
#export BUILDRESULT="/var/cache/pbuilder/$NAME/result/"
BINDMOUNTS="${BUILDRESULT}"
APTCACHE="${PATH_PBUILDER}/${NAME}/aptcache/"
BUILDPLACE="${PATH_PBUILDER}/${NAME}/build/"
HOOKDIR="${HOME}/.pbuilder-hooks/"
###################################################################################

if $(printf "%s\\n" "${DEBIAN_SUITES[@]}" | grep ${DIST} >/dev/null 2>&1); then
    #debian configuration
    MIRRORSITE="http://${DEBIAN_MIRROR}/debian/"
    COMPONENTS="main contrib non-free"
    #this is for enabling backports for the Debian stable suite.
    if $(printf "%s\\n" "${STABLE_CODENAME} stable" | grep ${DIST} >/dev/null 2>&1); then
        EXTRAPACKAGES="${EXTRAPACKAGES} debian-backports-keyring"
        OTHERMIRROR="${OTHERMIRROR} | deb http://www.backports.org/debian ${STABLE_BACKPORTS_SUITE} ${COMPONENTS}"
    fi
elif $(printf "%s\\n" "${UBUNTU_SUITES[@]}" | grep ${DIST} >/dev/null 2>&1); then
    #ubuntu configuration
    MIRRORSITE="http://${UBUNTU_MIRROR}/ubuntu/"
    COMPONENTS="main restricted universe multiverse"
else
    printf "%s\\n" "Unknown distribution: ${DIST}"
    exit 1
fi

#http://askubuntu.com/questions/323557/resolving-dependencies-inside-a-pbuilder-environment-instead-of-on-the-host-syst
#if the pbuilder env has a different architecture than the local one use aptitude, otherwise gdebi
if [ "${ARCH}" = "$(dpkg --print-architecture)" ]; then
    #gdebi resolution, requires gdebi-core
    if command -v "gdebi" >/dev/null 2>&1; then
        PBUILDERSATISFYDEPENDSCMD="/usr/lib/pbuilder/pbuilder-satisfydepends-gdebi" || true
        rm -rf "${HOOKDIR}/D01install_aptitude_resolver"
    else
        printf "%s\\n" '#!/bin/sh' > "${HOOKDIR}/D01install_aptitude_resolver"
        printf "%s\\n" 'apt-get install -y --no-install-recommends aptitude' >> "${HOOKDIR}/D01install_aptitude_resolver"
        chmod +x "${HOOKDIR}/D01install_aptitude_resolver"
    fi
else
    printf "%s\\n" '#!/bin/sh' > "${HOOKDIR}/D01install_aptitude_resolver"
    printf "%s\\n" 'apt-get install -y --no-install-recommends aptitude' >> "${HOOKDIR}/D01install_aptitude_resolver"
    chmod +x "${HOOKDIR}/D01install_aptitude_resolver"
fi

#faster compress/decompress ratio
if command -v "pigz" >/dev/null 2>&1; then
    COMPRESSPROG="pigz"
fi

# ccache
#sudo mkdir -p "$PATH_PBUILDER/ccache"
#sudo chmod a+w $PATH_PBUILDER/ccache"
if command -v "ccache" >/dev/null 2>&1; then
    export PATH="/usr/lib/ccache:${PATH}"
    export CCACHE_DIR="${HOME}/.ccache_ubuntu"
    EXTRAPACKAGES="ccache ${EXTRAPACKAGES}"
    BINDMOUNTS="${CCACHE_DIR} ${BINDMOUNTS}"
    #if [ -n "${BUILDUSERID}" ]; then
       #chown "${BUILDUSERID}":"${BUILDUSERID}" "${CCACHE_DIR}" || true
    #fi
    #export CCACHE_PREFIX="distcc"
fi