(defparameter *root-dir* (make-pathname :directory '(:relative)))

(defun make-relative (path)
  (let ((dir (pathname-directory path)))
    (make-pathname :directory (append '(:relative) (nthcdr (position "src" dir :test #'string=) dir))
                   :defaults path)))

(defun get-source-paths (path)
  (directory (merge-pathnames path *root-dir*)))

(defun default-files ()
  (get-source-paths
   (make-pathname :directory '(:relative "src")
                  :name :wild
                  :type "cpp")))

(defun software-renderer ()
  (get-source-paths
   (make-pathname :directory '(:relative "src" "Backends" "Rendering")
                  :name "Software"
                  :type "cpp")))

(defun x11-platform ()
  (get-source-paths
   (make-pathname :directory '(:relative "src" "Backends" "Platform")
                  :name "X11"
                  :type "cpp")))

(defun x11-window ()
  (get-source-paths
   (make-pathname :directory '(:relative "src" "Backends" "Rendering" "Window" "Software")
                  :name "X11"
                  :type "cpp")))

(defun solaris-audio ()
  (get-source-paths
   (make-pathname :directory '(:relative "src" "Backends" "Audio" "SoftwareMixer")
                  :name "Solaris"
                  :type "cpp")))

(defun null-audio ()
  (get-source-paths
   (make-pathname :directory '(:relative "src" "Backends" "Audio")
                  :name "Null"
                  :type "cpp")))

(defun software-mixer ()
  (get-source-paths
   (make-pathname :directory '(:relative "src" "Backends" "Audio")
                  :name "SoftwareMixer"
                  :type "cpp")))

(defun null-controller ()
  (get-source-paths
   (make-pathname :directory '(:relative "src" "Backends" "Controller")
                  :name "Null"
                  :type "cpp")))

(defun get-sources ()
  (append
   (software-renderer)
   ;;(software-mixer)
   (x11-platform)
   (null-controller)
   (x11-window)
   (null-audio)
   (default-files)))

(defun get-build-path (path)
  (let ((dir (pathname-directory path)))
    (make-pathname :directory (append '(:relative "build")
                                      (nthcdr (position "src" dir :test #'string=) dir))
                   :type "o"
                   :defaults path)))

(defun make-compile-command (file)
  (let ((str
          (format nil "g++ -O2 -c ~A -o ~A"
                  (namestring (make-relative file))
                  (get-build-path file))))
    (when (string-equal "Bitmap" (pathname-name file))
      (setf str (format nil "~A -I./fakes/" str)))
    (format nil "echo ~A~%~A~%" str str)))

(defun make-link-command (sources)
  (format nil "echo Linking.~%g++ -O2 -o CSE2 ~{~A ~}-lX11 -lm~%"
          (mapcar 'get-build-path sources)))

(defun make-build-script (output-path)
  (with-open-file (stream output-path :direction :output :if-exists :supersede :if-does-not-exist :create)
    (setf *root-dir* (make-pathname :directory '(:relative)))
    (format stream "#!/bin/sh~%~%")
    (let ((sources (get-sources)))
      (loop :for file :in sources
            :for build-path := (get-build-path file)
            )
      (dolist (file sources)
        (write-string (make-compile-command file) stream))
      (write-string (make-link-command sources) stream))))