#!/bin/bash

. ./common.sh $*

cd $SRCDIR/inst
rm -rf texmf-dist
ln -s $TLINSTDIR/texmf-dist texmf-dist
mkdir texmf-var &>/dev/null
mktexlsr ./texmf-var 

# pmpost
if [ "$FLG_PMPOST" = "TRUE" ] ;then
mkdir -p texmf/metapost/base
echo -e "\e[0;33m[copying]   \e[0m mpost.mp -> pmpost.mp"
cp texmf-dist/metapost/base/mpost.mp texmf/metapost/base/pmpost.mp
fi

if [ "$FLG_NOOTF" = "TRUE" ] ;then
echo -e "\e[1;31mDo not install japanese and otf packages.\e[0m"
else
# japanese.sty
mkdir -p texmf/tex/platex/japanese
cd texmf/tex/platex/japanese
tarz $TLSRCDIR/ptex/japanese.zip
rm japanese.ldf sample.tex &>/dev/null
echo -e "\\input ptex.tex\\input japanese.ins\ny" | ptex -ini &>/dev/null
mkdir -p ../../../doc/platex/japanese
mv sample.* ../../../doc/platex/japanese/
mv README ../../../doc/platex/japanese/
cd ../../../..

# otf package
tarz $TLSRCDIR/ptex/otfbeta.zip
#if [ "$FLG_UP" = "TRUE" ] ;then
#    tar zxf $TLSRCDIR/uptex/otfstable-uptex-0.05.tar.gz
#fi
cd otfbeta
sedwrite mkjvf "s/ovp2ovf/wovp2ovf/" && chmod +x mkjvf
#if [ "$FLG_UP" = "TRUE" ] ;then
#    sedwrite umkjvf "s/'ovp2ovf/'wovp2ovf/" && chmod +x umkjvf
#fi
for x in script/*.pl; do sedwrite $x "s/ovp2ovf/wovp2ovf/"; chmod +x $x; done
./makeotf
#if [ "$FLG_UP" = "TRUE" ] ;then
#    ./umakeotf
#fi
mkdir -p ../texmf/fonts/tfm/otf && cp tfm/*.tfm ../texmf/fonts/tfm/otf/
mkdir -p ../texmf/fonts/vf/otf && cp vf/*.vf ../texmf/fonts/vf/otf/
mkdir -p ../texmf/tex/latex/otf && cp sty/* ../texmf/tex/latex/otf
cd ..
rm -rf otfbeta
fi

echo -e "\e[0;33m[rewriting] \e[0m updmap.cfg"
echo "KanjiMap otf-cktx.map" >> texmf/web2c/updmap.cfg || exit
(cd $XMAP && tarz $TLSRCDIR/ptex/ptexlive-maps-????????.tar.xz)

# xdvi
if [ -f bin/$ARCH/pxdvi ]; then
    cp ../texk/pxdvik/texmf/XDvi texmf/xdvi/XDvi
    cp ../texk/pxdvik/texmf/pxdvi.cfg texmf/xdvi/pxdvi.cfg
    cp $TLSRCDIR/xdvi-ptex.map.sample texmf/xdvi/
    rm -f texmf/xdvi/xdvi-ptex.map &>/dev/null
    rm -f texmf/xdvi/xdvi-ptex.sample &>/dev/null
    echo -e "\e[0;33m[rewriting] \e[0m updmap.cfg"
    updmap-sys --setoption pxdviUse=true --nomkmap &>/dev/null
    sedwrite $TEXMF/xdvi/pxdvi.cfg  "s/kanjix.map/xdvi-ptex.map/"
fi

echo "Finishing..."
mktexlsr &>/dev/null
updmap-sys --syncwithtrees --nomkmap &>/dev/null

exit 0
