#!/bin/sh
#|-*- mode:lisp -*-|#
#|
exec ros -Q +A -- $0 "$@"
|#
(progn ;;init forms
  #-asdf(ros:ensure-asdf :version nil)
  (roswell:include "util")
  )

(defpackage :ros.script.compile-file.3716986869
  (:use :cl :roswell.util))
(in-package :ros.script.compile-file.3716986869)

(defun fasl-dir (&optional env)
  (merge-pathnames (format nil "~Aimpls/~A/~A/~A/fasl/"
                           (let ((env (or env (opt "roswellenv"))))
                             (if env
                                 (format nil "env/~A/" env)
                                 ""))
                           (uname-m) (uname) (opt "impl"))
                   (homedir)))

(defmacro consume (argv &rest cond)
  (let (default
        (params (gensym)))
    `(loop for ,params on ,argv
        unless (cond
                 ,@(loop for e in cond
                      when (or (eql (caar e) :in)
                               (eql (caar e) :on))
                      do (setf default e)
                      else
                      collect `((equal (first ,params) ,(caar e))
                                (let ,(loop for i in (cdar e)
                                         for j from 1
                                  collect `(,i (nth ,j ,params)))
                                  ,@(cdr e)
                                  ,@(unless (zerop (length (cdar e)))
                                            `((setf ,params (nthcdr ,(length (cdar e)) ,params))))
                                  t))))
        do (let ((,(cadar default) (car ,params)))
             ,@(cdr default)))))

(defun main (&rest argv)
  (declare (ignorable argv))
  (let ((out t)
        asdf)
    (consume
     argv
     (("-o" opt)
      (read-call "uiop:chdir"
                 (setf *default-pathname-defaults* (truename opt)))
      (setf out t))
     (("-o-")
      (setf out nil))
     (("-o.")
      (setf out t))
     (("-asdf")
      (setf asdf t))
     (("+asdf")
      (setf asdf nil))
     ((:in file)
      (if asdf
          (read-call "uiop:chdir"
                     (setf *default-pathname-defaults*
                           (merge-pathnames (format nil "lisp/asdf/~A/" file) (homedir))))
          (when (ros:opt "asdf.version")
            (ros:asdf)))
      (funcall (read-from-string "asdf:compile-file*")
               (if asdf
                   (merge-pathnames "asdf.lisp" *default-pathname-defaults*)
                   file)
               :output-file
               (make-pathname
                :defaults (if (or out asdf)
                              *default-pathname-defaults*
                              (fasl-dir))
                :name (if asdf
                          "asdf"
                          (pathname-name file))
                :type (format nil "~A~@[_~A~]"
                              (if (or out asdf)
                                  (substitute #\- #\/ (substitute #\_ #\. (opt "impl")))
                                  (pathname-type file))
                              (and (not asdf)
                                   (ros:opt"asdf.version")
                                   (substitute #\- #\/ (substitute #\_ #\. (ros:opt"asdf.version")))))))))))

;;; vim: set ft=lisp lisp:
