#!/bin/sh
#|-*- mode:lisp -*-|#
#|
exec ros -Q -- $0 "$@"
|#
(progn ;;init forms
  (ros:ensure-asdf)
  ;;#+quicklisp(ql:quickload '() :silent t)
  )

(defpackage :ros.script.make-depends.3745285738
  (:use :cl))
(in-package :ros.script.make-depends.3745285738)

(defun depend-systems (system-name &optional result)
  (let ((subs (cons system-name (asdf:system-depends-on (asdf:find-system system-name)))))
    (loop for sub in subs
          unless (find sub result :test #'equal)
          do (push sub result)
             (setf result (depend-systems sub result)))
    result))

(defun system-files (system-name)
  (let ((system (asdf:find-system system-name)))
    (cons (asdf:system-source-file system)
          (mapcar #'asdf:component-pathname
                  (asdf:module-components system)))))

(defun makefile (o files file)
  (format o "~A: \\~%" file)
  (format o "~{	~A~^ \\~%~}~%" 
          (mapcar #'uiop:native-namestring (remove-if #'uiop:directory-pathname-p (remove nil files)))))

(defun main (&rest argv)
  (let* ((systems (depend-systems (first argv)))
         (files (apply #'append (mapcar 'system-files systems)))
         (file (second argv))
         (output 'makefile))
    (if (third argv)
        (with-open-file (*standard-output* (third argv) :direction :output :if-exists :supersede)
          #1=(funcall output *standard-output* files file))
        #1#)))
;;; vim: set ft=lisp lisp:
