#!/www/bin/mhc -z
;;; mklib: -*- Meta-HTML -*- Build a binary package file from input.
;;;
;;;  Copyright (c) 1996 Brian J. Fox
;;;  Author: Brian J. Fox (bfox@ai.mit.edu) Sun Jul 21 11:27:07 1996.
<set-var mhc::explicit-output-only=true>

<set-var mhtml::include-prefix[] = <cgi-exec pwd>>

<defun mklib::print item>
  <with-open-stream print *standard-output* mode=write>
    <stream-put print <get-var item>>
  </with-open-stream>
</defun>

<if <lt <get-var mhtml::version> 3.95>
    <prog
      <mklib::print
 "Sorry, but this version of Meta-HTML (<get-var mhtml::version>)
  is too old to build libararies,  You need version 3.95 or greater.\n">>>

<when <not <lt <get-var mhtml::version> 3.95>>>
  ;;; Parse command line arguments.
  ;;; They are simply the name of the output file, and the names of files
  ;;; of Meta-HTML code to include, then write out.
  <set-var filenames[]="" output-file="" this-arg-is-output="">

  <foreach arg program-arguments start=1>
    <if <get-var this-arg-is-output>
	<set-var output-file=<get-var arg> this-arg-is-output="">
      <if <or <string-eq <get-var arg> -o>
	      <string-eq <get-var arg> --output>>
	  <set-var this-arg-is-output=true>
	<array-append <get-var arg> filenames>>>
  </foreach>

  <if <not <get-var output-file>>
      <prog
	<mklib::print "You must specify an output file with `--output'.\n">
	<break>>>

  <copy-var *meta-html*::include *meta-html*::mhc-include>

  <set-var mklib::inclev = 0>

  <defsubst include f &optional verbatim &key &unevalled alt whitespace=delete>
    <mklib::print
     "<pad \"\" <mul 2 mklib::inclev> align=right> Including <get-var f>\n">
    <increment mklib::inclev>
    <mhc-include <get-var f> <get-var verbatim>
		 alt=<or <get-var-once alt>
			 <mklib::print " Failed!!\n">>>
    <decrement mklib::inclev>
  </defsubst>

  <foreach fname filenames>
    <if <string-eq <substring <get-var fname> 0 1> />
	<set-var mhtml::include-prefix = />>
    <include <get-var fname>>
  </foreach>

  <undef include>

  ;;; Get the name of every package.
  <set-var dump-packages[]=<package-names>>

  ;;; Remove those which the user should not set in a library.
  <set-var skip-packages[] =
    <prog
      MKLIB
      MHTML
      MHTPD
      DEFAULT
      ENV
      POSTED
      TEMP
      TMP
      MHC
      ISP
      *PARSER*
      %%
      *META-HTML*>>
  <foreach p skip-packages>
    <set-var i=<array-member <get-var p> dump-packages>>
    <if <get-var i>
	<array-shift -1 dump-packages start=<get-var i>>>
  </foreach>

  ;;; Show which packages are being dumped:
  <mklib::print "Dumping packages: ...">
  <foreach p dump-packages>
  <if <match <get-var p> " ">
      <mklib::print "\"<get-var p>\" ">
    <mklib::print "<get-var p> ">>
  </foreach>
  <mklib::print "\n">
  <set-var **save::def = <function-def mklib::print>>
  <undef mklib::print>
  <set-var expr =
    <&&write-package-file <get-var output-file>
			  <concat
			   <foreach p dump-packages>
			   <if <match <get-var p> " ">
			       <concat "\"<get-var p>\" ">
			     <concat "<get-var p> ">>
			   </foreach>>>>
  <subst-in-var expr "^<&&" "<%%">
  <set-var num-written = <%%eval <get-var expr>>>
  <%%eval <get-var **save::def>>
  <mklib::print
   "Wrote <get-var num-written> packages to <get-var output-file>.\n">
</when>
<mklib::print "<debugging-output retrieve>">
<mklib::print "<system-error-output retrieve>">
