#!/bin/bash
# modified from 7font-search.sh in ptexlive-20100322

. ./common.sh $*

# -----------------------------------
### font search and generate map files


# usage: present minr gothr minb gothb mgoth gotheb minl
# return value: 0: not found, 2: minr and gothr are found, >=5
present() {
  mingoth=0
  kpsewhich -format 'truetype fonts' $1 > /dev/null && mingoth=1
  kpsewhich -format 'opentype fonts' $1 > /dev/null && mingoth=1
  if [ $mingoth -ne 1 ] ; then return 0; fi
  kpsewhich -format 'truetype fonts' $2 > /dev/null && mingoth=2
  kpsewhich -format 'opentype fonts' $2 > /dev/null && mingoth=2
  if [ $mingoth -ne 2 ] ; then return 0; fi
  kpsewhich -format 'truetype fonts' $3 > /dev/null && mingoth=3
  kpsewhich -format 'opentype fonts' $3 > /dev/null && mingoth=3
  if [ $mingoth -ne 3 ] ; then return 2; fi
  kpsewhich -format 'truetype fonts' $4 > /dev/null && mingoth=4
  kpsewhich -format 'opentype fonts' $4 > /dev/null && mingoth=4
  if [ $mingoth -ne 4 ] ; then return 2; fi
  kpsewhich -format 'truetype fonts' $5 > /dev/null && mingoth=5
  kpsewhich -format 'opentype fonts' $5 > /dev/null && mingoth=5
  if [ $mingoth -ne 5 ] ; then return 2; fi
  kpsewhich -format 'truetype fonts' $6 > /dev/null && mingoth=6
  kpsewhich -format 'opentype fonts' $6 > /dev/null && mingoth=6
  if [ $mingoth -ne 6 ] ; then return 5; fi
  kpsewhich -format 'truetype fonts' $7 > /dev/null && mingoth=7
  kpsewhich -format 'opentype fonts' $7 > /dev/null && mingoth=7
  return $mingoth
}

dispfnt() {
  if [ $2 -eq 0 ]; then
    echo -e "$1 \e[0;31mNot found\e[0m"
  else
    FNTCAN="$FNTCAN$3/"
    if [ $2 -ge 5 ]; then
      echo -e "$1 \e[1;32m$2\e[0m fonts"
    else
      echo -e "$1 \e[0;33m$2\e[0m fonts"
    fi
  fi
}

mkdir -p $XMAP
cd $XMAP

echo; echo -e "\e[1;36mFound fonts: \e[0m"
mktexlsr $TEXMF &>/dev/null
FNTCAN=

present HiraMinProN-W3.otf HiraKakuProN-W3.otf \
        HiraMinProN-W6.otf HiraKakuProN-W6.otf HiraMaruProN-W4.otf \
        HiraKakuStdN-W8.otf HiraMinProN-W2.otf
hirafnt=$?
dispfnt "(H) Hiragino: " $hirafnt "H"

present A-OTF-RyuminPr6N-Light.otf A-OTF-GothicBBBPr6N-Medium.otf \
        A-OTF-FutoMinA101Pr6N-Bold.otf A-OTF-FutoGoB101Pr6N-Bold.otf A-OTF-Jun101Pr6N-Light.otf \
        A-OTF-MidashiGoPr6N-MB31.otf A-OTF-RyuminPr6N-Light.otf
morifnt=$?
dispfnt "(M) Morisawa: " $morifnt "M"

present KozMinPr6N-Regular.otf KozGoPr6N-Regular.otf \
        KozMinPr6N-Bold.otf KozGoPr6N-Bold.otf KozGoPr6N-Heavy.otf \
        KozGoPr6N-Heavy.otf KozMinPr6N-Light.otf
kozufnt=$?
dispfnt "(K) Kozuka:   " $kozufnt "K"


present ipam.ttf ipag.ttf dummy dummy dummy dummy dummy
ipatfnt=$?; dispfnt "(I) ipa:      " $ipatfnt "I"

present ipaexm.ttf ipaexg.ttf dummy dummy dummy dummy dummy
ipaxfnt=$?; dispfnt "(J) ipaex  :  " $ipaxfnt "J"

mktexlsr $TEXMF &>/dev/null
PXDVIRML=
PXDVIGBM=

### UTF/OTF/pTeX
echo; echo -e "\e[1;36mFonts for pTeX and OTF packages\e[0m"
echo "'N' means 'don't embed fonts'. "
echo -n "fonts for pTeX and OTF packages (${FNTCAN}N) > "
read ANS


