#!/bin/bash

# Make sure they've given us the name of an existing directory.
if [[ ! -d "$1" ]]; then
    echo "${0##*/} <repo-path>"
    exit 0
fi

TMP=$(mktemp --suffix=".sources")
trap "rm -f $TMP" EXIT

# Write our own sources.list entry (deb822 format). We do this to keep apt from
# accessing anything other than the indicated repository, regardless of what's
# in the local /etc/apt/sources.list[.d].
cat > $TMP <<EOF
Types: deb
URIs: file:$1
Suites: unstable
Components: main
Allow-Insecure: yes
Trusted: yes
Check-Valid-Until: no
Check-Date: no
EOF

ARGS="--option Dir::Etc::SourceList=$TMP --option Dir::Etc::SourceParts=/dev/null"

# Run the update, allowing the installation of new packages from the repository
# if necessary. Note that we don't blindly install ALL of the packages in the
# repository, only the ones that somehow relate to packages already installed.
apt update $ARGS && apt upgrade --assume-yes --with-new-pkgs $ARGS
