#!/bin/sh

withX=1

xf86config="/etc/X11/XF86Config"
fsconfig="/etc/X11/fs/config"
x11dirs=" /usr/lib/X11 /usr/X11R6 /usr/local/X11R6 "

ttscore=1000
while [ $# != 0 ] ; do
    case $1 in
	-tt | -TT | -truetype )
	    ttscore=1000
	    ;;
	-nott | -noTT | -notruetype )
	    ttscore=-1000
	    ;;
	-nox | -noX )
	    withX=0
	    ;;
	-cfg )
	    shift
	    if [ -f $1 ] ; then
		xf86config=$1
	    fi
	    ;;
	-fscfg )
	    shift
	    if [ -f $1 ] ; then
		fsconfig=$1
	    fi
	    ;;
	-xdir | -Xdir )
	    shift
	    if [ -d $1 ] ; then
		x11dirs=$1
	    fi
	    ;;
	*)
	    echo 'usage: make-qti18nrc [-tt|-nott] [-noX] [-cfg XF86Config] [-fscfg fs-config] [-xdir /usr/X11R6] [-help] > $HOME/.qti18nrc' 1>&2
	    exit 1
	    ;;
    esac
    shift
done

tmpfile=/tmp/mkqti18nrc.$$
touch $tmpfile
trap "rm -f $tmpfile" 0 1 2 3 4 5 6 7 8 9

JIS76=jisx0201.1976
JIS83=jisx0208.1983

if [ $withX = "1" ]; then
    xlsfonts > $tmpfile
fi
if [ $withX = "0" -o $? != "0" ] ; then
    if [ $withX = "1" ]; then
	echo "Please run `basename $0` in X11!" 1>&2
    fi
    if [ -f $xf86config ] ; then
	xfontsdir=`grep '^[ 	]*FontPath' $xf86config | \
	sed -e 's/^[^"]*"//' -e 's/".*//' -e 's/:unscaled//' `
	if echo "$xfontsdir" | grep -q ':' && [ -f $fsconfig ] ; then

	    xfontsdir="$xfontsdir ` \
		cat $fsconfig | \
		awk '
		/^catalogue[ 	]*=[ 	]*/ {
		    catalogue = $0
		    while (match($0, ",[ 	]*$")) {
			getline
			catalogue = catalogue $0
		    }
		    sub("^catalogue[ 	]*=[ 	]*", "", catalogue)
		    gsub(",[ 	]*", " ", catalogue);
		}
		END {
		    print catalogue
		}
		' \
	    `"

	fi
    else
	xfontsdir=""
	for d in $x11dirs ; do
	    if [ -d $d/lib/X11/fonts ] ; then
		xfontsdir="$xfontsdir `find $d -type d -print`"
	    fi
	done
    fi
    for d in $xfontsdir ; do
	if [ -f $d/fonts.dir ] ; then
	    tail +2 $d/fonts.dir | awk '{print $2}' >> $tmpfile
	fi
	if [ -f $d/fonts.alias ] ; then
	    grep -v '^!' $d/fonts.alias | awk '{print $1}' >> $tmpfile
	fi
    done
fi

mv $tmpfile $tmpfile.orig
sort $tmpfile.orig | uniq > $tmpfile
rm -f $tmpfile.orig

grep -q $JIS83 $tmpfile
if [ $? != "0" ] ; then
    echo "Couldn't find Japanese fonts!" 1>&2
    exit 2
fi

grep -e $JIS76 -e $JIS83 $tmpfile | \
tr 'A-Z' 'a-z' | \
awk -v jis76str=$JIS76 -v jis83str=$JIS83 -v TTSCORE=$ttscore '

BEGIN {
    SCORE["alias"] = 20
    SCORE["dynalab"] = 10
    SCORE["ryobi"] = 10

    FACE_SCORE["mincho"] = 200
    FACE_SCORE["gothic"] = 100

    FS = "-"
    OFS = FS
}

{
    point = 1
    tt = 0

    $4 = "*"
    $5 = "*"

    for (i in SCORE) {
	if (match($2, i)) {
	    point += SCORE[i]
	}
    }
    for (i in FACE_SCORE) {
	if (match($3, i)) {
	    point += FACE_SCORE[i]
	}
    }
    if ($8 == 0 && $9 == 0) {
	point += TTSCORE
	tt = 1
    } else {
	$8 = "*"
	$9 = "*"
	$13 = "*"
	if (match($14, jis76str)) {
	    bpoints76[$0] += point
	} else {
	    bpoints83[$0] += point
	}
    }
    if (match($14, jis76str)) {
	if (point > maxjis76+0) {
	    jis76 = $0
	    maxjis76 = point
	    jis76tt = tt
	}
    } else {
	if (point > maxjis83+0) {
	    jis83 = $0
	    maxjis83 = point
	    jis83tt = tt
	}
    }
    # print point ": " $0
}

END {
    if (!jis76tt && !jis83tt) {
	for (i in bpoints76) {
	    fnt83 = i
	    sub(jis76str, jis83str, fnt83)
	    if (bpoints83[fnt83]) {
		bpoints76[i] += bpoints83[fnt83]
		bpoints83[fnt83] = bpoints76[i]
	    } else {
		bpoints76[i] = 0
	    }
	}
	for (i in bpoints83) {
	    fnt76 = i
	    sub(jis83str, jis76str, fnt76)
	    if (bpoints76[fnt76] == 0) {
		bpoints83[i] = 0
	    }
	}
    }
    if (!jis76tt) {
	max = 0
	for (i in bpoints76) {
	    if (max < bpoints76[i]+0) {
		max = bpoints76[i]
		maxfonts = i
	    }
	}
	jis76 = maxfonts
    }
    if (!jis83tt) {
	max = 0
	for (i in bpoints83) {
	    if (max < bpoints83[i]+0) {
		max = bpoints83[i]
		maxfonts = i
	    }
	}
	jis83 = maxfonts
	sub(jis76str, jis83str, jis83)
    }
    print "*\t" jis76 ", \\"
    print "\t" jis83
}

'




