#!/bin/sh

map=/etc/texmf/vfontmap
cfg=/etc/texmf/dvips/config-ja.ps
pdfm=/etc/texmf/dvipdfm/config

case "$1" in
add)
# for vfontmap
if [ -f "${map}" ] ; then
    if ! grep -q "#-- ptex-jisfonts begin" ${map}; then
    cp -f ${map} ${map}_giro
    sed -e '$a\
#-- ptex-jisfonts begin\
jis	min\
jisg	goth\
#-- ptex-jisfonts end' < ${map} > ${map}_$$ && \
    mv -f ${map}_$$ ${map}
    fi
else
echo "You should install xdvik-ja first then do \"jisftconfig add\" "
fi
# for config.ps
if [ -f ${cfg} ] ; then
    if ! grep -q "%-- ptex-jisfonts begin" ${cfg}; then
    cp -f ${cfg} ${cfg}_giro
    sed -e '/^p +psfonts.amz/a\
%-- ptex-jisfonts begin\
p +jis.map\
%-- ptex-jisfonts end' < ${cfg} > ${cfg}_$$ && \
    mv -f ${cfg}_$$ ${cfg}
    fi
else
echo "You should install dvipsk-ja first then do \"jisftconfig add\" "
fi
# for dvipdfm
if [ -f ${pdfm} ] ; then
    if ! grep -q "%-- ptex-jisfonts begin" ${pdfm}; then
    cp -f ${pdfm} ${pdfm}_giro
    sed -e '$a\
%-- ptex-jisfonts begin\
f jis.map\
%-- ptex-jisfonts end' < ${pdfm} > ${pdfm}_$$ && \
    mv -f ${pdfm}_$$ ${pdfm}
    fi
else
echo "You should install dvipdfm first then do \"jisftconfig add\" "
fi
;;

clean)
rm -f ${map}_giro
rm -f ${cfg}_giro
rm -f ${pdfm}_giro
;;

remove)
if [ -f "${map}" ] ; then
sed -e '/#-- ptex-jisfonts begin/,/#-- ptex-jisfonts end/d' < ${map} > \
${map}_$$ && mv -f ${map}_$$ ${map}
fi

if [ -f ${cfg} ] ; then
sed -e '/%-- ptex-jisfonts begin/,/%-- ptex-jisfonts end/d' < ${cfg} > \
${cfg}_$$ && mv -f ${cfg}_$$ ${cfg}
fi

if [ -f ${pdfm} ] ; then
sed -e '/%-- ptex-jisfonts begin/,/%-- ptex-jisfonts end/d' < ${pdfm} > \
${pdfm}_$$ && mv -f ${pdfm}_$$ ${pdfm}
fi

;;

*)
echo "$0 is called with wrong options"
;;
esac
