#!/bin/bash

# CFLAGS etc. 
CFLAGS="-DHAVE_CONFIG_H  -I. -I.. -I../.. -I../..  -s -O2"
LDFLAGS=
CC=gcc

# compile e-upTeX
EUPTEX=1

# To enable FAM256 patch, set FAM256 to 1.
FAM256=0

#-------------------------

EPDIR=`echo ../eptex-[0-9]*`
EPVER=`echo $EPDIR | sed "s/\.\.\/eptex-//"`

rm -rf ../eptexdir ../euptex

cd ..
if test "$FAM256" = "1"; then
  cat texmfmem.h|sed "s/#if\ \!defined(Omega)/#if\ \!defined(Omega)\ \&\&\ \!defined(TeX)/" > texmfmem2.h
  mv texmfmem.h texmfmem.h.orig
  mv texmfmem2.h texmfmem.h
fi


cp -r ptex eptexdir

cd eptexdir

if test "$FAM256" = "1"; then
cat <<EOF >> kanji.defines
  @define function getintone();
  @define procedure setintone();
EOF
fi

cp ../etexdir/etex.ch .
patch -p0 < $EPDIR/etex.diff
patch -p0 -f < $EPDIR/ep1.diff
cp ptexextra.h ptexextra.in
patch -p0 < $EPDIR/pconvert.diff
cat ptexextra.in | sed "s/EPTEX-VERSION/$EPVER/" > ptexextra.h

if test "$FAM256" = "1"; then
  ../tie -m eptex.web ptex.web ptex.ch etex.ch \
      $EPDIR/etex-sysdep.ch $EPDIR/fp.ch $EPDIR/fam256.ch 
else
  ../tie -m eptex.web ptex.web ptex.ch etex.ch \
      $EPDIR/etex-sysdep.ch $EPDIR/fp.ch
fi
../tangle eptex
./pconvert eptex

if test "$FAM256" = "1"; then
  cat <<EOF >> texd.h
#define getintone(w) (w).cint1
#define setintone(w,a) ((w).cint1=a)
EOF
fi

$CC $LDFLAGS $CFLAGS -o eptex texini.c tex0.c tex1.c tex2.c tex3.c \
    ptexextra.c kanji.c version.c usage.c  ../lib/lib.a plib.a \
    ../../kpathsea/.libs/libkpathsea.a -lm ../../ptexenc/.libs/libptexenc.a

cd $EPDIR
pwd

if test "$EUPTEX" = "1"; then
  cd ..
  cp -r uptexdir/ euptex
  cd euptex
  if test "$FAM256" = "1"; then
  cat <<EOF >> kanji.defines
  @define function getintone();
  @define procedure setintone();
EOF
  fi
  patch -p1 < $EPDIR/up1.diff
  cp ../etexdir/etex.ch .
  cp $EPDIR/fam256.ch .
  patch -p0 < $EPDIR/etex.diff
  patch -p0 < $EPDIR/uptex.diff 
  patch -p0 < $EPDIR/up2.diff &>/dev/null 
    # This patch may be fail, but it's not problem.

  cat ptexextra.in | sed "s/EPTEX-VERSION/$EPVER/" \
    | sed "s/UPTEX-VERSION/`cat ../uptexdir/uptex.version`/"> ptexextra.h
  if test "$FAM256" = "1"; then
    ../tie -m euptex.web uptex.web uptex.ch etex.ch \
        $EPDIR/etex-sysdep.ch $EPDIR/fp.ch fam256.ch 
  else
    ../tie -m euptex.web uptex.web uptex.ch etex.ch \
        $EPDIR/etex-sysdep.ch $EPDIR/fp.ch
  fi
  ../tangle euptex
  ./pconvert euptex
  if test "$FAM256" = "1"; then
    cat <<EOF >> texd.h
#define getintone(w) (w).cint1
#define setintone(w,a) ((w).cint1=a)
EOF
fi
  $CC $LDFLAGS $CFLAGS -o euptex texini.c tex0.c tex1.c tex2.c tex3.c \
       ptexextra.c kanji.c version.c usage.c  ../lib/lib.a plib.a \
       ../../ptexenc/.libs/libptexenc.a ../../kpathsea/.libs/libkpathsea.a -lm
  
  cd $EPDIR
fi

echo " " 
ls -l ../eptexdir/eptex ../euptex/euptex

if test "$FAM256" = "1"; then
  cd ..
  mv texmfmem.h.orig texmfmem.h
fi



# For making ARCHIVES

# *** Generate e-pTeX archive ***
# rm eptex-*/*~ -i; rm eptex-*/*/*~ -i
# tar jcvf eptex-090223.tar.bz2 eptex-*/; mv eptex-*.tar.bz2 /opt/home-supp/tl08/

# *** Generate tl08-compile-* archive ***
#  (at /opt/home-supp/tl08) 
#  tar jcvf tl08-compile-090223.tar.bz2 bld*.sh \
#    xdvik-tl08.patch.gz updmap-tl08 option hyphen/ \
#    up-tl08.patch ep-tl08.patch compile.txt

# (in root)
# cp /tmp/texlive-*-source/inst/bin/x86_64-unknown-linux-gnu/eptex \
#    /opt/TeX/bin/x86_64-unknown-linux-gnu
# cp /tmp/texlive-*-source/inst/bin/x86_64-unknown-linux-gnu/euptex \
#    /opt/TeX/bin/x86_64-unknown-linux-gnu
# cp /tmp/texlive-*-source/inst/texmf/web2c/eptex.pool /opt/TeX/texmf/web2c
# cp /tmp/texlive-*-source/inst/texmf/web2c/euptex.pool /opt/TeX/texmf/web2c
# sync; fmtutil-sys --all

# version 番号が中に書かれているファイルは，README.txt 中に * がついた
#  ファイルと，ep-tl08.patch, up-tl08.patch, compile.txt である．
