#!/bin/bash -e

# Run in the Calamares exec phase, after sources-final has written real apt
# sources and after networkcfg has brought the network up.
# Applies all available updates so the installed system is never out of date.

CHROOT=$(mount | grep proc | grep calamares | awk '{print $3}' | sed -e "s#/proc##g")

if ! ping -c 1 -W 5 deb.debian.org >/dev/null 2>&1; then
    echo "system-upgrade: no network connectivity, skipping."
    exit 0
fi

echo "Running system-upgrade: refreshing package lists..."
DEBIAN_FRONTEND=noninteractive chroot "$CHROOT" apt-get update -q

echo "Running system-upgrade: applying available upgrades..."
DEBIAN_FRONTEND=noninteractive chroot "$CHROOT" apt-get dist-upgrade -y -q

echo "system-upgrade complete."
