#!/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* type=file 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[]="" findex=0 output-file="" i=0>

  <while <get-var mhtml::program-arguments[i]>>
    <set-var arg=<get-var mhtml::program-arguments[i]>>
    <increment i>

    <if <or <string-eq <get-var arg> -o>
	    <string-eq <get-var arg> --output>>
	<prog
	  <set-var output-file=<get-var mhtml::program-arguments[i]>>
	  <increment i>>
      <prog
	<set-var filenames[findex]=<get-var arg>>
	<increment findex>>>
  </while>

  <when <not <get-var output-file>>>
    <mklib::print "You must specify an output file with `--output'.\n">
    <newline>
  </when>

  <when <get-var output-file>>

    <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>

    <set-var i=0>
    <while <get-var filenames[i]>>
      <if <string-eq <substring <get-var filenames[i]> 0 1> />
	  <set-var mhtml::include-prefix = />>
      <include <get-var filenames[i]>>
      <increment i>
    </while>

    <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
				%%
				\*META-HTML\*>>
    <set-var i = 0>
    <while <get-var skip-packages[i]>>
      <subst-in-var dump-packages " <get-var skip-packages[i]> " " ">
      <increment i>
    </while>

    ;;; 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>
</when>
