#!/usr/bin/perl
#
# cz-node-info - return information about a debian host
#
# Copyright 2026 CZ Software - All rights reserved.
# Author: Bill MacAllister <whm@dropbox.com>

use Getopt::Long;
use Pod::Usage;
use strict;

my $opt_debug;
my $opt_help;
my $opt_manual;

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

GetOptions(
    'debug'  => \$opt_debug,
    'help'   => \$opt_help,
    'manual' => \$opt_manual
);

# -- help the poor souls out
pod2usage(-verbose => 2) if $opt_manual;
pod2usage(-verbose => 0) if $opt_help || ($ARGV[0] && $ARGV[0] eq 'help');

my $puppet_conf      = '/etc/cz-puppet.conf';
my $kernel_running   = `uname -r`;
my @kernel_installed = `dpkg -l | grep linux-image`;

print("Running Kernel: $kernel_running\n");
print("Installed Kernels:\n");
for my $k (@kernel_installed) {
    if ($k =~ /^(\S+) \s* (\S+) \s* (\S+)/xms) {
        my $state = $1;
        my $kname = $2;
        my $kver  = $3;
        printf("%-4s %-32s %-16s\n", $state, $kname, $kver);
    }
}

print("\n");
print("Puppet ");
if (-e $puppet_conf) {
    my $cmd             = "grep MANIFEST_ID $puppet_conf";
    my $puppet_manifest = `$cmd`;
    if ($puppet_manifest =~ /MANIFEST_ID=(\S+)/xms) {
        my $pup = $1;
        print("Manifest: $pup\n");
    } else {
        print("ERROR: MANIFEST_ID not found in $puppet_conf\n");
    }
} else {
    print("ERROR: $puppet_conf not found\n");
}

print("\n");
print("Debian:\n");

my %repos         = ();
my $deb_repo_file = '/etc/apt/sources.list';
if (-e $deb_repo_file) {
    open(my $fd, '<', $deb_repo_file)
      or die("ERROR: problem opening $deb_repo_file\n");
    while (<$fd>) {
        chomp;
        my $inline = $_;
        if ($inline =~ /^deb \s+ (\S+) \s+ (\S+)/xms) {
            my $repo     = $1;
            my $repoName = $2;
            $repos{$repoName} += 1;
        }
    }
    close($fd)
      or die("ERROR: problem closing $deb_repo_file\n");
    if (scalar(keys %repos)) {
        for my $repo (sort keys %repos) {
            print("  $repo ($repos{$repo})\n");
        }
    } else {
        print("ERROR: no repositories found in $deb_repo_file\n");
    }
} else {
    print("ERROR: $deb_repo_file not found\n");
}

exit;

__END__

=head1 NAME

cz-node-info - Return software information about a debian host

=head1 SYNOPSIS

cz-node-info [--debug] [--help] [--manual]

=head1 DESCRIPTION

This script returns information about the running kernel, the
installed kernels, the puppet manifest, and the debian configured
Debian repositories.  The script is specific to ca-zephyr.org hosts
but will run on any debian system.  It is intended as the target
of remctl.

=head1 ARGUMENTS

=over 4

=item --help

Display usage information.

=item --manual

Display the man page for this script.

=item --debug

Display debugging information.  Currently there is no debugging
output.

=back

=head1 COPYRIGHT

Copyright (c) 2023 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.

=head1 AUTHORS

Bill MacAllister <whm@dropbox.com>

=cut
