 #! /usr/local/bin/scsh \
 -e main -s

 file name: write-setenvs.scm

 In Mac OS X, personal environment variable settings go in
 ~/.MacOSX/environment.plist, a property list file in XML-format.
 These settings affect the environment of applications, including
 the Terminal.app, but not the shell per se.  However, they can
 be imported explicitly in the tcsh init file by

                   eval `write-setenvs.scm`

 (if the script resides in the exe search path).  Useful for
 remote logins.


 Related Links

 Setting ennvironment variables for user processes
 http://developer.apple.com/qa/qa2001/qa1067.html

 Environment variables
 http://developer.apple.com/documentation/MacOSX/Conceptual/BPRuntimeConfig/Concepts/EnvironmentVars.html

 macosxhints -- Populate tcsh environment variables via a script
 http://www.macosxhints.com/article.php?story=20040327132019645
 http://www.macosxhints.com/comment.php?mode=display&sid=20040327132019645&title=New+Version+for+both+Bash+and+Tcsh&type=article&order=&pid=39603


 Da Code

 uses the defaults(1) utility to access the plist.  The
 output of "defaults read $HOME/.MacOSX/environment" looks
 like so:
 {
    CC = "gcc-3.3";
    CPP = "cpp-3.3";
    "SCSH_LIB_DIRS" = "#f \\"/Users/Shared/Eunuchs/lib/scsh-libs\\"";
 }
 The output format doesn't seem to be specified, though.

 Works with scsh-0.6.6 for the tcsh in OS X 1.3;
 easily adapted to the bash.
 !#

 (define assignment-re
   (rx (* whitespace)
       (| (submatch (+ alphanumeric))
          (: #\" (submatch (+ (| alphanumeric #\_))) #\"))
       " = "
       (: #\" (submatch (* any)) "\"; ")))


 ;; returns a string alist as used by the Scsh's env procedures
 (define (defaults->alist inport)
   (awk (read-line inport) (line) ((bindings '()))
        (,@assignment-re => (lambda (match)
                              (cons (cons (or (match:substring match 1)
                                              (match:substring match 2))
                                          (match:substring match 3))
                                    bindings)))))


 (define (write-csh-assignment var:val)
   (format #t "setenv ~a '~a';~%" (car var:val) (cdr var:val)))


 (define (main args)
   (for-each write-csh-assignment
             (defaults->alist (run/port (defaults read
                                          ,(home-file ".MacOSX/environment"))))
             ))