#!/bin/sh set -eu usage() { echo "usage: select-module.sh selector|tag VALUE" >&2 exit 2 } [ "$#" -eq 2 ] || usage mode=$1 value=$2 case "$mode" in selector|tag) ;; *) usage ;; esac script_dir=$(CDPATH= cd "$(dirname "$0")" && pwd) mapping=${RELEASEKIT_MODULE_MAP:-$script_dir/release-modules.tsv} [ -f "$mapping" ] || { echo "module map not found: $mapping" >&2; exit 2; } tab=$(printf '\t') matches=0 selected_selector= selected_pattern= selected_module= selected_profile= while IFS="$tab" read -r selector pattern module profile || [ -n "${selector:-}${pattern:-}${module:-}${profile:-}" ]; do case "${selector:-}" in ''|\#*) continue ;; esac fields=$(awk -F '\t' '{ print NF }' <&2; exit 2; } [ -n "$pattern" ] && [ -n "$module" ] && [ -n "$profile" ] || { echo "module map row has an empty field for selector $selector" >&2 exit 2 } matched=false if [ "$mode" = selector ]; then [ "$selector" = "$value" ] && matched=true else # The value remains a shell pattern in this case statement; it is never eval'd. case "$value" in $pattern) matched=true ;; esac fi if [ "$matched" = true ]; then matches=$((matches + 1)) selected_selector=$selector selected_pattern=$pattern selected_module=$module selected_profile=$profile fi done < "$mapping" if [ "$matches" -ne 1 ]; then if [ "$matches" -eq 0 ]; then echo "no module map row matches $mode $value" >&2 else echo "module map has ambiguous rows for $mode $value" >&2 fi exit 2 fi printf '%s\t%s\t%s\t%s\n' \ "$selected_selector" "$selected_pattern" "$selected_module" "$selected_profile"