;                         -*- mode: lisp -*-

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Setting up CLISP for normal use ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; In shell:

;;; Create the directory structure
;;;  mkdir -p ~/.clisp/{site,systems}
;;;  cd ~/.clisp

;;; Get some libraries
;;;  wget http://common-lisp.net/project/asdf/asdf.lisp
;;;  wget http://common-lisp.net/project/asdf-install/asdf-install_latest.tar.gz
;;;  tar xvzf asdf-install_latest.tar.gz
;;;  mv asdf.lisp asdf-install/asdf-install site/
;;;  ln -s site/asdf-install/asdf-install.asd systems/
;;;  rm -rf asdf-install*

;;; In .clisprc:
(load "/home/salvi/.clisp/site/asdf.lisp")
(push "/home/salvi/.clisp/systems/" asdf:*central-registry*)

;;; In .asdf-install:
#+clisp
(setf asdf-install:*locations*
      '((#p"/home/salvi/.clisp/site/" #p"/home/salvi/.clisp/systems/"
         "Personal installation"))
      asdf-install:*preferred-location* 1
      asdf-install:*verify-gpg-signatures* nil)

;;; Now your CLISP is ready to use.


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Setting up CLISP for scripting ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; Get parse-args (maybe it will be ASDF-INSTALLable later):
;;;  cd ~/.clisp/site
;;;  hg clone http://bitbucket.org/salvipeter/parse-args/
;;;  cd ..; ln -s parse-args/parse-args.asd systems/

;;; Start up a new REPL, and then:
(asdf:operate 'asdf:load-op 'parse-args :verbose nil)
(ext:saveinitmem "scriptinit.mem" :norc t :script t)
;;; ... and put scriptinit.mem in a good place (~/.clisp/ for example)


;;;;;;;;;;;;;;;;;;;;;;;
;;; Writing scripts ;;;
;;;;;;;;;;;;;;;;;;;;;;;

;;; The first line should be something like
;;; #!/usr/bin/clisp -q -q -C -M /home/salvi/.clisp/scriptinit.mem
;;; where -q -q makes CLISP _really_ quiet,
;;; and -C also compiles the file while loading.

;;; Here is a simple script to show off some functionality:

(multiple-value-bind (args unknown missing-parameter)
    (parse-args:parse-args ext:*args*
                           `((help #\h "help")
                             (number #\n "with-number" :optional
                                     ,(lambda (name x)
                                        (if x (parse-integer x) 42)))
                             (string #\s "with-string" :required)))
  (if (or (null args) unknown missing-parameter
          (member 'help args :key #'car))
      (format t "Usage: ~a [options]~%~
                 -h --help              : prints this help~%~
                 -n --with-number [n]   : sets a number~%~
                 -s --with-string str   : sets a string~%"
              *load-pathname*)         ; *load-truename* for full path
      (let ((n (cdr (assoc 'number args)))
            (str (cdr (assoc 'string args))))
        (when n (format t "I see a number: ~a~%" n))
        (when str (format t "I~:[~; also~] see a string: ~a~%" n str)))))