#!/bin/sh
# updmap-otf: v0.7
# 07 Nov 2004 by KOBAYASHI R. Taizo <tkoba965@mac.com> v0.7
#    do not echo back the message of updmap.
# 17 Oct 2004 by KOBAYASHI R. Taizo <tkoba965@mac.com> v0.6
#    set hiragino map file if nofont is installed and arg is auto.
# 04 Oct 2004 by KOBAYASHI R. Taizo <tkoba965@mac.com> v0.5
#    handl standby map files more strictly
# 20 Sep 2004 by KOBAYASHI R. Taizo <tkoba965@mac.com> v0.4
#    hand over current status to map file installer
# 19 Sep 2004 by KOBAYASHI R. Taizo <tkoba965@mac.com> v0.3
#    handl *-udvips.map in TEXMF/dvipdfm/config/otf/
# 02 Mar 2004 by KOBAYASHI R. Taizo <tkoba@ike-dyn.ritsumei.ac.jp> v0.2
#    added noFont-udvips.map
# 28 Feb 2004 by KOBAYASHI R. Taizo <tkoba@ike-dyn.ritsumei.ac.jp> v0.1

PREFIX=/usr/local
FONTDIR=${PREFIX}/share/texmf/dvipdfm/CIDFont
DVIPSMAPDIR=${PREFIX}/share/texmf/dvips/config/extmap
DVIPDFMMAPDIR=${PREFIX}/share/texmf/dvipdfm/config/otf

###
### Usage
###

Usage() {
cat <<EOF
  Usage:  updmap-otf {hiragino|morisawa|kozuka|nofont|"installed font name"|auto|status}

     hiragino:   set Hiragino Fonts embedded in pdf files by teTeX otf package
     morisawa:   set Morisawa Fonts embedded in pdf files by teTeX otf package
     kozuka:     set kozuka Fonts embedded in pdf files by teTeX otf package
     nofont:     set no fonts are embedded
                 If your system does not have above 3 font families,
                 this target is selected automatically.
     "installed font name":
                 set fonts which are installed as TEXMF/dvipdfm/config/otf/*-udvips.map
     auto:       set fonts automatically
     status:     get information about current environment and usable font map

EOF
}


###
### Check Installed Font
###

CheckInstallFont() {
if [ -f ${FONTDIR}/HiraMinPro-W3.otf ]; then
    HIRAGINO=installed
else
    HIRAGINO=""
fi
    
if [ -f ${FONTDIR}/RyuminPro-Light.otf ]; then
    MORISAWA=installed
else
    MORISAWA=""
fi

if [ -f ${FONTDIR}/KozMinPro-Regular-Acro.otf ]; then
    KOZUKA=installed
else
    KOZUKA=""
fi
}

###
### GetStatus
###

GetStatus() {

for i in $DVIPDFMMAPDIR/*-udvips.map
do
    if [ -f $i ]; then
	MAPFILE=`basename $i | sed -e "s|-udvips.map||"`
	case "$MAPFILE" in
	    hiragino)
		if [ "$HIRAGINO" = "installed" ]; then
			echo "Standby map file : $MAPFILE"
		fi
		;;
	    morisawa)
		if [ "$MORISAWA" = "installed" ]; then
			echo "Standby map file : $MAPFILE"
		fi
		;;
	    kozuka)
		if [ "$KOZUKA" = "installed" ]; then
			echo "Standby map file : $MAPFILE"
		fi
		;;
	    *)
		echo "Standby map file : $MAPFILE"
		;;
	esac
    fi
done

for i in $DVIPSMAPDIR/*-udvips.map
do
    if [ "ckt-udvips.map" != "`basename $i`" ] && [ -f $i ]; then
	STATUS=`basename $i | sed -e "s|-udvips.map||"`
    fi
done

echo "INSTALLED map file : $STATUS"
}

###
### Cleanup
###

CleanUp() {
for TARGET in $DVIPSMAPDIR/*-udvips.map
do
    if [ "ckt-udvips.map" != "`basename ${TARGET}`" ] && [ -f ${TARGET} ]; then
	rm -f ${TARGET}
	echo "Remove `basename ${TARGET}`"
    fi
done
}

###
### Install Map files
###

InstallMapFile() {

CleanUp

MAPFILE=${DVIPDFMMAPDIR}/$1-udvips.map

if [ -f ${MAPFILE} ]; then
    cp ${MAPFILE} ${DVIPSMAPDIR}
    echo "Install `basename $MAPFILE`"
else
    echo "NOT EXIST `basename $MAPFILE`"
    return -1
fi
}


###
### MAIN
###

main() {

CheckInstallFont

if [ $# != 1 ] ; then
    eval Usage ${0##*/}
    return -1
fi

case "$1" in
     hiragino)
	if [ "$HIRAGINO" = "installed" ]; then
	    InstallMapFile hiragino
	else
	    main auto
	fi
	;;
     morisawa)
	if [ "$MORISAWA" = "installed" ]; then
	    InstallMapFile morisawa
	else
	    main auto
	fi
	;;
     kozuka)
	if [ "$KOZUKA" = "installed" ]; then
	    InstallMapFile kozuka
	else
	    main auto
	fi
	;;
     nofont)
	    InstallMapFile nofont
	;;
     auto)
	GetStatus
	MAPFILE=${DVIPDFMMAPDIR}/$STATUS-udvips.map
	if [ "$STATUS" = "morisawa" ] && [ "$MORISAWA" = "installed" ]; then
	    InstallMapFile morisawa
	elif [ "$STATUS" = "kozuka" ] && [ "$KOZUKA" = "installed" ]; then
	    InstallMapFile kozuka
	elif [ "$STATUS" = "nofont" ] && [ "$HIRAGINO" = "installed" ]; then
	    InstallMapFile hiragino
	elif [ -f $MAPFILE ]; then
	    InstallMapFile $STATUS
	elif [ "$HIRAGINO" = "installed" ]; then
	    InstallMapFile hiragino
	elif [ "$MORISAWA" = "installed" ]; then
	    InstallMapFile morisawa
	elif [ "$KOZUKA" = "installed" ]; then
	    InstallMapFile kozuka
	else
	    InstallMapFile nofont
	fi
	;;
     status)
	GetStatus
	return 0
	;;
     *)
     	if [ -f ${DVIPDFMMAPDIR}/$1-udvips.map ]; then
		InstallMapFile $1
	else
		eval Usage ${0##*/}
		return -1
	fi
	;;
esac

if [ -f ${PREFIX}/bin/updmap ]; then
    echo "executing updmap... "
    ${PREFIX}/bin/updmap 2> /dev/null
fi
}

main $@