updmap-sys --nomkmap --enable KanjiMap otf-@kanjiEmbed@.map &>/dev/null
updmap-sys --nomkmap --enable KanjiMap ptex-@kanjiEmbed@.map &>/dev/null
case $ANS in
    [Hh])
	if [ $hirafnt = 0 ]; then ANS=N; else
	    updmap-sys --nomkmap --setoption kanjiEmbed $hiravar
	    PXDVIRML=HiraMinProN-W3.otf
	    PXDVIGBM=HiraKakuProN-W3.otf
	    ANS=$hiravar
	fi
	;;
    [Mm])
	if [ $morifnt = 0 ]; then ANS=N; else
	    updmap-sys --nomkmap --setoption kanjiEmbed $morivar
	    PXDVIRML=A-OTF-RyuminPr6N-Light.otf
	    PXDVIGBM=A-OTF-GothicBBBPr6N-Medium.otf
	    ANS=$morivar
	fi
	;;
    [Kk])
	if [ $kozufnt = 0 ]; then ANS=N; else
	    updmap-sys --nomkmap --setoption kanjiEmbed kozuka
	    PXDVIRML=KozMinPr6N-Regular.otf
	    PXDVIGBM=KozGoPr6N-Regular.otf
	    ANS=$kozuvar
	fi
	;;
    [Ii])
	if [ $ipatfnt = 0 ]; then ANS=N; else
	    updmap-sys --nomkmap --setoption kanjiEmbed ipa
	    PXDVIRML="ipam.ttf\/AJ16"; PXDVIGBM="ipag.ttf\/AJ16"; ANS=ipa
	fi
	;;
    [Jj])
	if [ $ipaxfnt = 0 ]; then ANS=N; else
	    updmap-sys --nomkmap --setoption kanjiEmbed ipaex
	    PXDVIRML="ipaexm.ttf\/AJ16"; PXDVIGBM="ipaexg.ttf\/AJ16"; ANS=ipaex
	fi
	;;
    [Nn]) ;;
    *) ANS=N;;
esac
case $ANS in
    [Nn])
        updmap-sys --nomkmap --setoption kanjiEmbed noEmbed
	ANS=noEmbed;;
esac


## pxdvi
setrmlgbm() {
    if test -n "$2"; then
        echo "set '$2' as $1."
        sedwrite $TEXMF/xdvi/pxdvi.cfg "s/^replace\s$1\s.*/replace $1 $2$3/"
    else
        echo "$0: No font was found as $1."
        exit 1
    fi
}

rmlgbm() {
   while read FONT; do
        case $FONT in
            \#*) continue;;
        esac
        case $MINGOTH in
            PXDVIRML) MINGOTH=PXDVIGBM;;
            *)        MINGOTH=PXDVIRML;;
        esac
        g=0
        kpsewhich -format 'truetype fonts' $FONT > /dev/null && g=1
        kpsewhich -format 'opentype fonts' $FONT > /dev/null && g=1
        if test $g != 0; then
            echo "  '$FONT' was found as '$MINGOTH'"
	    echo $FONT | grep "\.tt[cf]" &>/dev/null
	    if [ $? = 0 ] ; then
		FONT="$FONT\\\/AJ16"
	    fi
            eval $MINGOTH=$FONT
        fi
    done
}

if [ -z "$PXDVIRML" ] ; then
    echo ; echo -e "\e[1;36mSetting fonts for pxdvi...\e[0m"
    rmlgbm <<EOF
# in case of no mincho font (mainly for Mandriva)
        umeplus-gothic.ttf
        dummy
        VL-Gothic-Regular.ttf
        dummy
# kochi (distribution stopped)
        kochi-mincho.ttf
        kochi-gothic.ttf
# kochi-subst (former of sazanami)
        kochi-mincho-subst.ttf
        kochi-gothic-subst.ttf
# sazanami http://wiki.fdiary.net/font/?sazanami
#        sazanami-mincho.ttf
#        sazanami-gothic.ttf
# UmePlus gothic font family (Mandriva Cooker 20080104)
# http://www.geocities.jp/ep3797/modified_fonts_01.html
        dummy
        umeplus-gothic.ttf
# VL gothic font family http://dicey.org/vlgothic/
        dummy
        VL-Gothic-Regular.ttf
# IPA mona http://www.geocities.jp/ipa_mona/
# (opfc-ModuleHP-1.1.1_withIPAMonaFonts-1.0.5.tar.gz)
        ipam-mona.ttf
        ipag-mona.ttf
# IPA font http://ossipedia.ipa.go.jp/ipafont/
        ipam.otf
        ipag.otf
        ipam.ttf
        ipag.ttf
# IPAex font http://ossipedia.ipa.go.jp/ipafont/
        ipaexm.ttf
        ipaexg.ttf
# DynaLab
        dfmimp3.ttc
        dfgotp5.ttc
# Windows native
        msmincho.ttc
        msgothic.ttc
# Ricoh TTF
        lxmincho.ttc
        lxgothic.ttc
# Turbo Linux 10
        tlmincho.ttc
        tlgothic.ttc
# Solaris 9
        HG-MinchoL.ttf
        HG-GothicB.ttf
# Solaris 10
        hgmlsun.ttf
        hggbsun.ttf
# Kozuka
        KozMinStd-Regular.otf
        KozGoStd-Regular.otf
        KozMinPro-Regular.otf
        KozGoPro-Medium.otf
        KozMinProVI-Regular.otf
        KozGoProVI-Medium.otf
        KozMinPr6N-Regular.otf
        KozGoPr6N-Medium.otf
# Hiragino
        HiraMinPro-W3.otf
        HiraKakuPro-W3.otf
        HiraMinProN-W3.otf
        HiraKakuProN-W3.otf
EOF
fi

if [ -z $PXDVIRML -o -z $PXDVIGBM ]; then
    echo ; echo -e "\e[1;31mI couldn't find fonts for pxdvi.\e[0m"
else
    echo 
    setrmlgbm Ryumin-Light     $PXDVIRML
    setrmlgbm GothicBBB-Medium $PXDVIGBM
fi

echo -e "\e[1;36mRunning mktexlsr ...\e[0m"
mktexlsr
echo -e "\e[1;36mRunning updmap-sys ...\e[0m"
updmap-sys --force &>/dev/null
echo -e "\e[1;36mRunning fmtutil-sys ...\e[0m"
fmtutil-sys --all &>/dev/null

exit 0
