#!/bin/sh
:;exec snow -f "$0" "$@"				    # -*- Scheme -*-
;;;;
;;;;
;;;; stk-genmake 		-- Generate a Makefile for STk extensions
;;;;
;;;; Copyright © 1998-1999 Erick Gallesio - I3S-CNRS/ESSI <eg@unice.fr>
;;;; 
;;;; Permission to use, copy, modify, distribute,and license this
;;;; software and its documentation for any purpose is hereby granted,
;;;; provided that existing copyright notices are retained in all
;;;; copies and that this notice is included verbatim in any
;;;; distributions.  No written agreement, license, or royalty fee is
;;;; required for any of the authorized uses.
;;;; This software is provided ``AS IS'' without express or implied
;;;; warranty.
;;;;
;;;;           Author: Erick Gallesio [eg@unice.fr]
;;;;    Creation date:  1-Jun-1998 18:44
;;;; Last file update:  3-Sep-1999 19:42 (eg)


(define version "0.1")		
(define config.make-file	; Complete path name of the  Config.make file 
  (string-append (%library-location) "/" (machine-type) "/Config/config.make"))

(define (abort . l)
  (apply format (current-error-port) l)
  (newline (current-error-port))
  (exit 0))



(define (generate-prelude targets)
  (format #t "# Makefile automatically generated by ~A version ~A. DO NOT EDIT\n"   
	  *program-name* version)
  (if (file-exists? config.make-file)
      ;; Copy the content of config.make
      (with-input-from-file config.make-file
	   (lambda ()
	     (do ((l (read-line) (read-line)))
		 ((eof-object? l) l)
	       (format #t "~A\n" l))))
      ;; config.make doesn't exists
      (abort "~A: File ~S does not exist (you probably need a \"make install.libs\""
	     *program-name*))

  ;; Generate the .SUFFIXES rules
  (format #t "CFLAGS= $(SH_CCFLAGS) $(STKCFLAGS) $(DFLGS) -DUSE_TK @DEFS@ \\\n")
  (format #t  "-I$(incdir)\n\n\n")
  (format #t ".SUFFIXES: .$(SH_SUFFIX) .o .c\n\n")
  (format #t ".o.$(SH_SUFFIX):\n")
  (format #t "\t$(SH_LOADER) $(SH_LDFLAGS) $*.$(SH_SUFFIX) $<\n")
  (format #t "\tif test -f a.out  ;then mv a.out $*.$(SH_SUFFIX); fi\n\n")

  ;; Generate the main target
  (format #t "\nall:\t")
  (for-each (lambda (x) (format #t "~A.$(SH_SUFFIX) " x)) targets)
  (format #t "\n\n"))


(define (generate-target target)
  (format #t "\n~A.$(SH_SUFFIX): ~A.o\n\n" target target))

(define (generate-postlude)
  (format #t "clean:\n")
  (format #t "\t@/bin/rm -f *.o *.$(SH_SUFFIX) core *~\n")
  (format #t "#End of Makefile\n"))

;;;;
;;;; Program starts here
;;;;

(if (zero? *argc*)
    (abort "Usage: ~A target [target ...]" *program-name*))

(generate-prelude *argv*)
(for-each generate-target *argv*)
(generate-postlude)
