#!/bin/bash

. ./common.sh $*

rm -rf $SRCDIR
(cd $TMPDIR; tarz $TLSRCDIR/texlive-*-source.tar.xz)

echo -e "\e[0;32m *** TeX Live 2011 fix ***\e[0m"
cd $SRCDIR
zpatch 0 $TLSRCDIR/branch2011-23716-synctex-coordinate-fix.patch
zpatch 0 $TLSRCDIR/branch2011-23740-enctex-off-by-one-fix.patch
zpatch 0 $TLSRCDIR/branch2011-24159-line-number-fix.patch
zpatch 0 $TLSRCDIR/branch2011-24346-dvips-buffer-overrun.patch
zpatch 0 $TLSRCDIR/branch2011-24347-ptex-memory-corruption.patch
# zpatch 0 $TLSRCDIR/branch2011-24348-eptex-fixes.patch
#    same as eptex-????????-justcopyfix.diff
zpatch 0 $TLSRCDIR/branch2011-24363-fontforge-array-size.patch
zpatch 0 $TLSRCDIR/branch2011-24460-ttf2pk-alloc.patch
zpatch 0 $TLSRCDIR/branch2011-24766-link-man.patch

cd texk/

# pxdvi
cp -r xdvik pxdvik
echo -e "\e[0;32m *** pxdvi ***\e[0m"
cd pxdvik
zpatch 1 $TLSRCDIR/xdvik-????????-texlive????.diff
zpatch 1 $TLSRCDIR/pxdvik-????????-density.diff
zpatch 1 $TLSRCDIR/uptex/pxdvik-????????-uptex.diff
# enable otvalid module
cd ../../libs/freetype2/freetype-2.4.4/
sedwrite modules.cfg 's/# (AUX_MODULES \+= otvalid)/\1/'
cd ../../../m4 
sedwrite kpse-pkgs.m4 "s/xdvik/xdvik\npxdvik/"

# \dtou patch for dvipdfmx
echo -e "\e[0;32m *** dvipdfmx (for dtou) ***\e[0m"
cd $SRCDIR/texk/dvipdfmx/dvipdfmx-20110311
zpatch 1 $TLSRCDIR/dvipdfmx-????????-dtou.diff

cd $SRCDIR/texk/web2c/
zpatch 1 $TLSRCDIR/ovp2ovf_web.patch
echo -e "\e[0;32m *** e-pTeX fix ***\e[0m"
# SyncTeX for e-pTeX
zpatch 1 $TLSRCDIR/eptex/eptex-20111022-synctex-tl11.diff
# justcopy fix for e-pTeX
zpatch 1 $TLSRCDIR/eptex/eptex-????????-justcopyfix.diff
# check_kanji fix for pTeX
zpatch 1 $TLSRCDIR/ptex/ptex-check_kanji-????????.diff

# pMetaPost
if [ "$FLG_PMPOST" = "TRUE" ] ;then
echo -e "\e[0;32m *** pMetaPost ***\e[0m"
  (cd ../; zpatch 0 $TLSRCDIR/pmpost-????????-tl11.diff )
  (cd ../; zpatch 0 $TLSRCDIR/pmpost-svg-????????-tl11.diff )
fi

# updmap
echo -e "\e[0;32m *** updmap ***\e[0m"
echo -e "\e[0;33m[copying]   \e[0m updmap-2011xxxx-tl11"
cp  $TLSRCDIR/updmap-????????-tl11 $SRCDIR/texk/tetex/updmap.pl

# dvips
echo -e "\e[0;32m *** dvips ***\e[0m"
cd $SRCDIR/texk/dvipsk
zpatch 1 $TLSRCDIR/dvipsk-????????-noomega-tl11.diff

exit 0
####################

# upTeX
if [ "$FLG_UP" = "TRUE" ] ;then
  echo -e "\e[0;32m *** upTeX patch ***\e[0m"
  # uptexenc (modified ptexenc for upTeX)
  cd $SRCDIR/texk/
  tarz $TLSRCDIR/uptex/uptexenc-*.tar.xz
  chmod +x uptexenc/configure
  cd ../m4
  cp kpse-ptexenc-flags.m4 kpse-uptexenc-flags.m4
  zpatch 0 ../texk/uptexenc/uptexenc-m4-texlive2010.diff
  # patches for dvipdfmx, pxdvi, makejvf 
  cd ../texk
  zpatch 1 $TLSRCDIR/uptex/uptex-*-dvipdfmx-texlive2010.diff
  zpatch 1 $TLSRCDIR/uptex/uptex-*-pxdvik-texlive2010.diff
  zpatch 1 $TLSRCDIR/uptex/uptex-*-makejvf-texlive2010.diff
  # Web2C
  cd web2c
  cp -r ptexdir uptexdir
  mv uptexdir/ptex-base.ch uptexdir/uptex-base.ch
  mv uptexdir/pbibtex.ch  uptexdir/upbibtex.ch
  mv uptexdir/ppltotf.ch  uptexdir/uppltotf.ch
  mv uptexdir/ptftopl.ch  uptexdir/uptftopl.ch
  mv uptexdir/pdvitype.ch uptexdir/updvitype.ch
  mv uptexdir/ptex.defines uptexdir/uptex.defines 
  cp $TLSRCDIR/uptex/uptex.am uptexdir/am/ # Makefile fragment for upTeX
  cd ..
  if [ "$FLG_PP" = "TRUE" ] ;then
    PMODE=ignore zpatch 0 $TLSRCDIR/uptex/uptex-*-uptexbase-texlive2010.diff
    zpatch 0 $TLSRCDIR/uptex/uptex-*-ptex100711-fix.diff
  else 
    zpatch 0 $TLSRCDIR/uptex/uptex-*-uptexbase-texlive2010.diff
  fi 
  zpatch 0 $TLSRCDIR/uptex/uptex-*-kcatcode_fix.diff
  zpatch 0 $TLSRCDIR/uptex/uptex-*-web2clib-texlive2010.diff
  cp web2c/omegaware/ovp2ovf.web web2c/omegaware/upovp2ovf.web 
  cp web2c/omegaware/ovp2ovf.ch  web2c/omegaware/upovp2ovf.ch
  zpatch 0 $TLSRCDIR/uptex/uptex-*-upovp2ovf-texlive2010.diff
  # include uptex.am
  sedwrite web2c/Makefile.am "s+#(.*/uptex.am)+\1+"
fi

# e-upTeX
if [ "$FLG_EP" = "TRUE" -a "$FLG_UP" = "TRUE" ] ;then
  echo -e "\e[0;32m *** e-upTeX patch ***\e[0m"
  cd $SRCDIR/texk/web2c/
  cp -r uptexdir euptexdir
  mv euptexdir/uptex-base.ch euptexdir/eptex-base.ch
  cd euptexdir
  cp $TLSRCDIR/eptex/pdfstrcmp.ch ./
  cp $TLSRCDIR/eptex/pdfsavepos.ch ./
  touch eptex.defines
  PMODE=ignore zpatch 2 $TLSRCDIR/eptex/eptex-??????-texlive2010.diff
  mv uptex.defines euptex.defines
  mv eptex-base.ch euptex-base.ch
  cp ../eptexdir/fam256.ch fam256.ch
  zpatch 0 $TLSRCDIR/eptex/euptex-??????-up*-texlive2010.diff
  chmod +x euptrip.test
  rm am/eptex.am
  cp $TLSRCDIR/eptex/euptex.am am/euptex.am
  cd ../..
  # include uptex.am
  sedwrite web2c/Makefile.am "s+#(.*/euptex.am)+\1+"
fi
exit 0
