#!/bin/bash
set -e

if [ "$#" -lt 1 ]; then
    echo "Error: Missing parameters." >&2
    echo "Usage: $0 <module name>" >&2
    echo "Example: $0 app" >&2
    exit 1
fi

# Make sure the device time is correct before starting update
while [ ! -f /run/systemd/timesync/synchronized ]; do
    sudo /usr/bin/timedatectl set-ntp true
    sleep 2
done
# Fix potential previous interrupted installation
sudo /usr/bin/dpkg --configure -a
# Sync with remote repository
sudo /usr/bin/apt update
# Install updates
sudo /usr/bin/apt install -y --allow-downgrades 10trace-$1
sudo /usr/bin/timedatectl set-ntp false