(in-package "COMMON-LISP-USER")

;;; File: getenv.lisp
;;;
;;; KPML 4.1+ Aug09
;;; Broken out of other x-platform files so that it can be loaded
;;; independently and used during setup. 
;;;
;;; Plus some minimal directory hackery at the end...
;;;

   ;; --------------------------------------------------------------------
   ;; 3.2.3 missed the update for Lispworks! 
   ;; 4.1+ added in further conditionalisations from Rob
   ;;      and defined in terms of a CL package neutral
   ;;      version that can be loaded prior to KPML.
   ;;      (cf. file: x-platform/getenv.lisp).
   ;;
   ;;      Unfortunately this is now loaded so early that we
   ;;      can't use the Lisp version conditionalisation any more...
   ;; --------------------------------------------------------------------

(defun KPML-GETENV (string &optional (default ""))
  #-clisp "Generalized GETENV across Lisps"
  (let ((env-var-value
	 (or 
	  #+acl3.0                                 
	  (allegro::getenv string)
  
	  #+clisp                      ;; 4.0
	  (ext:getenv string)
	  
	  #+(and excl (or allegro-v4.2
			  allegro-v4.3))
	  (sys:getenv string)

	  #+Allegro (sys:getenv name)
	  
	  #+lcl5.0
	  (lispworks:environment-variable string)

	  ;; 3.2.3 (version-case out in 4.1+)
	  #+lispworks                 
	  (lispworks:environment-variable string)

	  #+CMU
	  (let ((x (assoc string ext:*environment-list*
			  :test #'string=)))
	    (if x (cdr x) default))
	  
	  #+ECL (si:getenv name)
   
	  #+SBCL (sb-unix::posix-getenv name)
   
	  nil)))

    (if (or (string= env-var-value "")
	    (not env-var-value))
	default
      env-var-value)))



;;; Directory manipulation... may need checking for different Lisp
;;; versions from time to time...

(defun KPML-PARENT-DIRECTORY (dir)
  (make-pathname 
   :device (pathname-device dir)
   :host (pathname-host dir)
   :directory (butlast (pathname-directory dir)))
  )