gnuboot/update

133 lines
3.0 KiB
Bash
Executable File

#!/usr/bin/env bash
# generic update scripts for updating configs and such
#
# Copyright (C) 2014, 2015, 2020, 2021 Leah Rowe <info@minifree.org>
# Copyright (C) 2015 Patrick "P. J." McDermott <pj@pehjota.net>
# Copyright (C) 2015, 2016 Klemens Nanni <contact@autoboot.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# 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. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
[ "${DEBUG+set}" = 'set' ] && set -v
set -u -e
projectname="$(cat projectname)"
./resources/scripts/misc/versioncheck
. resources/scripts/misc/sysexits.sh
list_update_paths() {
find resources/packages \
-mindepth 2 -maxdepth 2 \
-type d \
-executable \
-name "update" \
-printf "%P\n"
}
list_packages() {
list_update_paths | \
sed 's#/.*##'
}
# Takes exactly one package as parameter
listoptions() {
package="${1}"
find resources/packages/"${package}/update/" \
-mindepth 1 -maxdepth 1 \
-type f \
-printf "%P\n"
}
help() {
cat <<- EOF
Usage:
./update <PACKAGE> <OPTION>
./update --help
possible values for 'package':
$(list_packages)
Example: ./update coreboot configs
Example: ./update coreboot configs x60
Refer to the ${projectname} documentation for more information.
EOF
}
die() {
ret="$1"
shift 1
printf 'Error: %s\n' "${@}" 1>&2
exit "${ret}"
}
if [ $# -lt 1 ]; then
die "${EX_USAGE}" \
"Wrong number of arguments specified. See './update --help'."
fi
package="${1}"
[ "${package}" = "--help" ] && help && exit 0
if [ $# -gt 1 ]; then
option="${2}"
shift 2
case "${option}" in
list)
if [ ! -d resources/packages/"${package}" ] ; then
die "${EX_USAGE}" \
"Invalid package '${package}'." \
" See './update --help'."
else
printf "Available options for package '%s':\n\n" \
"${package}"
listoptions "${package}"
fi
;;
all)
for option in $(listoptions "${package}"); do
# shellcheck disable=SC2068
resources/packages/"${package}"/update/"${option}" $@
done
;;
*)
if [ -d resources/packages/"${package}"/update ]; then
pkg_dir=resources/packages/"${package}"
if [ -f "${pkg_dir}"/update/"${option}" ]; then
# shellcheck disable=SC2068
"${pkg_dir}"/update/"${option}" $@
else
help
die "${EX_USAGE}" \
"Invalid option for '${package}'." \
" See './update ${package} list'."
fi
else
help
die "${EX_USAGE}" \
"Invalid package '${package}'." \
" See './update --help'."
fi
;;
esac
else
help
exit 0
fi