#!/bin/sh

cfg_default=/etc/texmf/dvipsj/config.ps
#XDVI=/etc/texmf/vfontmap
#cfg_old=/etc/texmf/dvipsj/config.ps

set_up ()
{
cfg_var="$1"
     if [ -f ${cfg_var} ] ; then
    	if ! grep -q "%-- morisawa begin" ${cfg_var}; then
	echo -n "modifying ${cfg_var} ..."
    	sed -e '/^p psfonts.map/a\
%-- morisawa begin\
p +morisawa.map\
%-- morisawa end' < ${cfg_var} > ${cfg_var}_$$ && \
    	mv -f ${cfg_var}_$$ ${cfg_var}
	echo "done"
    	fi
      else
	echo "You should install dvipsk-ja first then do \"morisawa-config add\" "
     fi
    return 0
}

set_up_other ()
{
cfg_var="$1"
     if [ -f ${cfg_var} ] ; then
    	if ! grep -q "%-- morisawa begin" ${cfg_var}; then
	echo -n "modifying ${cfg_var} ..."
    	sed -e '$a\
%-- morisawa begin\
p +morisawa.map\
%-- morisawa end' < ${cfg_var} > ${cfg_var}_$$ && \
    	mv -f ${cfg_var}_$$ ${cfg_var}
	echo "done"
    	fi
      else
	echo "You should install ${cfg_var} first."
      fi
    return 0
}

clean_up ()
{
cnf="$1"
      if [ -f ${cnf} ] ; then
      echo -n "removing settings of ${cnf} ..."
      sed -e '/%-- morisawa begin/,/%-- morisawa end/d' < ${cnf} > \
      ${cnf}_$$ && mv -f ${cnf}_$$ ${cnf}
      echo "done"
      fi
    return 0
}

if [ $# -eq 1 ] ; then
cfg=${cfg_default}
   if [ "$1" = "add" ] ; then
       set_up $cfg
   elif [ "$1" = "remove" -o "$1" = "rem" ] ; then
       clean_up ${cfg}
   elif [ "$1" = "xdvi" ] ; then
       if [ -x /usr/sbin/update-vfontmap ]; then
	   echo "It is not necessary to run \"morisawa-config xdvi\" anymore."
       else
	   echo "You should upgrade xdvik-ja to 22.84-j1.21 or later."
       fi
   elif [ "$1" = "remxdvi" ] ; then
       if [ -x /usr/sbin/update-vfontmap ]; then
	   echo "It is not necessary to run \"morisawa-config remxdvi\" anymore."
       else
	   echo "You should upgrade xdvik-ja to 22.84-j1.21 or later."
       fi
   else
      echo "$0 is called with wrong options"
   fi
elif [ $# -eq 2 ] ; then
#cfg=/etc/texmf/dvipsj/"$2"
cfg=/etc/texmf/dvipsj/"$2"
   if [ "$1" = "add" ] ; then
       set_up_other ${cfg}
   elif [ "$1" = "remove" -o "$1" = "rem" ] ; then
       clean_up ${cfg}
   else
      echo "$0 is called with wrong options"
   fi
else
echo "$0 is called with wrong options"
fi
