1 # This setup hook modifies a Perl script so that any "-I" flags in its shebang
2 # line are rewritten into a "use lib ..." statement on the next line. This gets
3 # around a limitation in Darwin, which will not properly handle a script whose
4 # shebang line exceeds 511 characters.
6 # Each occurrence of "-I /path/to/lib1" or "-I/path/to/lib2" is removed from
7 # the shebang line, along with the single space that preceded it. These library
8 # paths are placed into a new line of the form
10 # use lib "/path/to/lib1", "/path/to/lib2";
12 # immediately following the shebang line. If a library appeared in the original
13 # list more than once, only its first occurrence will appear in the output
14 # list. In other words, the libraries are deduplicated, but the ordering of the
15 # first appearance of each one is preserved.
17 # Any flags other than "-I" in the shebang line are left as-is, and the
18 # interpreter is also left alone (although the script will abort if the
19 # interpreter does not seem to be either "perl" or else "env" with "perl" as
20 # its argument). Each line after the shebang line is left unchanged. Each file
21 # is modified in place.
24 # shortenPerlShebang SCRIPT...
26 shortenPerlShebang
() {
27 while [ $# -gt 0 ]; do
28 _shortenPerlShebang
"$1"
33 _shortenPerlShebang
() {
36 echo "shortenPerlShebang: rewriting shebang line in $program"
38 if ! isScript
"$program"; then
39 die
"shortenPerlShebang: refusing to modify $program because it is not a script"
42 local temp
="$(mktemp)"
46 if (!($0 ~ /\/(perl|env +perl)\>/)) {
47 print "shortenPerlShebang: script does not seem to be a Perl script" > "/dev/stderr"
51 while (match($0, / -I ?([^ ]+)/, pieces)) {
52 matches[idx] = pieces[1]
54 $0 = gensub(/ -I ?[^ ]+/, "", 1, $0)
59 for (idx in matches) {
61 if (!(path in seen)) {
62 printf "%s\"%s\"", prefix, path
73 ' "$program" > "$temp" || die
74 # Preserve the mode of the original file
75 cp --preserve=mode
--attributes-only "$program" "$temp"
78 # Measure the new shebang line length and make sure it's okay. We subtract
79 # one to account for the trailing newline that "head" included in its
81 local new_length
=$
(( $
(head -n 1 "$program" |
wc -c) - 1 ))
83 # Darwin is okay when the shebang line contains 511 characters, but not
84 # when it contains 512 characters.
85 if [ $new_length -ge 512 ]; then
86 die
"shortenPerlShebang: shebang line is $new_length characters--still too long for Darwin!"