#!/bin/sh
exec guile -s $0 "$@"
!#

;; Spit out the environment variable settings needed based on
;; arguments listing gnc-module-dirs, guile-load-dirs, and
;; library-dirs

(debug-enable 'backtrace)
(debug-enable 'debug)
(read-enable 'positions)

(define args (cdr (command-line)))
(define display-exports? #t)

(define gnc-module-dirs '())
(define guile-load-dirs '())
(define library-dirs '())

(define (usage-death)
  (display "Usage: gnc-test-env [ --no-exports ]\n")
  (display "                    [ (--gnc-module-dir dir | \n")
  (display "                       --guile-load-dir dir | \n")
  (display "                       --library-dir dir) ... ]\n")
  (exit 1))

(define (process-args! args)
  (let loop ((rest args))
    (cond
     ((null? rest) #t)
     ((string=? "--gnc-module-dir" (car rest))
      (set! gnc-module-dirs (cons (cadr rest) gnc-module-dirs))
      (loop (cddr rest)))
     ((string=? "--guile-load-dir" (car rest))
      (set! guile-load-dirs (cons (cadr rest) guile-load-dirs))
      (loop (cddr rest)))
     ((string=? "--library-dir" (car rest))
      (set! library-dirs (cons (cadr rest) library-dirs))
      (loop (cddr rest)))
     (else (usage-death))))
  (set! gnc-module-dirs (reverse gnc-module-dirs))
  (set! guile-load-dirs (reverse guile-load-dirs))
  (set! library-dirs (reverse library-dirs)))


(if (and (not (null? args))
         (string=? "--no-exports" (car args)))
    (begin
      (set! display-exports? #f)
      (set! args (cdr args))))

(if (null? args) (exit 0))

(process-args! args)

(display "GNC_MODULE_PATH=${GNC_MODULE_PATH}")
(display (apply string-append
                (map (lambda (dir) (string-append ":" dir))
                     gnc-module-dirs)))

(display " GUILE_LOAD_PATH=${GUILE_LOAD_PATH}")
(display (apply string-append
                (map (lambda (dir) (string-append ":" dir))
                     gnc-module-dirs)))
(display (apply string-append
                (map (lambda (dir) (string-append ":" dir))
                     guile-load-dirs)))

(display " LD_LIBRARY_PATH=${LD_LIBRARY_PATH}")
(display (apply string-append
                (map
                 (lambda (dir)
                   (string-append ":" dir ":" dir "/.libs"))
                 gnc-module-dirs)))
(display (apply string-append
                (map
                 (lambda (dir)
                   (string-append ":" dir ":" dir "/.libs"))
                 library-dirs)))

(display " LTDL_LIBRARY_PATH=${LTDL_LIBRARY_PATH}")
(display (apply string-append
                (map
                 (lambda (dir)
                   (string-append ":" dir ":" dir "/.libs"))
                 gnc-module-dirs)))
(display (apply string-append
                (map
                 (lambda (dir)
                   (string-append ":" dir ":" dir "/.libs"))
                 library-dirs)))

(if display-exports?
    (begin
      (display "; ")
      (display " export GNC_MODULE_PATH;")
      (display " export GUILE_LOAD_PATH;")
      (display " export LD_LIBRARY_PATH;")
      (display " export LTDL_LIBRARY_PATH;")))

;; Local Variables:
;; mode: scheme
;; End:
