1#!/bin/sh
2
3#-
4# SPDX-License-Identifier: BSD-2-Clause
5#
6# Copyright 2023 Beckhoff Automation GmbH & Co. KG
7# Copyright 2023 Bjoern A. Zeeb
8#
9# Redistribution and use in source and binary forms, with or without
10# modification, are permitted providing that the following conditions
11# are met:
12# 1. Redistributions of source code must retain the above copyright
13#    notice, this list of conditions and the following disclaimer.
14# 2. Redistributions in binary form must reproduce the above copyright
15#    notice, this list of conditions and the following disclaimer in the
16#    documentation and/or other materials provided with the distribution.
17#
18# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28# POSSIBILITY OF SUCH DAMAGE.
29
30: ${LIBEXEC_PATH:="/usr/libexec/fwget"}
31
32usage()
33{
34	cat <<EOF
35Usage: $(basename "$0") [options] [subsystem]
36
37Supported subsystems
38  pci
39
40Options:
41  -n		-- Do not install package, only print the results
42  -v		-- More verbose
43EOF
44	exit 1
45}
46
47log()
48{
49	echo "$@"
50}
51
52log_verbose()
53{
54	if [ "${VERBOSE}" = "n" ]; then
55		return
56	fi
57
58	echo "$@"
59}
60
61addpkg()
62{
63	local _p
64
65	_p=$1
66
67	case "${packages}" in
68	"")	packages="${_p}" ;;
69	*)	# Avoid duplicates.
70		case " ${packages} " in
71		*\ ${_p}\ *) ;;	# duplicate
72		*)	packages="${packages} ${_p}" ;;
73		esac
74	esac
75}
76
77DRY_RUN=n
78VERBOSE=n
79
80while [ $# -gt 0 ]; do
81	case $1 in
82		-n)
83			DRY_RUN=y
84			;;
85		-v)
86			VERBOSE=y
87			;;
88		*)
89			subsystems="${subsystems} $1"
90			;;
91	esac
92	shift
93done
94
95# Default searching PCI subsystem
96if [ -z "${subsystems}" ]; then
97	subsystems="pci"
98fi
99
100# Fail early on unsupported subsystem
101for subsystem in ${subsystems}; do
102	if [ ! -f "${LIBEXEC_PATH}"/"${subsystem}" ]; then
103		usage
104	fi
105	. "${LIBEXEC_PATH}"/"${subsystem}"
106done
107
108packages=""
109for subsystem in ${subsystems}; do
110	"${subsystem}"_search_packages
111done
112
113case "${packages}" in
114""|^[[:space:]]*$)
115	echo "No firmware packages to install."
116	exit 0
117	;;
118esac
119
120echo "Needed firmware packages: '${packages}'"
121if [ "${DRY_RUN}" = "y" ]; then
122	exit 0
123fi
124
125pkg install -qy ${packages}
126