#!/usr/bin/env bash
# Code generated by Claude Code

set -e

# ## # ## # ## # ## # ## # ## # ## # ## # ## # ## # ## # ## # ## # ##
logme() {
    msg="$1"
    logger -t upgrade-kernel "$msg"
    if [[ "$VERBOSE" = true ]]; then
        echo "$msg"
    fi
}

log_finished() {
    logme "all done"
}

send_mail() {
    local subject="$1"
    local body="$2"
    if [[ -n "$MAIL_TO" ]]; then
        logme "sending mail to $MAIL_TO"
        echo "$body" | mail -s "safe-kernel-upgrade: $subject" "$MAIL_TO"
    else
        logme "no mail recipient specified; skipping mail"
    fi
}

# ## # ## # ## # ## # ## # ## # ## # ## # ## # ## # ## # ## # ## # ##

MAIL_TO=""
VERBOSE=false

while [[ $# -gt 0 ]]; do
    case "$1" in
        --mail-to)
            MAIL_TO="$2"
            shift 2
            ;;
        --verbose|-v)
            VERBOSE=true
            shift
            ;;
        *)
            echo "unknown option: $1" >&2
            exit 1
            ;;
    esac
done

logme "starting"

# Run auristor-client-installable first
aci_rc=0
aci_output=$(auristor-client-installable -v 2>&1) || aci_rc=$?
if [[ $aci_rc -ne 0 ]]; then
    msg="AuriStor client module is not available for the latest kernel"
    logme "$msg"

    send_mail "AuriStor module not yet available" "$aci_output"
    log_finished
    exit 1
fi

# If we get here then we know a compatible AuriStor client module is available
# for installation.
export DEBIAN_FRONTEND=noninteractive
apt_output=$(apt-get -y upgrade 2>&1)
logme "finished 'apt-get -y upgrade'"

# Reboot (if necessary)
if [[ -f /var/run/reboot-required ]]; then
    msg="reboot required; will reboot now"
    logme "$msg"

    send_mail "upgrade complete, rebooting" "$apt_output"
    log_finished
    reboot
else
    msg="no reboot required"
    logme "$msg"

    send_mail "upgrade complete, no reboot required" "$apt_output"
fi

log_finished
exit 0


: <<'=cut'

=head1 NAME

safe-kernel-upgrade - upgrade packages only when a compatible AuriStor kernel module is available

=head1 SYNOPSIS

B<safe-kernel-upgrade> [B<--mail-to> I<address>] [B<--verbose>|B<-v>]

=head1 DESCRIPTION

B<safe-kernel-upgrade> guards an B<apt-get upgrade> behind a
pre-flight check that ensures a pre-built AuriStor kernel module is
available for the latest kernel.  This prevents the system from
upgrading to a kernel that would lack AuriStor filesystem support.

The script performs the following steps:

=over 4

=item 1.

Runs B<auristor-client-installable -v> to verify that an
B<auristorfs-modules3-*> package exists for the latest kernel.
If the check fails the script sends a notification email (if
B<--mail-to> was specified) and exits without upgrading.

=item 2.

Runs B<apt-get -y upgrade> with B<DEBIAN_FRONTEND> set to
C<noninteractive>.

=item 3.

Sends a notification email (if B<--mail-to> was specified) with the
output of B<apt-get upgrade> as the message body.

=item 4.

If F</var/run/reboot-required> exists after the upgrade, the
system is rebooted immediately.

=back

All progress messages are written to syslog (facility C<user>, tag
C<upgrade-kernel>).  With B<--verbose>, messages are also written to
standard output.

=head1 OPTIONS

=over 4

=item B<--mail-to> I<address>

Send a notification email to I<address> after each run.  The subject
line begins with C<safe-kernel-upgrade:> followed by a short summary.
The message body contains the output of B<apt-get -y upgrade> (or, if
the AuriStor pre-flight check failed, the output of
B<auristor-client-installable>).  If this option is not specified, no
email is sent and a message is logged to that effect.

=item B<--verbose>, B<-v>

Print progress messages to standard output in addition to syslog.

=back

=head1 EXIT STATUS

=over 4

=item B<0>

The upgrade completed successfully and no reboot was required.

=item B<1>

The AuriStor module check failed and no upgrade was performed.

=back

=head1 FILES

=over 4

=item F</var/run/reboot-required>

Checked after the upgrade to decide whether to reboot.

=back

=head1 DEPENDENCIES

B<auristor-client-installable>(1), B<apt-get>(8), B<logger>(1),
B<mail>(1), B<reboot>(8).

=head1 SEE ALSO

B<auristor-client-installable>(1), B<apt-get>(8), B<mail>(1),
B<reboot>(8)

=cut
