#!/bin/sh
# smartupdate — SUM CLI wrapper.
#
# Enforces --silent mode, then delegates all remaining arguments to sumcli.
# Both this script and sumcli must reside in the same directory.
#
# Silent-mode triggers (any of the following are accepted and stripped):
#   /s  -s  --silent  /silent  -silent
#

set -e

CFMCLI="$(dirname "$0")/sumcli"

if [ ! -x "$CFMCLI" ]; then
    echo "Error: sumcli not found at $CFMCLI" >&2
    exit 2
fi

found_silent=0
first=1

for arg in "$@"; do
    if [ "$first" -eq 1 ]; then
        first=0
        set --
    fi
    case "$arg" in
        /s|-s|--silent|/silent|-silent)
            found_silent=1
            ;;
        -h|--help|/help|-help|/h)
            cat <<'EOF'
Usage: smartupdate --silent [OPTIONS]

Update HPE server firmware via SUM.

TARGET OPTIONS:
  --target <ip>               Target server iLO IP address (required)
                              Repeatable for multi-server updates
                              Example: --target 192.168.1.100 --target 192.168.1.101
  --user <username>           iLO username with firmware update privileges (required)
                              Typically 'Administrator' or equivalent role
  --passwd <password>         iLO password for the specified user (required)

FIRMWARE BUNDLE:
  --use_location <path>       Path to SPP ISO file, ZIP file.
                              Supported formats: .iso file or .zip file 
                              Example: /mnt/Gen11SPP2026050000.iso, /mnt/Gen11SPP2026050000.zip

INSTALLATION CONTROL:
  --downgrade, -g             When enabled, any component version higher than the baseline will be downgraded to match the baseline
                              (Default: False; upgrades only; downgrades are skipped)
                              Use when rolling back to an earlier firmware version
  --install_sw_drivers        Install software components along with firmware updates.
                              (Default: False)
  --power_off                 After a firmware update is complete, power off the server
                              (Default: False; server remains powered on)
                              Use when maintenance window allows for shutdown
  --dryrun                    Analyze what would be updated without making actual changes
                              Performs pre-flight checks and shows components to be updated
                              No server modifications or reboots occur
  --continue_on_error         Continue processing remaining targets if some fail
                              Requires at least one target to succeed
                              Exit code reflects successful targets when enabled

INPUT FILE:
  --inputfile <path>          Path to configuration file with settings and multiple targets
                              Format: KEY=VALUE pairs (case-insensitive), one per line
                              Supports [TARGETS] blocks with HOST, UID, PWD for each server
                              Each [TARGETS] block must end with [END]
                              Supported keys: SILENT, USE_LOCATION, DOWNGRADE, INSTALL_SW_DRIVERS,
                                             POWER_OFF, DRYRUN, CONTINUE_ON_ERROR, VERBOSE
                              CLI flags override inputfile values
  --deleteinputfile           Automatically delete the input file after successful processing
                              Use for security when file contains credentials

OTHER:
  --verbose, -v               Enable verbose logging for troubleshooting
                              Shows detailed progress and API responses
  --help, -h                  Show this help message and exit

DEFAULTS:
  By default, smartupdate updates firmwares components

EXAMPLES:
  # Basic firmware update
  smartupdate --silent --target 192.168.1.100 --user admin --passwd secret --use_location /mnt/spp.iso

  # Include drivers and software during firmware baseline update
  smartupdate --silent --target 192.168.1.100 --user admin --passwd secret --use_location /mnt/spp.iso --install_sw_drivers

  # When enabled, any component version higher than the baseline will be downgraded to match the baseline
  smartupdate --silent --target 192.168.1.100 --user admin --passwd secret --use_location /mnt/spp.iso --downgrade

  # After a firmware update is complete, power off the server
  smartupdate --silent --target 192.168.1.100 --user admin --passwd secret --use_location /mnt/spp.iso --power_off

  # Dry run to preview changes without applying them
  smartupdate --silent --target 192.168.1.100 --user admin --passwd secret --use_location /mnt/spp.iso --dryrun

  # Use input file for configuration with multiple targets
  smartupdate --silent --inputfile /tmp/update.conf

  # Example input file (update.conf):
  #   SILENT=YES
  #   CONTINUE_ON_ERROR=YES
  #   USE_LOCATION=/home/baselines/Gen11SPP2026030000.iso
  #   DOWNGRADE=YES
  #   POWER_OFF=YES
  #   INSTALL_SW_DRIVERS=YES
  #   VERBOSE=YES
  #   [TARGETS]
  #   HOST=192.168.1.100
  #   UID=Administrator
  #   PWD=secret123
  #   [END]
  #   [TARGETS]
  #   HOST=192.168.1.101
  #   UID=Administrator
  #   PWD=secret456
  #   [END]
EOF
            exit 0
            ;;
        *)
            set -- "$@" "$arg"
            ;;
    esac
done

if [ "$found_silent" -eq 0 ]; then
    echo "Error: --silent (-s) is required." >&2
    echo "Usage: smartupdate --silent [OPTIONS]  (use --help for full usage)" >&2
    exit 1
fi

# exec replaces this process with sumcli
exec "$CFMCLI" "$@"
