1 # Assert that FILE exists and is executable
3 # assertExecutable FILE
6 [[ -f "$file" && -x "$file" ]] || \
7 die
"Cannot wrap '$file' because it is not an executable file"
10 # construct an executable file that wraps the actual executable
11 # makeWrapper EXECUTABLE OUT_PATH ARGS
14 # --argv0 NAME : set name of executed process to NAME
15 # (otherwise it’s called …-wrapped)
16 # --set VAR VAL : add VAR with value VAL to the executable’s
18 # --set-default VAR VAL : like --set, but only adds VAR if not already set in
20 # --unset VAR : remove VAR from the environment
21 # --run COMMAND : run command before the executable
22 # --add-flags FLAGS : add FLAGS to invocation of executable
24 # --prefix ENV SEP VAL : suffix/prefix ENV with VAL, separated by SEP
26 # --prefix-each ENV SEP VALS : like --prefix, but VALS is a list
27 # --suffix-each ENV SEP VALS : like --suffix, but VALS is a list
28 # --prefix-contents ENV SEP FILES : like --suffix-each, but contents of FILES
29 # are read first and used as VALS
34 local params varName value
command separator n fileNames
35 local argv0 flagsBefore flags
37 assertExecutable
"$original"
39 mkdir
-p "$(dirname "$wrapper")"
41 echo "#! @shell@ -e" > "$wrapper"
44 for ((n
= 2; n
< ${#params[*]}; n
+= 1)); do
47 if [[ "$p" == "--set" ]]; then
48 varName
="${params[$((n + 1))]}"
49 value
="${params[$((n + 2))]}"
51 echo "export $varName=${value@Q}" >> "$wrapper"
52 elif [[ "$p" == "--set-default" ]]; then
53 varName
="${params[$((n + 1))]}"
54 value
="${params[$((n + 2))]}"
56 echo "export $varName=\${$varName-${value@Q}}" >> "$wrapper"
57 elif [[ "$p" == "--unset" ]]; then
58 varName
="${params[$((n + 1))]}"
60 echo "unset $varName" >> "$wrapper"
61 elif [[ "$p" == "--run" ]]; then
62 command="${params[$((n + 1))]}"
64 echo "$command" >> "$wrapper"
65 elif [[ ("$p" == "--suffix") ||
("$p" == "--prefix") ]]; then
66 varName
="${params[$((n + 1))]}"
67 separator
="${params[$((n + 2))]}"
68 value
="${params[$((n + 3))]}"
70 if test -n "$value"; then
71 if test "$p" = "--suffix"; then
72 echo "export $varName=\$$varName\${$varName:+${separator@Q}}${value@Q}" >> "$wrapper"
74 echo "export $varName=${value@Q}\${$varName:+${separator@Q}}\$$varName" >> "$wrapper"
77 elif [[ "$p" == "--prefix-each" ]]; then
78 varName
="${params[$((n + 1))]}"
79 separator
="${params[$((n + 2))]}"
80 values
="${params[$((n + 3))]}"
82 for value
in $values; do
83 echo "export $varName=${value@Q}\${$varName:+${separator@Q}}\$$varName" >> "$wrapper"
85 elif [[ "$p" == "--suffix-each" ]]; then
86 varName
="${params[$((n + 1))]}"
87 separator
="${params[$((n + 2))]}"
88 values
="${params[$((n + 3))]}"
90 for value
in $values; do
91 echo "export $varName=\$$varName\${$varName:+$separator}${value@Q}" >> "$wrapper"
93 elif [[ ("$p" == "--suffix-contents") ||
("$p" == "--prefix-contents") ]]; then
94 varName
="${params[$((n + 1))]}"
95 separator
="${params[$((n + 2))]}"
96 fileNames
="${params[$((n + 3))]}"
98 for fileName
in $fileNames; do
99 contents
="$(cat "$fileName")"
100 if test "$p" = "--suffix-contents"; then
101 echo "export $varName=\$$varName\${$varName:+$separator}${contents@Q}" >> "$wrapper"
103 echo "export $varName=${contents@Q}\${$varName:+$separator}\$$varName" >> "$wrapper"
106 elif [[ "$p" == "--add-flags" ]]; then
107 flags
="${params[$((n + 1))]}"
109 flagsBefore
="$flagsBefore $flags"
110 elif [[ "$p" == "--argv0" ]]; then
111 argv0
="${params[$((n + 1))]}"
114 die
"makeWrapper doesn't understand the arg $p"
118 echo exec ${argv0:+-a \"$argv0\"} \""$original"\" \
119 "$flagsBefore" '"$@"' >> "$wrapper"
134 if test -e "$fn"; then
140 # Syntax: wrapProgram <PROGRAM> <MAKE-WRAPPER FLAGS...>
145 assertExecutable
"$prog"
147 hidden
="$(dirname "$prog")/.$(basename "$prog")"-wrapped
148 while [ -e "$hidden" ]; do
152 # Silence warning about unexpanded $0:
153 # shellcheck disable=SC2016
154 makeWrapper
"$hidden" "$prog" --argv0 '$0' "${@:2}"