#!/bin/sh


check_arch() {
    local architecture
    architecture="$(dpkg --print-architecture)"
    # run test only on amd64 because it tests only Python code anyway
    case "$architecture" in
        amd64)
            echo "Running upgrade test on $architecture"
            ;;
        *)
            echo "Skipping upgrade test on $architecture"
            exit 0
            ;;
    esac
}

chroot_exec() {
    local chroot_dir
    chroot_dir="$1"
    shift

    chroot "$chroot_dir" env http_proxy="$http_proxy" eatmydata "$@"
}

do_debootstrap() {
    local chroot_dir distro mirror
    distro="$1"
    chroot_dir="$2"
    mirror="$3"

    debootstrap --include eatmydata,time "$@"

    mount --bind /dev/pts "$chroot_dir/dev/pts"
    mount --bind /proc "$chroot_dir/proc"
    trap "umount \"$chroot_dir/proc\"; umount \"$chroot_dir/dev/pts\"; rm -rf \"$chroot_dir\"" EXIT
}

enable_security_updates() {
    local chroot_dir distro
    chroot_dir="$1"
    distro="$2"

    case "$(dpkg-vendor --query Vendor)" in
        "Ubuntu")
            sed "s/$distro/$distro-security/" < "$chroot_dir/etc/apt/sources.list" \
                > "$chroot_dir/etc/apt/sources.list.d/security.list"
            ;;
        "Debian")
            echo "deb http://security.debian.org/ $distro/updates main" \
                 > "$chroot_dir/etc/apt/sources.list.d/security.list"
            ;;
    esac
}

disable_security_updates() {
    local chroot_dir
    chroot_dir="$1"

    rm "$chroot_dir/etc/apt/sources.list.d/security.list"
}

enable_release_updates() {
    local chroot_dir distro
    chroot_dir="$1"
    distro="$2"

    sed "s/$distro/$distro-updates/" < "$chroot_dir/etc/apt/sources.list" \
        > "$chroot_dir/etc/apt/sources.list.d/updates.list"
}

disable_release_updates() {
    local chroot_dir
    chroot_dir="$1"

    rm "$chroot_dir/etc/apt/sources.list.d/updates.list"
}

upgrade_python_apt() {
    local chroot_dir distro
    chroot_dir="$1"
    distro="$2"

    # Release's most updated python-apt is installed because this is the version
    # systems are running most likely. This also contains latest stability fixes
    # and speed optimizations, thus speed regressions nd improvements can be
    # tracked in autopkgtest runs.
    enable_security_updates "$chroot_dir" "$distro"
    enable_release_updates "$chroot_dir" "$distro"
    chroot_exec "$chroot_dir" apt-get update
    chroot_exec "$chroot_dir" apt-get install -y python3-apt 2>&1
    disable_release_updates "$chroot_dir"
    disable_security_updates "$chroot_dir"
    chroot_exec "$chroot_dir" apt-get update
}

run_u_u() {
    local chroot_dir
    chroot_dir="$1"

    uu_prefix=""
    if chroot_exec "$chroot_dir" which valgrind > /dev/null; then
        uu_prefix="$uu_prefix env PYTHONMALLOC=malloc valgrind"
    fi

    # let the test pass on battery power, too
    echo 'Unattended-Upgrade::OnlyOnACPower "false";' > "$chroot_dir/etc/apt/apt.conf.d/51unattended-upgrades-acpower"
    echo 'Unattended-Upgrade::Skip-Updates-On-Metered-Connections "false";' > "$chroot_dir/etc/apt/apt.conf.d/51unattended-upgrades-metered"

    # test if noninteractive is always passed to dpkg
    echo 'DPkg::Pre-Install-Pkgs:: "echo $DEBIAN_FRONTEND | grep -q noninteractive";' > "$chroot_dir/etc/apt/apt.conf.d/51unattended-upgrades-dpkg-frontend-check"

    # enable a few features to test

    echo 'Unattended-Upgrade::Mail "root";' > "$chroot_dir/etc/apt/apt.conf.d/51unattended-upgrades-mail"
    chroot_exec "$chroot_dir" apt-get update
    chroot_exec "$chroot_dir" unattended-upgrade --download-only
    chroot_exec "$chroot_dir" time unattended-upgrade --verbose --dry-run 2>&1
    chroot_exec "$chroot_dir" $uu_prefix python3 /usr/bin/unattended-upgrade --verbose --debug
    chroot_exec "$chroot_dir" time python3 /usr/bin/unattended-upgrade --verbose 2>&1
    echo "new packages marked as manually installed (should be none): "
    chroot_exec "$chroot_dir" apt-mark showmanual | diff "$chroot_dir/tmp/manual" -
    chroot_exec "$chroot_dir" perl -MMIME::QuotedPrint -pe '$_=MIME::QuotedPrint::decode($_);' /var/mail/mail

    rm "$chroot_dir/etc/apt/apt.conf.d/51unattended-upgrades-dpkg-frontend-check"
}
