#!/bin/bash
#
# Copyright (C) 2023 Canonical Ltd.
# Author: Bryce Harrington <bryce@canonical.com>

set -efu

# Examine the package's deb for what it installs
commands=$(dpkg -L sg3-utils | grep /usr/bin)

exceptions=(
    '/usr/bin/scsi_satl'
    '/usr/bin/sg_dd'
    '/usr/bin/sg_emc_trespass'
    '/usr/bin/sg_map'
    '/usr/bin/sg_rdac'
    '/usr/bin/sg_scan'
    '/usr/bin/sginfo'
)

ret=0
for cmd in ${commands}; do
    if [ ! -f "${cmd}" ]; then
        continue
    fi
    test -e "${cmd}"
    if "${cmd}" --help > /dev/null 2>&1; then
        echo "${cmd}: OK"
    else
        found=
        for e in "${exceptions[@]}"; do
            if [ "${e}" = "${cmd}" ]; then
                found="yes"
            fi
        done
        if [ "${found}" = "yes" ]; then
            # This command is expected to emit a non-zero exit code for --help,
            # so treat it as a non-error (just an inconsistency, thus XFAIL).
            echo "${cmd}: XFAIL"
        else
            echo "${cmd}: ERROR: No --help option?"
            ret=1
        fi
    fi
done

exit "${ret}"
