#!/bin/sh
# Convert WEB programs not needing special treatment to C.
# 
# $1 is the Pascal file to be converted.
# $2 is the C file to be created.
# $3, if present, is extended with .h, and #included in the C file, and
# extended with .defines, and prepended along with the common
# definitions.

: ${srcdir=.}

usage="Usage: $0 <basefile>."
basefile=
while test $# -gt 0; do
  case $1 in
    -*) echo "$1?" >&2; echo "$usage" >&2; exit 1;;
     *) if test -n "$basefile"; then
          echo "$1?" >&2; echo "$usage" >&2; exit 1; fi
        basefile=$1;;
  esac
  shift
done
if test -z "$basefile"; then
  echo "Missing basefile argument." >&2
  echo "$usage" >&2
  exit 1
fi

pascalfile=$basefile.p
cfile=$basefile.c

# This is for tangleboot if the build and source directories are different.
test -r $pascalfile || pascalfile=$srcdir/$pascalfile

# We use cpascal.h by default instead of config.h because straight C
# routines can only be confused by the definitions for `chr', etc.
hfile=cpascal.h
more_defines=
web2c_options=-c$basefile
precmd=
midcmd=
fixwrites_options=
postcmd=
output="> $cfile"
output_files="$cfile $basefile.h"

case $basefile in
  bibtex)
    midcmd="| sed -f $srcdir/web2c/cvtbib.sed";;
  mf|mp) # MF and MP are almost the same.
    more_defines="$srcdir/web2c/texmf.defines $srcdir/web2c/mfmp.defines"
    precmd="| sed -f $srcdir/web2c/cvtmf1.sed"
    web2c_options="-m -c${basefile}coerce"
    hfile=texmfmp.h
    midcmd="| sed -f $srcdir/web2c/cvtmf2.sed"
    postcmd="| ./web2c/splitup $basefile"
    cfile=${basefile}2.c # last output file, or thereabouts
    output=
    output_files="$basefile[0-9]*.c ${basefile}ini.c ${basefile}d.h \
${basefile}coerce.h"
    ;;
  tex|etex|pdftex|pdfetex|omega)
    more_defines="$srcdir/web2c/texmf.defines"
    prog_defines="$srcdir/${basefile}dir/$basefile.defines"
    if test -f $prog_defines; then
      more_defines="$more_defines $prog_defines"
    fi
    web2c_options="-t -c${basefile}coerce"
    hfile=texmfmp.h
    fixwrites_options=-t
    postcmd="| ./web2c/splitup ${basefile}"
    cfile=${basefile}2.c # last output file, or thereabouts
    output=
    output_files="$basefile[0-9]*.c ${basefile}ini.c ${basefile}d.h \
${basefile}coerce.h"
    ;;
esac

# Do it.
eval "cat $srcdir/web2c/common.defines $more_defines $pascalfile \
  $precmd \
  | ./web2c/web2c -h$hfile $web2c_options \
  $midcmd \
  | ./web2c/fixwrites $fixwrites_options $basefile \
  $postcmd \
  $output"

# Using the above pipeline as the condition of an if does no good, since
# typical sh's use the status of the first command as the pipeline result.
# So check for an empty output file, or one with the error marker we put in.
if test ! -s $cfile || grep @error@ $output_files >/dev/null; then
  : ${TMPDIR=/tmp}
  # Don't just delete it, since it may be useful for debugging.
  echo "$0: conversion of $pascalfile failed, moving dregs:" >&2
  cmd="mv $output_files $TMPDIR"
  (cd $TMPDIR && rm -f $output_files)
  echo "$0:   $cmd" >&2
  $cmd
  exit 1
fi

case $basefile in
  bibtex)
    sed -e 's/(buftype)//g' -e 's/(pdstype)//g' <bibtex.h >xbibtex.h
    mv xbibtex.h bibtex.h
    ;;
  tex|etex|pdftex|pdfetex|omega|mf|mp)
    sleep 2 # so timestamps are definitely later, to avoid make weirdness
    cat ${basefile}coerce.h $srcdir/web2c/coerce.h >x${basefile}coerce.h
    mv x${basefile}coerce.h ${basefile}coerce.h
    touch ${basefile}d.h
    ;;
esac

exit 0
