#!/bin/sh
#|
cd "`dirname \"$0\"`"
src="configure.ac"
tgt="../configure"
if [ ! -e "$src" ]; then echo "abort: did not find $src"; exit 1; fi
echo "Creating $tgt from $src"
if [ -e "$tgt" ]; then
  /bin/echo -n "overwriting $tgt, Ctrl-C to abort, enter to continue "; read R;
fi
autoconf "$src" | racket -qr "$0" > "$tgt"
chmod +x "$tgt"
exit 0
|#

;; When autoconf produces `configure', it includes many
;;  options that do not apply to Racket.  We want to
;;  get rid of them, so that `configure --help' produces
;;  valid information.
;; In addition, we want none of the feature-selection flags
;;  (such as --enable-mac64) to be passed to sub-configures,
;;  so we adjust the script to strip them away.

(define skip-rxs
  (map (lambda (s)
	 (regexp (format "^  --~a=DIR" s)))
       '(sbindir 
	 libexecdir 
	 sysconfdir
	 sharedstatedir
	 localstatedir
	 oldincludedir
	 infodir
         htmldir
         dvidir
         pdfdir
         psdir
         localedir)))

(let loop ([in-subconfig? #f])
  (let ([l (read-line)])
    (unless (eof-object? l)
      (cond
       [(ormap (lambda (rx)
                 (regexp-match rx l))
               skip-rxs)
        ;; Skip
        (loop in-subconfig?)]
       [(regexp-match #rx"CONFIG_SUBDIRS section[.]" l)
        ;; Copy; now in code to call sub-configures
        (displayln l)
        (loop #t)]
       [(and in-subconfig?
             (regexp-match #rx"--prefix=[*].*[)]" l))
        ;; Found where --prefix is stripped from subconfigure args;
        ;; add a case to remove all --enable and --disable flags
        (displayln l)
        (let ([l2 (read-line)]
              [indent (car (regexp-match #rx" *" l))])
          (displayln l2)
          (printf "~a# Strip away all feature choices\n" indent)
          (printf "~a-enable* | --enable* | -disable* | --disable*)\n" 
                  indent)
          (displayln l2))
        (loop #f)]
       [else
        ;; Copy
        (displayln l)
        (loop in-subconfig?)]))))
