#!/bin/sh
# updmap-otf: v0.8
# 10 Jun 2005 by KOBAYASHI R. Taizo <tkoba965@mac.com> v0.8
#    modified to use updmap-sys in teTeX3
# 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
TEXMF=${PREFIX}/share/texmf
FONTDIR=${TEXMF}/fonts/opentype
MAPDIR=${TEXMF}/fonts/map/dvipdfm
STATEDIR=${TEXMF}-var/updmap-otf

export TEXMFSYSVAR=${TEXMF}-var
export PATH=${PREFIX}/bin:${PATH}

###
### 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/fonts/map/dvipdfm/otf-"install font name".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}/A-OTF-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 $MAPDIR/otf-*.map
do
    if [ "otf-cktx.map" != "`basename $i`" ] && [ -f $i ]; then
	MAPFILE=`basename $i | sed -e "s|.map||"`
	case "$MAPFILE" in
	    otf-hiraginox)
		if [ "$HIRAGINO" = "installed" ]; then
			echo "Standby map file : $MAPFILE"
		fi
		;;
	    otf-morisawax)
		if [ "$MORISAWA" = "installed" ]; then
			echo "Standby map file : $MAPFILE"
		fi
		;;
	    otf-kozukax)
		if [ "$KOZUKA" = "installed" ]; then
			echo "Standby map file : $MAPFILE"
		fi
		;;
	    *)
		echo "Standby map file : $MAPFILE"
		;;
	esac
    fi
done

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

if [ -f $STATEDIR/otf-${STATUS}.map ]; then
	echo "CURRENT map file : otf-$STATUS"
fi
}

###
### Cleanup
###

CleanUp() {
for TARGET in $STATEDIR/otf-*.map
do
    if [ "otf-cktx.map" != "`basename ${TARGET}`" ] && [ -f ${TARGET} ]; then
	rm -f ${TARGET}
	echo "Removing ... `basename ${TARGET}`"
	${PREFIX}/bin/updmap-sys --disable `basename ${TARGET}` 2> /dev/null
    fi
done
}

###
### Setup Map files
###

SetupMapFile() {

CleanUp

MAPFILE=otf-$1.map

if [ -f ${MAPDIR}/${MAPFILE} ]; then
    touch ${STATEDIR}/otf-$1.map
    echo "Setting up ... $MAPFILE"
    ${PREFIX}/bin/updmap-sys --enable KanjiMap ${MAPFILE} 2> /dev/null
else
    echo "NOT EXIST $MAPFILE"
    return -1
fi
}

###
### MAIN
###

main() {

${PREFIX}/bin/texhash 2> /dev/null

CheckInstallFont

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

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

main $@

