#!/bin/sh
# Copyright © 2012  Andy Whitcroft <apw@ubuntu.com>
#
# schroot is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# schroot is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see
# <http://www.gnu.org/licenses/>.
#
#####################################################################

set -e

. "$SETUP_DATA_DIR/common-data"
. "$SETUP_DATA_DIR/common-functions"
if [ -f "$SETUP_DATA_DIR/common-config" ]; then
    . "$SETUP_DATA_DIR/common-config"
fi

# Do not do anything if this is not a sbuild-launchpad chroot.
if [ -z "$LAUNCHPAD_SERIES" ]; then
    exit 0
fi

# configuration settings supported:
#  apt.enable: defaults to true. disable with false.
#  apt.default.suite: defaults to 'proposed'
#  apt.default.component: defaults to 'universe'
#  apt.default.mirror: the ubuntu mirror to use.
#    default is from the first 'deb' line in sources.list
if [ $STAGE = "setup-start" ] || [ $STAGE = "setup-recover" ]; then
    if [ "$CHROOT_SESSION_PURGE" = 'false' ]; then
        exit 0
    fi
    if [ "${APT_ENABLE:-true}" != "false" ]; then
        sources="${CHROOT_PATH}/etc/apt/sources.list"
        mirror=${APT_DEFAULT_MIRROR}
        series=${LAUNCHPAD_SERIES}
        if [ -z "$mirror" ]; then
            # no apt.default.mirror given, read the first deb line.
            mirror=`awk '$1 == "deb" { print $2; exit(0); }' "$sources"`
            if [ -z "$mirror" ]; then
                fatal "failed to read mirror from $sources"
            fi
            info "Using mirror '$mirror' from <chroot>/etc/apt/sources.list"
        fi

        suite="${APT_DEFAULT_SUITE:-proposed}"
        comp="${APT_DEFAULT_COMPONENT:-universe}"

        # CHROOT_ALIAS is set to the alias used or CHROOT_NAME.
        # it will be of the form: codename[-suite][.component]-arch
        # if only the name is used, then use default components and suite.
        if [ "$CHROOT_NAME" != "$CHROOT_ALIAS" ]; then
            sbase=${CHROOT_ALIAS}
            sbase=${sbase#*-}  # lstrip codename-
            sbase=${sbase%-*}  # rstrip -arch
            case "$sbase" in
                '') ;;
                *.*) # split suite.component
                     suite=${sbase%.*}
                     comp=${sbase#*.}
                     ;;
                *) suite=$sbase;;
            esac
        fi

        case "$suite" in
            release)   suites="" ;;
            security)  suites="security" ;;
            updates)   suites="security updates" ;;
            proposed)  suites="security updates proposed" ;;
            backports) suites="security updates proposed backports" ;;
            *) fatal "unknown suite $suite" ;;
        esac
        case "$comp" in
            main)       comps="main" ;;
            restricted) comps="main restricted" ;;
            universe)   comps="main universe" ;;
            multiverse) comps="main restricted universe multiverse" ;;
            *) fatal "unknown component '$comp" ;;
        esac

        {
            # release pocket is always used.
            echo "deb $mirror $series $comps"
            for suite in $suites; do
                echo "deb $mirror $series-$suite $comps"
            done
        } > "$sources"

        info "Rendered sources.list:"
        while read line; do
            info "  $line"
        done <"$sources"
    fi
fi
