#!/usr/bin/env bash
#
# Safely update a Debian/Ubuntu‑style system.
# Run as root *or* with sudo (the script checks).

set -euo pipefail

source /opt/scripts_remote/shared_functions.sh

main() {
  require_root

  log "refreshing package lists"
  apt-get update -y

  log "performing full upgrade"
  DEBIAN_FRONTEND=noninteractive \
    apt-get full-upgrade -y \
    -o Dpkg::Options::="--force-confnew"

  log "removing obsolete packages"
  apt-get autoremove -y

  log "cleaning APT cache"
  apt-get clean

  if [[ -f /var/run/reboot-required ]]; then
    log "reboot required — rebooting in 30 s (Ctrl‑C to cancel)"
    sleep 30
    reboot
  else
    log "no reboot needed — done ✅"
  fi
}

main "$@"
