#!/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 mhtml::program-arguments>
    <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 whitespace=delete>
    <mklib::print
     "<pad \"\" <mul 2 mklib::inclev> align=right> Including %0\n">
    <increment mklib::inclev>
    <mhc-include %body 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=" <subst-in-string <package-names> \"\n\" \" \"> ">

  ;;; Remove those which the user should not set in a library.
  <set-var skip-packages[]= <prog
			      MKLIB
			      MHTML
			      MHTPD
			      DEFAULT
			      ENV
			      POSTED
			      MHC
			      ISP
			      *PARSER*
			      %%
			      \*META-HTML\*>>
  <foreach p skip-packages>
    <subst-in-var dump-packages " <get-var p> " " ">
  </foreach>

  ;;; Show which packages are being dumped:
  <mklib::print "Dumping packages: ...">
  <mklib::print "<get-var dump-packages>\n">
  <set-var expr =
    "<&&write-package-file <get-var output-file> <get-var dump-packages>>">
  <subst-in-var expr "  " " " "^<&&" "<%%">
  <set-var num-written = <%%eval <get-var expr>>>
  <mklib::print
   "Wrote <get-var num-written> packages to <get-var output-file>.\n">
</when>
