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

. ./common.sh $*

TRUETYPE=$TEXMF/fonts/truetype
OPENTYPE=$TEXMF/fonts/opentype

# -----------------------------------
### symlink 

# usage: lns dist-link src-dir1 src-dir2 ...
lns() {
    LINK=notyet
    DIST="$1"
    shift
    #echo "DIST=$DIST"
    if [ -h "$DIST" ]; then return; fi
    while test -n "$1" -a $LINK = "notyet"; do
	#echo "  $1"
	if test -f "$1" -o -d "$1"; then
	    echo -e "\e[0;33m[linking ]\e[0m   $1"
	    echo -e "             to \$TEXMF/fonts/`basename $DIST`"
	    ln -sf "$1" "$DIST"
	    if [ $? -eq 0 ]; then
		echo -e "\e[u\e[60C\e[0;32mOK\e[0m"
	    else
		echo -e "\e[u\e[60C\e[0;31mNG\e[0m"
	    fi
            LINK=already
	fi
	shift
    done
}

mksymlink(){
    mkdir -p $1 &>/dev/null
    while read line; do
	case $line in \#*) continue;; esac # skip comment
	eval lns $1/$line
    done
}

echo; echo -e "\e[1;36mSearching fonts...\e[0m"

mksymlink $TRUETYPE <<EOF
### link name [space] directory or file to be searched
x11       /usr{,/local,/X11R6,/pkg}/lib/X11/fonts/truetype
X11       /usr{,/local,/X11R6,/pkg}/lib/X11/fonts/TrueType
X11TTF    /usr{,/local,/X11R6,/pkg}/lib/X11/fonts/TTF
truetype  /usr{,/local}/share/fonts/truetype
TrueType  /usr{,/local}/share/fonts/TrueType
TTF	  /usr{,/local}/share/fonts/TTF
ja        /usr{,/local}/share/fonts/ja/TrueType
japanese  /usr{,/local}/share/fonts/japanese/TrueType
default   /usr{,/local}/share/fonts/default/TTF
openwin   /usr/openwin/lib/locale/ja/X11/fonts/TT
QtPalmtop	/opt/QtPalmtop/lib/X11/fonts/TrueType
f8sazanami-min	/usr/share/fonts/sazanami-fonts-mincho
f8sazanami-goth	/usr/share/fonts/sazanami-fonts-gothic
f11sazanami	/usr/share/fonts/sazanami
vine5ipa	/usr/share/fonts/TrueType-ipafont
vine5vlgoth	/usr/share/fonts/TrueType-vlgothic
test /opt/fonts
EOF

mksymlink $OPENTYPE <<EOF
### link name [space] directory or file to be searched
QtPalmtop /opt/QtPalmtop/lib/X11/fonts/opentype
# for test (contains white space)
hoge "/tmp/hoge hoge"
EOF

if test -d /System/Library/Fonts; then # for Mac OS X
LC_ALL=ja_JP.UTF-8
cat <<EOF | /usr/bin/iconv -f UTF-8 -t UTF-8-MAC | mksymlink $OPENTYPE
HiraMinPro-W3.otf   {,/System}"/Library/Fonts/ヒラギノ明朝 Pro W3.otf"
HiraMinPro-W6.otf   {,/System}"/Library/Fonts/ヒラギノ明朝 Pro W6.otf"
HiraKakuPro-W3.otf  {,/System}"/Library/Fonts/ヒラギノ角ゴ Pro W3.otf"
HiraKakuPro-W6.otf  {,/System}"/Library/Fonts/ヒラギノ角ゴ Pro W6.otf"
HiraKakuStd-W8.otf  {,/System}"/Library/Fonts/ヒラギノ角ゴ Std W8.otf"
HiraMaruPro-W4.otf  {,/System}"/Library/Fonts/ヒラギノ丸ゴ Pro W4.otf"
HiraMinProN-W3.otf  {,/System}"/Library/Fonts/ヒラギノ明朝 ProN W3.otf"
HiraMinProN-W6.otf  {,/System}"/Library/Fonts/ヒラギノ明朝 ProN W6.otf"
HiraKakuProN-W3.otf {,/System}"/Library/Fonts/ヒラギノ角ゴ ProN W3.otf"
HiraKakuProN-W6.otf {,/System}"/Library/Fonts/ヒラギノ角ゴ ProN W6.otf"
HiraKakuStdN-W8.otf {,/System}"/Library/Fonts/ヒラギノ角ゴ StdN W8.otf"
HiraMaruProN-W4.otf {,/System}"/Library/Fonts/ヒラギノ丸ゴ ProN W4.otf"
EOF
fi

if test -d /cygdrive; then # for Cygwin
mksymlink $TRUETYPE <<EOF
windows  "`cygpath --mixed -W`/Fonts"
EOF
fi

(IFS=";"; NUM=1; for f in $EXTRA_TRUETYPE; do
    echo "extra$NUM $f"
    NUM=`expr $NUM + 1`
done) | mksymlink $TRUETYPE

(IFS=";"; NUM=1; for f in $EXTRA_OPENTYPE; do
    echo "extra$NUM $f"
    NUM=`expr $NUM + 1`
done) | mksymlink $OPENTYPE

