#!/bin/bash
set -e
set -o pipefail

usage() {
    cat << EOF
Usage: openqa-auto-update
Trigger automatic system upgrade and reboot if devel:openQA packages are stable

Options:
 -h, --help         display this help
EOF
    exit "$1"
}

opts=$(getopt -o h --long help -n "$0" -- "$@") || usage 1
eval set -- "$opts"
while true; do
    case "$1" in
        -h | --help) usage 0 ;;
        --)
            shift
            break
            ;;
        *) break ;;
    esac
done

"$(dirname "${BASH_SOURCE[0]}")"/openqa-check-devel-repo
# call ref independently of dup to avoid unintended mass-removals in case ref fails (see poo#150845)
zypper -n ref
zypper -n --no-refresh dup --replacefiles --auto-agree-with-licenses --download-in-advance
# shellcheck disable=SC2015
needs-restarting --reboothint > /dev/null || (command -v rebootmgrctl > /dev/null && rebootmgrctl reboot || :)
