(in-package "COMMON-LISP-USER")
;;;*************************************************************************
;;;      -----  NO USER CHANGES REQUIRED IN THIS FILE ------
;;;*************************************************************************
;;; 
;;; load-kpml.lisp ()
;;;
;;;  load the core KOMET-PENMAN system
;;;  with options on multilinguality...
;;;  (modelled on compile/load-penman)
;;;
;;;  Feb95: updated for ANSI packages compatibility (John)
;;;  Jun98: references to packages USER and LISP removed (John)
;;;  Oct01: there was a parameter missing in a conditionalization
;;;         for ACL4.3, added (3.2+). Only was a problem if the
;;;         flag *acl-ipc-required?* is set, as it would be for
;;;         Allegro Lisp (John)
;;;  Sep02: the increasing number of startup files pushed to a subdir (John)
;;;  Jul06: loading of resources moved to last action prior to reporting
;;;         that we're done. This is necessary to avoid unexpected
;;;         conditionalization failures during resource loading (John) 
;;;*************************************************************************

(defvar *root-of-kpml*)

;;; ----------------------------------------------------------------------
;;; Ascertain what KPML variations are required...
;;; ----------------------------------------------------------------------

(load 
 (merge-pathnames 
  "system-flag-defs.lisp" 
  (merge-pathnames "Processes/startup/" *root-of-kpml*)))
(configure-kpml *questions-during-configuration*)

;;; ----------------------------------------------------------------------
;;; Set the binaries root to the KPML root unless a user has specified
;;; otherwise...
;;; ----------------------------------------------------------------------

(unless (and (boundp '*kpml-binaries-root*)
	     *kpml-binaries-root*)
  (setf *kpml-binaries-root* *root-of-kpml*))

;;; ----------------------------------------------------------------------
;;; Make sure the modified defsystem facility is available...
;;; ----------------------------------------------------------------------

(without-redefinition-warnings
 (format t "~%;;; Loading conditionalized defsystem facility...")
 (load 
  (merge-pathnames 
   "cond-defsystem"
   (merge-pathnames "Processes/startup/" *root-of-kpml*)))
 )

;;; ----------------------------------------------------------------------
;;; Following file when loaded must define the selected kpml kr definition
;;; in *kpml-kr-system-name*. This should be a mk::system name
;;; that is used for loading up below....
;;; 4.0 made configurable.
;;; ----------------------------------------------------------------------

(when *load-kr-system?*
  (load *kpml-kr-loader*))

;;; ----------------------------------------------------------------------
;;; Get CLISP in...
;;; ----------------------------------------------------------------------

(unless (find-package "CLISP") 
  (make-package 'clisp)
  (shadow '(assq bind memq neq puthash tab) 'clisp)
  #-LCL4.2(use-package '(common-lisp) 'clisp))

(without-redefinition-warnings
 (load 
  (merge-pathnames 
   "clisp-defsys.lisp"
   (merge-pathnames "clisp/" *root-of-kpml*)))
 (make:operate-on-system 'clisp :load
			 :compile-during-load nil
			 :load-source-instead-of-binary nil))

;;; ----------------------------------------------------------------------
;;; Load the KPML defsys and KPML package definition...
;;; ----------------------------------------------------------------------

(load 
 (merge-pathnames 
  "kpml-defsys.lisp"
  (merge-pathnames "Processes/startup/" *root-of-kpml*)))

;;; ----------------------------------------------------------------------
;;; Copy some values to the old PENMAN package variables just in
;;; case someone attempts to use them...
;;; ----------------------------------------------------------------------

(defvar *penman-pathname-default* *root-of-kpml*)

;;; ----------------------------------------------------------------------
;;; Load the KPML system... this is the main work... and is divided into
;;; the core system (essentially ML-Penman with very very very many additions),
;;; an optional stratification module for doing genre and stuff (not part
;;; of the standard release), the user interface, and the particular
;;; KR system and KPML-KR interface selected.
;;; ----------------------------------------------------------------------

;;;
;;; Core system...
;;;
(make:operate-on-system 'kpml :load
			   :compile-during-load nil
			   :load-source-instead-of-binary nil)

;; 3.0, 3.2+
(when *acl-ipc-required?*
  (make:operate-on-system 'kpml-acl-ipc :load
			  :compile-during-load nil
			  :load-source-instead-of-binary nil))

;; 4.0
(when *cl-xml-required?*
  (make:operate-on-system 'kpml-xml :load
			  :compile-during-load nil
			  :load-source-instead-of-binary nil))

;;; Stratification extensions if required (these extend/alter existing
;;; functions so we don't report redefinitions...)
;;;
(when *stratification?*
  (without-redefinition-warnings
   (make:operate-on-system 'stratification  :load
			   :compile-during-load nil
			   :load-source-instead-of-binary nil)))

;;;
;;; UI...
;;;
(cond (*window-interface-required?*
       (unless (find-package "KPML-I") 
	 (make-package "KPML-I")	;  :use '(clim-lisp clim)
	 (shadow #+Lucid '(lcl::warning)
		 #-Lucid '(common-lisp:warning)
		 "KPML-I"))
       (make:operate-on-system  'kpml-clim
				:load
				:compile-during-load nil
				:load-source-instead-of-binary nil)
       (when *stratification?*
	 (make:operate-on-system 'ui-stratification
				 :load 
				 :compile-during-load nil
				 :load-source-instead-of-binary nil))
       
       #+LCL ;; don't know if anyone needs this anymore, but...
       (:version-case (":LCL")
		      ("4.1"
		       (make:operate-on-system 
			'lucid-4-1-clim-hack :load
			:compile-during-load nil
			:load-source-instead-of-binary t)))
       )
      (T
       (make:operate-on-system 'kpml-interface-stubb :load
			       :compile-during-load nil
			       :load-source-instead-of-binary nil)))

;;;
;;; Selected KR system...
;;;
(if *load-kr-system?*
    (when *kpml-kr-system-name*
      (make:operate-on-system  *kpml-kr-system-name* 
			       :load 
			       :compile-during-load nil
			       :load-source-instead-of-binary nil))
  (make:operate-on-system 'kpml-null-kr
			  :load 
			  :compile-during-load nil
			  :load-source-instead-of-binary nil))

;;;================================================================
;;; The KPML system is now installed... Report this in the version
;;; name of the penman-features-record, in *features*, and finally
;;; load any outstanding patches. *Kpml-version* is kept
;;; up to date in systemic-networks/variables.lisp.

(in-package "KPML")

#-lispworks
(unless (common-lisp:short-site-name)
  (setf (common-lisp:short-site-name) 
	(if (getenv "HOST")
	    (getenv "HOST")
	  "unknown")))

(when (boundp '*penman-features*)
  (set-penman-feature :version *kpml-version*)
  (set-penman-feature :site (common-lisp:short-site-name))
  (set-penman-feature :language kpml::all_languages))
(push *kpml-version* *features*)

(when *stratification?*
  (kpml::set-penman-feature 
   :version (kpml::experimental-version 
	     (kpml::get-penman-feature :version)))
  (push (kpml::experimental-version kpml::*kpml-version*) *features*))

;;; ----------------------------------------------------------------------
;;; Load any patches and site specifics that are to be found...
;;; ----------------------------------------------------------------------

(kpml::load-kpml-patches)
(load (merge-pathnames "site-specifics.lisp" common-lisp-user::*root-of-kpml*)
      :if-does-not-exist nil)

;;; ----------------------------------------------------------------------
;;; KPML is now there, we can now load linguistic resources if required...
;;; default is just the upper model but this can only be done after the
;;; KR system requested is there!!! 
;;;
;;; 4.1  This was previously done sooner, which is not really logical
;;;      since it is only at this point, after site-specifics and
;;;      patches, etc. that the KPML that is going to be used is
;;;      fully available. So we move it down here.
;;; 
;;; 4.1+ added option for loading OWL upper model via Racer
;;;        too
;;;
(cond (cl-user::*load-linguistic-resources?* 
       (push :semantics kpml::*loading-saving-profile*)
       (let ((kpml::*editing-protocol* nil)) ;; deactivate for configuration
	 (kpml::load-linguistic-resources 
	  (if (boundp '*kpml-initial-resources*)
	      *kpml-initial-resources*
	    cl-user::*kpml-initial-resources*)))
       (pop kpml::*loading-saving-profile*))
      (t
       (let ((kpml::*editing-protocol* nil)) ;; deactivate for configuration
	 (when cl-user::*upper-model-required?*
	   (unless (and (boundp '*use-racer*) *use-racer*)
	     (kpml::load-semantics-from-directory
	      (pathname
	       (concatenate 'string 
			    (namestring cl-user::*root-of-resources*)
			    (string cl-user::*kpml-general-resources*) 
			    (string kpml::*directory-delimiter*))))))
	 (kpml::kb-initializations)
	 (when cl-user::*inquiry-impls-required?*
	   (kpml::load-inquiry-implementations 
	    cl-user::*kpml-general-resources*)))))

;;; ----------------------------------------------------------------------
;;; And do a last reconfiguration if there is a configuration file to
;;; be found...
;;; ----------------------------------------------------------------------

(kpml::load-kpml-configuration-file)

;;; ----------------------------------------------------------------------
;;; Report what we've got so far...
;;; ----------------------------------------------------------------------

(describe-kpml t "KPML LOADED AS FOLLOWS ...")

;;;==================================================================
;;; After doing the above, we can make an image in the normal
;;; time-honored way... This can be done for Lucid and Allegro Common Lisp
;;; by calling the function (kpml::make-kpml-image "file-name").
;;;
;;; ... Enjoy ...

