#!/bin/sh

if [ "x$2" != "xPOST-INSTALL" ]; then exit 0; fi

KPSEWHICH=${PKG_PREFIX}/bin/kpsewhich
web2cdir=${PKG_PREFIX}/share/texmf/web2c

pkfonts_path=${PKG_PREFIX}/lib/fonts/pk300
pkfonts="cmr5.300pk"
a2bkfonts_path=${PKG_PREFIX}/share/fonts/vf-a2bk
a2bkfonts="min5.vf rml.tfm"

if [ ! -f ${web2cdir}/texmf.cnf ]; then exit 0; fi

if [ ! -x ${KPSEWHICH} ]; then exit 0; fi

for f in ${pkfonts}; do
  ${KPSEWHICH} $f > /dev/null && pkfonts_exist="${pkfonts_exist} $f"
done

for f in ${a2bkfonts}; do
  ${KPSEWHICH} $f > /dev/null || a2bkfonts_not_exist="${a2bkfonts_not_exist} $f"
done

if [ -n "${pkfonts_exist}" -a -z "${a2bkfonts_not_exist}" ]; then exit 0; fi

echo "******************************************************************************"

if [ "x$3" != "xBATCH" ]; then
  echo "You have to set some variables in the path configuration file for"
  echo "kpathsea ${web2cdir}/texmf.cnf so that the program"
  echo "can find PK fonts, Japanese Virtual fonts (\"a2bk\" type)."
  echo
  read -p "Would you like to add path for these fonts in texmf.cnf? [y]: " ans
  case x${ans} in
  xy*|xY*|x)
    echo
    if [ -z "${pkfonts_exist}" ]; then
      while :; do
        read -p "Path for PK fonts [${pkfonts_path}]: " path
        if [ -z "${path}" ]; then
          path="${pkfonts_path}"
        fi
        fonts_exist=
        for f in ${pkfonts}; do
          ${KPSEWHICH} -path="${path}" $f > /dev/null \
            && fonts_exist="${fonts_exist} $f"
        done
        if [ -n "${fonts_exist}" ]; then
          break
        else 
          read -p "Fonts do not exist in this path. Is it OK? [n]: " ans
          case x${ans} in
          xy*|xY*) break;;
          esac
        fi 
      done
      pkfonts_path="${path}"
    fi

    if [ -n "${a2bkfonts_not_exist}" ]; then
      while :; do
        read -p "Path for a2bk fonts [${a2bkfonts_path}]: " path
        if [ -z "${path}" ]; then
          path="${a2bkfonts_path}"
        fi
        fonts_not_exist=
        for f in ${a2bkfonts}; do
          ${KPSEWHICH} -path="${path}" $f > /dev/null \
            || fonts_not_exist="${fonts_not_exist} $f"
        done
        if [ -z "${fonts_not_exist}" ]; then
          break
        else 
          read -p "Fonts do not exist in this path. Is it OK? [n]: " ans
          case x${ans} in
          xy*|xY*) break;;
          esac
        fi 
      done
      a2bkfonts_path="${path}"
    fi;;

  *) exit 0;;
  esac
  echo
fi

mv ${web2cdir}/texmf.cnf ${web2cdir}/texmf.cnf.bak
cp ${web2cdir}/texmf.cnf.bak ${web2cdir}/texmf.cnf.tmp

if [ -z "${pkfonts_exist}" ]; then
  sed -e "s,^ *PKFONTS *=* \.:,PKFONTS = \.:${pkfonts_path}:," \
      ${web2cdir}/texmf.cnf.tmp > ${web2cdir}/texmf.cnf
  echo "Add path for PK fonts to PKFONTS in ${web2cdir}/texmf.cnf"
fi

if [ -n "${a2bkfonts_not_exist}" ]; then
  if [ -f ${web2cdir}/texmf.cnf ]; then
    mv ${web2cdir}/texmf.cnf ${web2cdir}/texmf.cnf.tmp
  fi 
  sed -e "s,^ *VFFONTS *=* \.:,VFFONTS = \.:${a2bkfonts_path}:," \
      -e "s,^ *TFMFONTS *=* \.:,TFMFONTS = \.:${a2bkfonts_path}:," \
      ${web2cdir}/texmf.cnf.tmp > ${web2cdir}/texmf.cnf
  echo "Add path for a2bk fonts to VFFONTS in ${web2cdir}/texmf.cnf"
  echo "Add path for a2bk fonts to TFMFONTS in ${web2cdir}/texmf.cnf"
fi

rm -f ${web2cdir}/texmf.cnf.tmp

echo "******************************************************************************"
