;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; LUSH Lisp Universal Shell
;;;   Copyright (C) 2002 Leon Bottou, Yann Le Cun, AT&T Corp, NECI.
;;; Includes parts of TL3:
;;;   Copyright (C) 1987-1999 Leon Bottou and Neuristique.
;;; Includes selected parts of SN3.2:
;;;   Copyright (C) 1991-2001 AT&T Corp.
;;;
;;; This program is free software; you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 2 of the License, or
;;; (at your option) any later version.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program; if not, write to the Free Software
;;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA
;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;  9/9/2002       adapted from gsl-config.lsh  by jhuangfu
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defvar torch-libname "libtorch")
(defvar torch-version 3)
(defvar torch-libtorch ())
(defvar torch-libtorch-loaded ())
(defvar torch-cpheader-cmd ())

(when (not torch-libtorch-loaded)

  ;; find  library
  (cond
   ;; torch-libtorch was already defined, don't touch it
   (torch-libtorch t)
   ;; look for dynamic libopentorch.so installed
   ((progn
      (let* ((lib (find-shared-library  torch-libname )))
        (when (and lib (filep lib)) (setq torch-libtorch lib)))))
   ;; look for static libopentorch.a installed
   ((progn
      (let* ((lib (find-static-library  torch-libname )))
        (when (and lib (filep lib)) (setq torch-libtorch lib)))))
   ;; couldn't find it, complain loudly.
   (t (printf "neither the libtorch.so nor libtorch.a could be found\n")
      (printf "You can download torch from\n")
      (printf "http://www.torch.ch/\n")
      (printf "compile it, and install it in standard paths \n")
      (printf "(e.g. in /usr/local/lib) so that I can find it.\n")
      (printf "If you have installed torch and still get this message, please do\n")
      (printf "(defparameter torch-libtorch \"yourtorchlibrary\") before loading this file\n")
      (printf "(see %s for more details)\n" file-being-loaded)
      (error "cannot configure torch") ) )
  
  (setq torch-cpheader-cmd
      '(progn
	 (cpheader "#define  USE_DOUBLE")
	 (cpheader "#include <torch/Allocator.h>")
	 (cpheader "#include <torch/Bagging.h>")
	 (cpheader "#include <torch/Boosting.h>")
	 (cpheader "#include <torch/BoostingMeasurer.h>")
	 (cpheader "#include <torch/ClassFormatDataSet.h>")
	 (cpheader "#include <torch/ClassFormat.h>")
	 (cpheader "#include <torch/ClassMeasurer.h>")
	 (cpheader "#include <torch/ClassNLLMeasurer.h>")
	 (cpheader "#include <torch/CmdLine.h>")
	 (cpheader "#include <torch/DataSet.h>")
	 (cpheader "#include <torch/DiskDataSet.h>")
	 (cpheader "#include <torch/DiskMatDataSet.h>")
	 (cpheader "#include <torch/DiskXFile.h>")
	 (cpheader "#include <torch/FrameSelectorDataSet.h>")
	 (cpheader "#include <torch/general.h>")
	 (cpheader "#include <torch/IOAscii.h>")
	 (cpheader "#include <torch/IOBin.h>")
	 (cpheader "#include <torch/IOBufferize.h>")
	 (cpheader "#include <torch/IOMulti.h>")
	 (cpheader "#include <torch/IOSequenceArray.h>")
	 (cpheader "#include <torch/IOSequence.h>")
	 (cpheader "#include <torch/IOSub.h>")
	 (cpheader "#include <torch/KFold.h>")
	 (cpheader "#include <torch/List.h>")
	 (cpheader "#include <torch/log_add.h>")
	 (cpheader "#include <torch/Machine.h>")
	 (cpheader "#include <torch/MatDataSet.h>")
	 (cpheader "#include <torch/MeanVarNorm.h>")
	 (cpheader "#include <torch/Measurer.h>")
	 (cpheader "#include <torch/MemoryDataSet.h>")
	 (cpheader "#include <torch/MemoryXFile.h>")
	 (cpheader "#include <torch/MSEMeasurer.h>")
	 (cpheader "#include <torch/MultiClassFormat.h>")
	 (cpheader "#include <torch/NullXFile.h>")
	 (cpheader "#include <torch/Object.h>")
	 (cpheader "#include <torch/OneHotClassFormat.h>")
	 (cpheader "#include <torch/OutputMeasurer.h>")
	 (cpheader "#include <torch/Parameters.h>")
	 (cpheader "#include <torch/PipeXFile.h>")
	 (cpheader "#include <torch/PreProcessing.h>")
	 (cpheader "#include <torch/Random.h>")
	 (cpheader "#include <torch/Sequence.h>")
	 (cpheader "#include <torch/Stack.h>")
	 (cpheader "#include <torch/string_utils.h>")
	 (cpheader "#include <torch/TimeMeasurer.h>")
	 (cpheader "#include <torch/Timer.h>")
	 (cpheader "#include <torch/Trainer.h>")
	 (cpheader "#include <torch/TwoClassFormat.h>")
	 (cpheader "#include <torch/WeightedSumMachine.h>")
	 (cpheader "#include <torch/XFile.h>")
         ;; convolutions
	 (cpheader "#include <torch/SpatialConvolution.h>")  
	 (cpheader "#include <torch/TemporalConvolution.h>")  
	 (cpheader "#include <torch/TemporalSubSampling.h>")
	 (cpheader "#include <torch/SpatialSubSampling.h>")  
	 (cpheader "#include <torch/TemporalMean.h>")
         ;; datasets
	 (cpheader "#include <torch/DiskHTKDataSet.h>")               
	 (cpheader "#include <torch/HTKDataSet.h>")  
	 (cpheader "#include <torch/IOHTKTarget.h>")
	 (cpheader "#include <torch/ExampleFrameSelectorDataSet.h>")  
	 (cpheader "#include <torch/IOHTK.h>")
         ;; distributions
	 (cpheader "#include <torch/BayesClassifier.h>")         
	 (cpheader "#include <torch/KMeans.h>")          
	 (cpheader "#include <torch/NLLMeasurer.h>")
	 (cpheader "#include <torch/BayesClassifierMachine.h>")  
	 (cpheader "#include <torch/LogRBF.h>")          
	 (cpheader "#include <torch/ParzenDistribution.h>")
	 (cpheader "#include <torch/DiagonalGMM.h>")             
	 (cpheader "#include <torch/MAPDiagonalGMM.h>")  
	 (cpheader "#include <torch/TableLookupDistribution.h>")
	 (cpheader "#include <torch/Distribution.h>")            
	 (cpheader "#include <torch/MAPHMM.h>")          
	 ;;(cpheader "#include <torch/ViterbiTrainer.h>")
	 (cpheader "#include <torch/EMTrainer.h>")               
	 (cpheader "#include <torch/Multinomial.h>")
	 (cpheader "#include <torch/HMM.h>")                     
	 (cpheader "#include <torch/NLLCriterion.h>")
         ;; gradients
	 (cpheader "#include <torch/ClassNLLCriterion.h>")  
	 (cpheader "#include <torch/Linear.h>")      
	 (cpheader "#include <torch/MSECriterion.h>")        
	 (cpheader "#include <torch/StochasticGradient.h>")
	 (cpheader "#include <torch/ConnectedMachine.h>")   
	 (cpheader "#include <torch/LogMixer.h>")    
	 (cpheader "#include <torch/MultiCriterion.h>")      
	 (cpheader "#include <torch/SumMachine.h>")
	 (cpheader "#include <torch/Criterion.h>")          
	 (cpheader "#include <torch/LogSigmoid.h>")  
	 (cpheader "#include <torch/SaturationMeasurer.h>")  
	 (cpheader "#include <torch/Tanh.h>")
	 (cpheader "#include <torch/Exp.h>")                
	 (cpheader "#include <torch/LogSoftMax.h>")  
	 (cpheader "#include <torch/Sigmoid.h>")             
	 (cpheader "#include <torch/WeightedMSECriterion.h>")
	 (cpheader "#include <torch/GradientMachine.h>")    
	 (cpheader "#include <torch/Mixer.h>")       
	 (cpheader "#include <torch/SoftMax.h>")
	 (cpheader "#include <torch/InputsSelect.h>")       
	 (cpheader "#include <torch/MLP.h>")         
	 (cpheader "#include <torch/SoftPlus.h>")
         ;; kernels
	 (cpheader "#include <torch/Kernel.h>")   
	 (cpheader "#include <torch/QCMachine.h>")  
	 (cpheader "#include <torch/SVMCache.h>")           
	 (cpheader "#include <torch/SVM.h>")
	 (cpheader "#include <torch/QCCache.h>")  
	 (cpheader "#include <torch/QCTrainer.h>")  
	 (cpheader "#include <torch/SVMClassification.h>")  
	 (cpheader "#include <torch/SVMRegression.h>")
         ;; matrix
	 (cpheader "#include <torch/Mat.h>")             
	 (cpheader "#include <torch/mx_givens.h>")       
	 (cpheader "#include <torch/mx_low_level.h>")  
	 (cpheader "#include <torch/mx_sym_eig.h>")       
	 (cpheader "#include <torch/Vec.h>")
	 (cpheader "#include <torch/Mat_operations.h>")  
	 (cpheader "#include <torch/mx_hessenberg.h>")   
	 (cpheader "#include <torch/mx_lu_factor.h>")  
	 (cpheader "#include <torch/Perm.h>")
	 (cpheader "#include <torch/matrix.h>")          
	 (cpheader "#include <torch/mx_householder.h>")  
	 (cpheader "#include <torch/mx_solve.h>")      
	 (cpheader "#include <torch/Perm_operations.h>")
         ;; nonparametrics
	 (cpheader "#include <torch/KNN.h>")  
	 (cpheader "#include <torch/NPTrainer.h>")  
	 (cpheader "#include <torch/ParzenMachine.h>")
         ;; speech
         ;; (cpheader "#include <torch/Dictionary.h>")
	 (cpheader "#include <torch/EditDistance.h>")
	 (cpheader "#include <torch/SpeechHMM.h>")

	 (cheader "#undef   Object")
	 (cheader "#undef   eval")
	 (cheader "#undef   free")
	 (cheader "#undef   realloc") ) )

  ;; load libraries
  ;; THIS IS COMPLETELY WRONG **AGAIN**
  ;; THE INTERFACE SHOULD USE DHC-MAKE-WITH-LIBS INSTEAD OF THIS HACK
  ;; AND IN FACT IT DOES NOT WORK WITH A STATIC LIBRARY!!!!!
  (mod-load torch-libtorch)
  (setq torch-libtorch-loaded t) )

