;; Initialization for sbcl when compiling roslisp


(require :asdf)

;;; Handle recompilation of fasls with wrong version.
(defmethod asdf:perform :around ((o asdf:load-op)
                                 (c asdf:cl-source-file))
  (handler-case (call-next-method o c)
    ;; If a fasl was stale, try to recompile and load (once).
    (sb-ext:invalid-fasl ()
      (asdf:perform (make-instance 'asdf:compile-op) c)
      (call-next-method))))

;; Add appropriate paths for asdf to look for things, using rospack to find the base directory
(let ((str (make-string-output-stream)))
  (sb-ext:run-program "rospack" '("find" "roslisp") :search t :output str)
  (let ((pathstr (delete #\Newline (get-output-stream-string str))))
    (when (search "couldn't find package" pathstr)
      (format t "~&~a~&" pathstr)
      (sb-ext:quit))
    (unless (eql (char pathstr (1- (length pathstr))) #\/)
      (setf pathstr (concatenate 'string pathstr "/")))
    (let ((roslisp-base (pathname pathstr)))
      (push (merge-pathnames #P"asdf/" roslisp-base) asdf:*central-registry*))))

(asdf:operate 'asdf:load-op :ros-load-manifest)

