#!/bin/sh
# Convert WEB programs not needing special treatment to C.
# $1 is the Pascal file to be converted.
#
##### author: Ken Nakano (ken-na@ascii.co.jp)
# copied by Matsu
#
# $Id: jconvert,v 1.5 2000/03/20 02:28:13 hideyuki Exp $
#
web2cdir=../../web2c
target=$1

usage () {
	echo ""
	echo "Usage: $0 <basefile>"
	echo "  <basefile> = ptex|tftopl"
	echo ""
}

convert_jmp () {
	cat $web2cdir/common.defines $web2cdir/texmf.defines \
		$web2cdir/mfmp.defines ./prockanji.defines \
                ../kanji.defines ./jmp.p \
	| sed -f $web2cdir/cvtmf1.sed \
	| $web2cdir/web2c -h../../texmfmp.h -m -cmpcoerce \
	| sed -f $web2cdir/cvtmf2.sed \
	| $web2cdir/fixwrites mp \
	| $web2cdir/splitup -i -l 10000 mp
	sleep 2
	cat mpcoerce.h $web2cdir/coerce.h > xmpcoerce.h
	echo '#include "kanji.h"' >> xmpcoerce.h
	mv xmpcoerce.h mpcoerce.h
	touch mpd.h
}

convert_tool () {
	cat $web2cdir/common.defines ./prockanji.defines \
            ../kanji.defines ext.defines $target.p \
	| $web2cdir/web2c -hjmpost.h -c$target \
	| $web2cdir/fixwrites > $target.c
}


case $target in 
	jmp) convert_jmp
		cfile=mp2.c # last output file, or thereabouts
		output=
		output_files="mp[0-9]*.c mpini.c mpd.h mpcoerce.h"
    ;;
	pdvitomp) convert_tool
		cfile=$target.c
		output_files="$target.c $target.h"
		;;
	*) usage;
esac

if test ! -s $cfile || grep @error@ $output_files >/dev/null; then
  : ${TMPDIR=./failure}
  # 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"
  if test ! -d $TMPDIR ; then
     mkdir $TMPDIR
  fi
  (cd $TMPDIR && rm -f $output_files)
  echo "$0:   $cmd" >&2
  $cmd
  exit 1
fi

exit 0
