#!/bin/sh

# Linux Vulnerability Mitigation
# Copyright (C) 2026 Daniel Baumann <daniel@debian.org>
#
# SPDX-License-Identifier: PD
#
# This program is free software: you have unlimited permission
# to copy, distribute and modify it.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

set -e

PROGRAM="$(basename "${0}")"

# Parameter
PARAMETER="${1}"

if [ -z "${PARAMETER}" ]
then
	echo "Usage: ${PROGRAM} COMMAND [OPTIONS]" >&2
	echo
	echo "Available commands:"

	COMMANDS="$(find /usr/libexec/linux-vulnerability-mitigation -mindepth 1 -printf '%f\n' | sort -V)"

	for COMMAND in ${COMMANDS}
	do
		echo " ${COMMAND}"
	done

	echo
	echo "See ${PROGRAM}(1) and ${PROGRAM}(7) for more information."

	exit 1
fi

# Command
COMMANDS="${1}"

# Options
shift 1
OPTIONS="${*}"

for COMMAND in $(echo "${COMMANDS}" | sed -e 's|,| |g')
do
	if [ ! -e "/usr/libexec/${PROGRAM}/${COMMAND}" ]
	then
		echo "'${COMMAND}': no such ${PROGRAM} command, see ${PROGRAM}(1)." >&2
		exit 1
	fi

	# Run
	"/usr/libexec/${PROGRAM}/${COMMAND}" "${OPTIONS}"
done
