#!/bin/sh
# $Id: mkdist-merge 56 2022-10-03 01:21:28Z karl $
# This file is public domain.

# Merge the constituent files with xeplain.tex to produce eplain.tex.

UTIL_DIR=`dirname $0`

# Usually we are called from the Makefile, and the `version' variable is set.
if test -z "$version"; then
  echo "No version number, using xx!"
  version=xx
fi

btxtmp=/tmp/mergebtx$$
common1tmp=/tmp/mergec1$$
common2tmp=/tmp/mergec2$$
common3tmp=/tmp/mergec3$$
eplaintmp=/tmp/mergepl$$
arrow1tmp=/tmp/mergea1$$
arrow2tmp=/tmp/mergea2$$
pathtmp=/tmp/mergepa$$
ifpdftmp=/tmp/mergeif$$

rm -f eplain.tex

# Use this regexp to strip comments (but not %% comments) with egrep.
commentre='^ *%([^%]|$)'

# Extract the relevant parts of btxmac.tex (without comments).
egrep -v "$commentre" btxmac.tex					\
  | sed -n								\
        -e "1,/^%% \[\[\[start .*\]\]\]/w $common1tmp"			\
        -e "/^%% \[\[\[start .*\]\]\]/,/^%% \[\[\[end .*\]\]\]/w $btxtmp" \
        -e "/^%% \[\[\[end .*\]\]\]/,\$w $common2tmp"

# Get texnames.sty and path.sty.  Remove the announcement, as well as
# the comments.
egrep -v "$commentre|immediate" texnames.sty > $common3tmp
egrep -v "$commentre|immediate" path.sty > $pathtmp

# Get arrow.tex.
egrep -v "$commentre" arrow.tex \
  | sed -n							\
        -e '1,/catcode.*\&.*4/w '"$arrow1tmp"			\
        -e '/catcode.*\&.*4/,$w '"$arrow2tmp"

# Get iftex.sty.
egrep -v "$commentre" iftex.sty > $ifpdftmp

# Merge the above into xeplain, calling the result eplain.  Also change the
# `filename' in the comment.%%
egrep -v "$commentre" xeplain.tex					\
  | sed -e 's/"xeplain.tex"/"eplain.tex"/'				\
        -e "/^%% \[\[\[here is the first.*\]\]\]/r $common1tmp"		\
        -e "/^  %% \[\[\[here are the BibTeX.*\]\]\]/r $btxtmp"		\
        -e "/^%% \[\[\[here is the second.*\]\]\]/r $common2tmp"	\
        -e "/^%% \[\[\[include texnames.*\]\]\]/r $common3tmp"		\
        -e "/^%% \[\[\[include path.*\]\]\]/r $pathtmp"			\
        -e "/^  %% \[\[\[include arrow1\]\]\]/r $arrow1tmp"		\
        -e "/^  %% \[\[\[include arrow2\]\]\]/r $arrow2tmp"		\
        -e "/^%% \[\[\[include iftex.*\]\]\]/r $ifpdftmp"		\
    > $eplaintmp

# Remove our [[[...]]] markers and \endinput's.
egrep -v '\[\[\[|^ *$|\\endinput' $eplaintmp > eplain.tex

# Fix header fields.
$UTIL_DIR/mkdist-add-date eplain.tex
$UTIL_DIR/mkdist-add-version $version eplain.tex
$UTIL_DIR/mkdist-fix-checksum eplain.tex

# Old checksum computation:
#checksum=`wc < $eplaintmp | sed -n "s/^ *//"`
# Replace the checksum in the old file.  We really should loop here,
# since one iteration may not be enough.  Only replace the first
# checksum, since the second is for btxmac.
#sed -e "1,50s/checksum = .*,/checksum = \"$checksum\",/" $eplaintmp \
#  > eplain.tex

chmod og-w eplain.tex
#ls -lt btxmac.tex path.sty texnames.sty xeplain.tex eplain.tex arrow.tex
#wc eplain.tex
#grep 'checksum.*=' eplain.tex

rm -f $btxtmp $eplaintmp $common1tmp $common2tmp $common3tmp
rm -f $arrow1tmp $arrow2tmp $pathtmp #$ifpdftmp
