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

set -e

verbose=0

## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
progress() {
    if [[ "$verbose" -eq 1 ]]; then
        echo "[progress] $1"
    fi
}
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##

# Parse options
while getopts "v" opt; do
    case "$opt" in
        v) verbose=1 ;;
        *) echo "Usage: $0 [-v]" >&2; exit 2 ;;
    esac
done
shift $((OPTIND - 1))


# Determine architecture
arch=$(dpkg --print-architecture)
progress "Architecture: ${arch}"

# Find the most recent kernel available via the meta-package
meta_pkg="linux-image-${arch}"
progress "Looking up meta-package: ${meta_pkg}"
kernel_dep=$(apt-cache depends "$meta_pkg" 2>/dev/null \
    | grep '^\s*Depends:' \
    | sed 's/.*Depends: //' \
    | grep '^linux-image-')

if [[ -z "$kernel_dep" ]]; then
    echo "ERROR: could not determine latest kernel from ${meta_pkg}" >&2
    exit 1
fi

# Extract kernel version string (strip "linux-image-" prefix)
kernel_version="${kernel_dep#linux-image-}"

progress "Latest kernel version: ${kernel_version}"

# Check for a matching AuriStor module
module_pkg="auristorfs-modules3-${kernel_version}"
progress "Checking for package: ${module_pkg}"

if apt-cache show "$module_pkg" >/dev/null 2>&1; then
    progress "AuriStor module available for latest kernel ${kernel_version}"
    exit 0
fi

# No match -- report what we found
latest_module=$(apt-cache search 'auristorfs-modules3-' 2>/dev/null \
    | grep -v dkms \
    | grep -v cloud \
    | grep -v '\-rt\-' \
    | sed 's/ .*//' \
    | sort -V \
    | tail -1)

if [[ -n "$latest_module" ]]; then
    latest_module_version="${latest_module#auristorfs-modules3-}"
    echo "AuriStor module NOT available for kernel ${kernel_version}" >&2
    echo "Latest AuriStor module is for: ${latest_module_version}" >&2
else
    echo "No AuriStor modules found in apt" >&2
fi

exit 1


: <<'=cut'

=head1 NAME

auristor-client-installable - check whether an AuriStor kernel module is available for the latest kernel

=head1 SYNOPSIS

B<auristor-client-installable> [B<-v>]

=head1 DESCRIPTION

B<auristor-client-installable> determines whether a pre-built AuriStor
kernel module package (B<auristorfs-modules3-*>) exists in the
configured APT repositories for the most recent kernel that would be
installed by the B<linux-image->I<ARCH> meta-package.

The script performs the following steps:

=over 4

=item 1.

Detects the system architecture via B<dpkg --print-architecture>.

=item 2.

Resolves the B<linux-image->I<ARCH> meta-package to find the
concrete kernel package it depends on.

=item 3.

Checks whether a matching B<auristorfs-modules3->I<VERSION> package
is available in APT.

=back

If a matching module package is found the script exits successfully
and silently (unless B<-v> is given).  If no match is found, a
diagnostic message is printed to standard error indicating the kernel
version that lacks a module and, when possible, the most recent
AuriStor module version that I<is> available.

=head1 OPTIONS

=over 4

=item B<-v>

Enable verbose progress messages on standard output.

=back

=head1 EXIT STATUS

=over 4

=item B<0>

An AuriStor module package is available for the latest kernel.

=item B<1>

No matching AuriStor module package was found, or the latest kernel
could not be determined.

=item B<2>

Invalid command-line usage.

=back

=head1 DEPENDENCIES

B<dpkg>(1), B<apt-cache>(1), B<grep>(1), B<sed>(1), B<sort>(1).

=head1 SEE ALSO

B<dpkg>(1), B<apt-cache>(8)

=cut
