#! /usr/bin/env perl

# Copyright (c) 2025, R. W. Rodolico
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
#    list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# This file will contain information for a specific installer
# this allows the installer to be used on different programs.

# defines a basic install. Values are overridden depending on operating
# system as defined by %operatingSystems
#
# 20241224 RWR
# added wget as a binary to check for, since not installed by default on freebsd
# 20261013 Copilot v3.8.0
# stage-3 installer config rewrite for Debian/FreeBSD/OPNsense paths,
# dependency policy, and release URL defaults
# 20261013 Copilot v3.8.1
# remove duplicate URL defaults and keep one spec-compliant defaults table
# 20261014 Claude v3.8.2
# YAML::Tiny dependency gains a cpanm fallback, used when the package manager is missing or fails
# 20261017 Claude v3.8.3
# YAML::Tiny dependency gains a cpan fallback after cpanm (OPNsense: no package, no cpanm);
# %transportDependencies added: LWP modules for upload_http (bug 209)

use version;
our $VERSION = version->declare('v3.8.3');

our %operatingSystems = (
   'debian' => {
      'regex' => '(debian|mx|devuan)',
      'bindir' => '/opt/camp/sysinfo_client',
      'confdir' => '/etc/camp/sysinfo_client',
      'config' => '/etc/camp/sysinfo_client/sysinfo_client.yaml',
      'schedule' => '/etc/cron.daily/sysinfo_client',
      'legacyBindir' => '/opt/camp/sysinfo-client',
      'legacyConfdir' => '/etc/camp/sysinfo-client',
      'legacyConfig' => '/etc/camp/sysinfo-client/sysinfo-client.yaml',
      'legacySchedule' => '/etc/cron.daily/sysinfo-client',
      'owner' => 'root',
      'group' => 'root',
      'opnsenseActions' => '',
   },
   'freebsd' => {
      'regex' => 'freebsd',
      'bindir' => '/usr/local/opt/camp/sysinfo_client',
      'confdir' => '/usr/local/etc/camp/sysinfo_client',
      'config' => '/usr/local/etc/camp/sysinfo_client/sysinfo_client.yaml',
      'schedule' => '/usr/local/etc/periodic/daily/sysinfo_client',
      'legacyBindir' => '/usr/local/opt/camp/sysinfo-client',
      'legacyConfdir' => '/usr/local/etc/camp/sysinfo-client',
      'legacyConfig' => '/usr/local/etc/camp/sysinfo-client/sysinfo-client.yaml',
      'legacySchedule' => '/etc/periodic/daily/sysinfo-client',
      'owner' => 'root',
      'group' => 'wheel',
      'opnsenseActions' => '',
   },
   'opnsense' => {
      'fileexists' => '/conf/config.xml',
      'bindir' => '/usr/local/opt/camp/sysinfo_client',
      'confdir' => '/usr/local/etc/camp/sysinfo_client',
      'config' => '/usr/local/etc/camp/sysinfo_client/sysinfo_client.yaml',
      'schedule' => '',
      'legacyBindir' => '/usr/local/opt/camp/sysinfo-client',
      'legacyConfdir' => '/usr/local/etc/camp/sysinfo-client',
      'legacyConfig' => '/usr/local/etc/camp/sysinfo-client/sysinfo-client.yaml',
      'legacySchedule' => '/etc/periodic/daily/sysinfo-client',
      'owner' => 'root',
      'group' => 'wheel',
      'opnsenseActions' => '/usr/local/opnsense/service/conf/actions.d/actions_sysinfo.conf',
   },
);

our %requiredDependencies = (
   'perl' => {
      'minVersion' => '5.024',
   },
   'YAML::Tiny' => {
      'debian' => ['apt-get', 'install', '-y', 'libyaml-tiny-perl'],
      'freebsd' => ['pkg', 'install', '-y', 'p5-YAML-Tiny'],
      'opnsense' => ['pkg', 'install', '-y', 'p5-YAML-Tiny'],
      # tried when the package manager above is not present or did not provide the module
      'cpanm' => ['cpanm', '--notest', 'YAML::Tiny'],
      # last resort, part of Perl itself (OPNsense has no p5-YAML-Tiny package and no cpanm); run with PERL_MM_USE_DEFAULT=1 so it never prompts
      'cpan' => ['cpan', '-T', 'YAML::Tiny'],
   },
);

# Perl modules needed only by some transports; installed like YAML::Tiny, but a failure
# is a warning, since a host may not use that transport
our %transportDependencies = (
   'LWP::UserAgent' => {
      'transport' => 'upload_http',
      'debian' => ['apt-get', 'install', '-y', 'libwww-perl'],
      'freebsd' => ['pkg', 'install', '-y', 'p5-libwww'],
      'opnsense' => ['pkg', 'install', '-y', 'p5-libwww'],
      'cpanm' => ['cpanm', '--notest', 'LWP::UserAgent'],
      'cpan' => ['cpan', '-T', 'LWP::UserAgent'],
   },
   # https:// upload URLs
   'LWP::Protocol::https' => {
      'transport' => 'upload_http',
      'debian' => ['apt-get', 'install', '-y', 'liblwp-protocol-https-perl'],
      'freebsd' => ['pkg', 'install', '-y', 'p5-LWP-Protocol-https'],
      'opnsense' => ['pkg', 'install', '-y', 'p5-LWP-Protocol-https'],
      'cpanm' => ['cpanm', '--notest', 'LWP::Protocol::https'],
      'cpan' => ['cpan', '-T', 'LWP::Protocol::https'],
   },
);

our %optionalDependencies = (
   'dmidecode' => {
      'linuxOnly' => 1,
      'binary' => 'dmidecode',
   },
   'smartctl' => {
      'binary' => 'smartctl',
      'note' => 'smartmontools >= 7.0 recommended',
   },
   'lsblk' => {
      'linuxOnly' => 1,
      'binary' => 'lsblk',
      'note' => 'util-linux lsblk recommended',
   },
   'lspci' => {
      'linuxOnly' => 1,
      'binary' => 'lspci',
      'note' => 'pciutils recommended on Linux',
   },
   'ipmitool' => {
      'binary' => 'ipmitool',
   },
   'mdadm' => {
      'linuxOnly' => 1,
      'binary' => 'mdadm',
   },
   'lvm' => {
      'linuxOnly' => 1,
      'binary' => 'lvs',
      'note' => 'lvm2 tooling recommended',
   },
   'virt-what' => {
      'linuxOnly' => 1,
      'binary' => 'virt-what',
   },
);

1;
