#! /bin/sh
set -e

. /usr/share/debconf/confmodule

ARCH="$(udpkg --print-architecture)"
BASECACHE=/target/var/cache/apt/archives
DESKTOPCACHE=/target/var/cache/archive-copier/desktop
SHIPCACHE=/target/var/cache/archive-copier/ship

log () {
    logger -t archive-copier "$@"
}

error () {
    log "error: $@"
}

if [ ! -f /cdrom/.disk/base_installable ]; then
    log "not installing from base-installable CD-ROM"
    exit 0
fi

if ! db_get mirror/suite; then
    log "required mirror/suite not set"
    exit 0
fi
SUITE="$RET"

db_capb backup

db_input medium archive-copier/copy || [ $? -eq 30 ]
if ! db_go; then
    exit 10 # back up to menu
fi

db_get archive-copier/copy
if [ "$RET" = false ]; then
    log "user requested no copy"
    exit 0
fi

# Get list of files; must do this first in order to know how long the
# progress bar should be.

zcat "/cdrom/dists/$SUITE/main/binary-$ARCH/Packages.gz" \
    >/tmp/archive-copier.packages
BASE="$(debootstrap --print-debs "$SUITE")"
# TODO: for Debian, using $SUITE for debootstrap wouldn't work (unstable vs.
# sid).
/usr/lib/archive-copier/package-cache-names /tmp/archive-copier.packages \
    ubuntu-desktop $BASE >/tmp/archive-copier.list

NUM_FILES=`wc -l /tmp/archive-copier.list | sed 's/^ *//' | cut -d' ' -f1`

db_progress START 0 "$NUM_FILES" archive-copier/progress

die () {
    template="$1"
    shift

    error "$@"
    db_input critical "$template" || [ $? -eq 30 ]
    db_go || true
    db_progress STOP
    rm -f /tmp/archive-copier.packages /tmp/archive-copier.list
    exit 1
}

# Current policy is:
#   never copy Base
#   copy Desktop and Ship except in custom mode

copybase=no
if db_get ubuntu/install-type && [ "$RET" = custom ]; then
    copydesktop=no
    copyship=no
else
    copydesktop=yes
    copyship=yes
fi

# Copy everything. Be paranoid about failures.
# Using fd 9 is a bit ugly; debconf gets in the way of random other fds.

if [ "$copybase" = yes ]; then
    mkdir -p "$BASECACHE" || \
	die archive-copier/copy-failed "'mkdir -p \"$BASECACHE\"' failed with code $?"
fi
if [ "$copydesktop" = yes ]; then
    mkdir -p "$DESKTOPCACHE" || \
	die archive-copier/copy-failed "'mkdir -p \"$DESKTOPCACHE\"' failed with code $?"
fi
if [ "$copyship" = yes ]; then
    mkdir -p "$SHIPCACHE" || \
	die archive-copier/copy-failed "'mkdir -p \"$SHIPCACHE\"' failed with code $?"
fi

while read filename version status <&9; do
    base="${filename##*/}"

    package="${base%%_*}"
    trailer="${base##*_}"

    case $status in
	base)
	    [ "$copybase" = yes ] || continue
	    cachedir="$BASECACHE"
	    ;;
	desktop)
	    [ "$copydesktop" = yes ] || continue
	    cachedir="$DESKTOPCACHE"
	    ;;
	ship)
	    [ "$copyship" = yes ] || continue
	    cachedir="$SHIPCACHE"
	    ;;
	*)
	    continue
	    ;;
    esac
    cachebase="${package}_${version}_${trailer}"

    db_progress STEP 1
    db_subst archive-copier/progress/package PACKAGE "$package"
    db_progress INFO archive-copier/progress/package
    cp -a "/cdrom/$filename" "$cachedir/$cachebase" || \
	die archive-copier/copy-failed \
	    "'cp -a \"/cdrom/$filename\" \"$cachedir/$cachebase\"' failed with code $?"
done 9</tmp/archive-copier.list

rm -f /tmp/archive-copier.packages /tmp/archive-copier.list

db_progress STOP

exit 0
