#!/bin/bash
# Copyright (c) 2022 Bill MacAllister <bill@ca-zephyr.org>
# File: cz-apt-upgrade
# Author: Bill MacAllister
# Description: Perform apt package upgrades

function display_usage {
    echo "Usage: cz-apt-upgrade [update|autoremove|help|manual]"
}

##############################################################################
# Main routine
##############################################################################

# Strip off a remctl sub-command of 'full-upgrade' from the command line.
if [ "$1" == "full-upgrade" ]
then
    shift
fi

case $1 in
    update)
        apt update
        export DEBIAN_FRONTEND=noninteractive
        apt full-upgrade -y \
            -o Dpkg::Options::=--force-confdef \
            -o Dpkg::Options::=--force-confold \
            --fix-missing
        ;;
    autoremove)
        export DEBIAN_FRONTEND=noninteractive
        apt autoremove -y
        ;;
    help)
        display_usage
        ;;
    manual)
        man remctl-apt-full-upgrade
        ;;
    *)
        display_usage
        ;;
esac

exit

DOCS=<<__END_OF_DOCS__

=head1 NAME

cz-apt-upgrade - wrapper for apt full-upgrade

=head1 SYNOPSIS

cz-apt-upgrade [update|autoremove|help|manual]

=head1 DESCRIPTION

This script runs the commands 'apt update' followed by 'apt
full-upgrade'.  The script runs the full-upgrade step with apt and
dpkg switches that attempt to make the full-upgrade run without
requiring any user input.

=head1 ACTIONS

=over 4

=item update

Invoke 'apt update' followed by 'apt full-upgrade'.

=item autoremove

Invoke 'apt autoremove -y'.

=item help

Display script usage.  This is the default action if not other action
is given on the command line.

=item manul

Display the man page for this script.

=back

=head1 AUTHOR

Bill MacAllister <bill@ca-zephyr.org>

=head1 COPYRIGHT

Copyright (C) 2022-2026 Bill MacAllister <bill@ca-zephyr.org>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

=cut

__END_OF_DOCS__
