Patch jerboa-awk regex cache for static build

ober

2acac0b361e5734356f645ed6043f2135f9446a9

diff --git a/scripts/patch-jerboa-shell-compat.sh b/scripts/patch-jerboa-shell-compat.sh
index 0782e86..28ece25 100644
--- a/scripts/patch-jerboa-shell-compat.sh
+++ b/scripts/patch-jerboa-shell-compat.sh
@@ -62,6 +62,47 @@ patch_main() {
   mv "$tmp" "$file"
 }
 
+patch_awk_regex_cache_order() {
+  file="$jsh/vendor/jerboa-awk/lib/jerboa-awk/runtime.sls"
+  [ -f "$file" ] || return 0
+
+  if awk '
+    /define \(make-regex-cache/ { make_line = NR }
+    /define \*regex-cache\*/ { cache_line = NR }
+    END { exit !(make_line && cache_line && make_line < cache_line) }
+  ' "$file"; then
+    return 0
+  fi
+
+  tmp="$file.tmp.$$"
+  awk '
+    $0 == "  (define *regex-cache* (make-regex-cache))" {
+      cache_define = $0
+      next
+    }
+    cache_define != "" && $0 == "" {
+      next
+    }
+    {
+      print
+      if ($0 == "  (define (make-regex-cache)") {
+        in_make = 1
+      } else if (in_make && $0 == "    (make-hashtable equal-hash equal?))") {
+        print ""
+        print cache_define
+        cache_define = ""
+        in_make = 0
+      }
+    }
+    END {
+      if (cache_define != "") {
+        print cache_define
+      }
+    }
+  ' "$file" > "$tmp"
+  mv "$tmp" "$file"
+}
+
 patch_util "$jsh/util.ss"
 patch_util "$jsh/jerboa-shell/util.ss"
 patch_util "$jsh/jsh-src/jsh/util.ss"
@@ -73,3 +114,5 @@ patch_redirect "$jsh/jsh-src/jsh/redirect.ss"
 patch_main "$jsh/main.ss"
 patch_main "$jsh/jerboa-shell/main.ss"
 patch_main "$jsh/jsh-src/jsh/main.ss"
+
+patch_awk_regex_cache_order