#!/bin/sh
#
# language/font setting script for Window Maker
#
# by MANOME Tomonori <manome@itlb.te.noda.sut.ac.jp>
# (rewriten slightly by judas@hell on Jan 27 2001
#  -argument --nodef tries to change only font coding (limited!)
#  -much easier to add new font codings
#  -much harder to read code...
#  -argument --auto gets locale by itself, but does not override if some
#       coding is also given
#  -argument --locale=?? takes locale code (ja, sk, ...)
# )
#
# [Special Thanks To]
# Korean fontset info from: 
#     Byeong-Chan, Kim <redhands@linux.sarang.net> 
#     Chae-yong Chong <cychong@metro.telecom.samsung.co.kr>
# Latin2 fontset info from: 
#     Piotr Dembinski <pdemb@aurora.put.poznan.pl>
#     Toni Bilic <root@toni.hr.tel.hr>
# Greek fontset info from: 
#     Nikolaos Papagrigoriou <papanikos@usa.net>
# Quote bug fix info from:
#     Luke Kendall <luke@research.canon.com.au>
# Bug fix info for Korean font section from:
#     CHOI Junho <junker@jazz.snu.ac.kr>
# Ukrainian fontset info from: 
#     Bohdan Vlasyuk <bohdan@vstu.edu.ua>

VERSION="Version 0.6  2002/01/12"

WDWRITE="wdwrite"
WDREAD="wdread"
PROGRAM=`basename $0`

help_msg() {
    cat >/dev/stderr <<EOF
$PROGRAM - font/language setting utility for Window Maker

Usage:                              $VERSION
        $PROGRAM [--nodef] [--auto] <font_family>
-----------------------------------------------------------------------
[font_fam]  [Fontset]                    [Font Information]
 default    helvetica                    
 latin1     helvetica(iso8859-1)
 latin2     helvetica(iso8859-2)          See *Note1.
 greek      helvetica(iso8859-7)          See *Note1.
 japanese   helvetica/-*-fixed(jisx02XX)  Included in X11R6/XFree86("fnon" pkg)
 korean     helvetica/-*-kodig(ksc5601)   See *Note1.
 korean2    helvetica/daewoo(ksc5601)     Included in X11R6/XFree86("fnon" pkg)
 russian    helvetica(koi8-r)             Included in X11R6/XFree86("fcyr" pkg)
 ukrainian  helvetica(koi8-u)             See *Note1.
 turkish    unknown                       See *Note2.
-----------------------------------------------------------------------
Latin1 Languages: Danish/Dutch/English/Finnish/French/Galician/German/
                  Italian/Norwegian/Portuguese/Spanish/Swedish/...
Latin2 Languages: Croatian/Czech/Polish/Romanian/Slovenian/...

*Note1: Not included in X11R6/XFree86. You can download them from ...
 Latin2 X fonts packages:    http://sizif.mf.uni-lj.si/linux/cee/iso8859-2.html
 Greek X fonts packages:     ftp://argeas.argos.hol.gr/pub/unix/linux/GREEK/fonts/
                             ftp://ftp.ntua.gr/pub/fonts/X11/
 Korean X fonts packages:    ftp://linux.sarang.net  (Linux RPM)
                             ftp://ftp.kaist.ac.kr/pub/hangul/fonts
 Ukrainian X fonts packages: http://www.inp.nsk.su/~bolkhov/files/fonts/cyr-rfx/
*Note2: please let me know appropriate font setting for WMGLOBAL/WindowMaker,
        and the fonts' URL to download. mail-to: manome@itlb.te.noda.sut.ac.jp
EOF
    exit
}

nodef=""
auto=""
family=""
for i in $*; do
    case $i in
    "--nodef")
	nodef="YES";;
    "--auto")
	auto="YES";;
    --locale=*)
        new_locale=`echo $i |sed -e "s|--locale=||" -`;;
    *)
	if [ -z $family ]; then
	    family=$i
	else
	    help_msg
	fi;;
    esac
done
if [ -z "$family" ] && [ -z "$new_locale" ] && [ -z "$auto" ]; then
    help_msg
fi

################################################################################
# You should make changes only in this area
# (or it is not what I wanted it to be...)
#

#
# For --auto and --locale= switch...
#
all_codings="latin1 latin2 greek japanese korean2 russian ukrainian"
latin1="ca da de gl nl fi fr de is it no pt es sv se"
latin2="hr cz cs hu pl ro sk sl"
greek="el"
japanese="ja"
korean2="ko"
russian="ru bg"
ukrainian="uk" # note that it's really uk, not ua.

#
# These are lists of all domains and keys we're gonna change...
#
DomainNames="WMGLOBAL WindowMaker"
DomainWMGLOBAL="SystemFont BoldSystemFont MultiByteText"
DomainWindowMaker="WindowTitleFont MenuTitleFont MenuTextFont IconTitleFont ClipTitleFont DisplayFont"

for i in $DomainNames; do
    tmp="Domain$i"
    eval AllKeys="\"$AllKeys \$${tmp}\""
done

#
# Items that don't need to have coding string appended ( = not fonts).
#
NotFont="MultiByteText"

#
# Elements of $NodefUnable will always have fonts set to defaults, while those
# of $NonedefAble will have changed only coding part of font name (if --nodef
# command line argument is used).
#
NodefAble="default latin1 latin2 greek russian ukrainian"
NodefUnable="japanese korean korean2"
Supported="$NodefAble $NodefUnable"

defaultCoding="*-*"
 latin1Coding="iso8859-1"
 latin2Coding="iso8859-2"
  greekCoding="iso8859-7"
russianCoding="koi8-r"
ukrainianCoding="koi8-u"

defaultMultiByteText="AUTO"
 latin1MultiByteText="NO"
 latin2MultiByteText="YES"
  greekMultiByteText="YES"
russianMultiByteText="YES"
ukrainianMultiByteText="YES"


#
# Default WindowMaker fonts for NodefAble font families (part telling us
# the font coding will be added automaticly from *Coding).
#
DefaultSystemFont="-*-helvetica-medium-r-normal-*-%d-*-*-*-*-*-"
DefaultBoldSystemFont="-*-helvetica-bold-r-normal-*-%d-*-*-*-*-*-"
DefaultWindowTitleFont="-*-helvetica-bold-r-normal-*-12-*-*-*-*-*-"
DefaultMenuTitleFont="-*-helvetica-bold-r-normal-*-12-*-*-*-*-*-"
DefaultMenuTextFont="-*-helvetica-medium-r-normal-*-12-*-*-*-*-*-"
DefaultIconTitleFont="-*-helvetica-medium-r-normal-*-8-*-*-*-*-*-"
DefaultClipTitleFont="-*-helvetica-bold-r-normal-*-10-*-*-*-*-*-"
DefaultDisplayFont="-*-helvetica-medium-r-normal-*-12-*-*-*-*-*-"
DefaultMultiByteText="AUTO"

#
# NodefUnable have to give full font descriptions...
#
japaneseSystemFont="-*-helvetica-medium-r-normal-*-%d-*-*-*-*-*-*-*,-*-*-medium-r-normal-*-%d-*,-*-*-medium-r-normal-*-*-*"
japaneseBoldSystemFont="-*-helvetica-bold-r-normal-*-%d-*-*-*-*-*-*-*,-*-*-medium-r-normal-*-%d-*,-*-*-medium-r-normal-*-*-*"
japaneseWindowTitleFont="-*-helvetica-bold-r-normal-*-12-*-*-*-*-*-*-*,-*-*-medium-r-normal-*-14-*,-*-*-medium-r-normal-*-*-*"
japaneseMenuTitleFont="-*-helvetica-bold-r-normal-*-12-*-*-*-*-*-*-*,-*-*-medium-r-normal-*-14-*,-*-*-medium-r-normal-*-*-*"
japaneseMenuTextFont="-*-helvetica-medium-r-normal-*-12-*-*-*-*-*-*-*,-*-*-medium-r-normal-*-14-*,-*-*-medium-r-normal-*-*-*"
japaneseIconTitleFont="-*-helvetica-medium-r-normal-*-8-*-*-*-*-*-*-*,-*-*-medium-r-normal-*-12-*,-*-*-medium-r-normal-*-*-*"
japaneseClipTitleFont="-*-helvetica-bold-r-normal-*-10-*-*-*-*-*-*-*,-*-*-medium-r-normal-*-12-*,-*-*-medium-r-normal-*-*-*"
japaneseDisplayFont="-*-helvetica-medium-r-normal-*-12-*-*-*-*-*-*-*,-*-*-medium-r-normal-*-14-*,-*-*-medium-r-normal-*-*-*"
japaneseMultiByteText="YES"

koreanSystemFont="-*-helvetica-medium-r-normal-*-%d-*-*-*-*-*-*-*,-*-kodig-medium-r-normal--%d-*-*-*-*-*-*-*,*-*-medium-r-normal-*-%d-*-*-*-*-*-*-*,*-*-*-*-*--%d-*-*-*-*-*-*-*,*"
koreanBoldSystemFont="-*-helvetica-bold-r-normal-*-%d-*-*-*-*-*-*-*,-*-kodig-bold-r-normal--%d-*-*-*-*-*-*-*,-*-kodig-medium-r-normal--%d-*-*-*-*-*-*-*,*-*-bold-r-normal-*-%d-*-*-*-*-*-*-*,*-*-*-*-*--%d-*-*-*-*-*-*-*,*"
koreanWindowTitleFont="-*-helvetica-bold-r-normal-*-12-*-*-*-*-*-*-*,-*-kodig-medium-r-normal--12-*-*-*-*-*-*-*,-*-*-medium-r-normal--12-*-*-*-*-*-*-*,-*-*-medium-r-normal--*-*-*-*-*-*-*-*,-*-*-*-r-normal--*-*-*-*-*-*-*-*,-*-*-*-*-*--*-*-*-*-*-*-*-*,*"
koreanMenuTitleFont="-*-helvetica-bold-r-normal-*-12-*-*-*-*-*-*-*,-*-kodig-medium-r-normal--14-*-*-*-*-*-*-*,-*-*-medium-r-normal--12-*-*-*-*-*-*-*,*-*-medium-r-normal--*-*-*-*-*-*-*-*,*-*-*-r-normal--*-*-*-*-*-*-*-*,*"
koreanMenuTextFont="-*-helvetica-medium-r-normal-*-12-*-*-*-*-*-*-*,-*-kodig-medium-r-normal--14-*-*-*-*-*-*-*,*-*-medium-r-normal--12-*-*-*-*-*-*-*,*-*-medium-r-normal--*-*-*-*-*-*-*-*,*-*-*-r-normal--*-*-*-*-*-*-*-*,*"
koreanIconTitleFont="-*-helvetica-medium-r-normal-*-8-*-*-*-*-*-*-*,-*-kodig-medium-r-normal--10-*-*-*-*-*-*-*,*-*-medium-r-normal--10-*-*-*-*-*-*-*,*-*-medium-r-normal--*-*-*-*-*-*-*-*,*-*-*-r-normal--*-*-*-*-*-*-*-*,*"
koreanClipTitleFont="-*-helvetica-bold-r-normal-*-10-*-*-*-*-*-*-*,-*-kodig-medium-r-normal--10-*-*-*-*-*-*-*,*-*-medium-r-normal--10-*-*-*-*-*-*-*,*-*-medium-r-normal--*-*-*-*-*-*-*-*,*-*-*-r-normal--*-*-*-*-*-*-*-*,*"
koreanDisplayFont="-*-helvetica-medium-r-normal-*-12-*-*-*-*-*-*-*,-*-kodig-medium-r-normal--12-*-*-*-*-*-*-*,*-*-medium-r-normal--12-*-*-*-*-*-*-*,*-*-medium-r-normal--*-*-*-*-*-*-*-*,*-*-*-r-normal--*-*-*-*-*-*-*-*,*"
koreanMultiByteText="YES"

korean2SystemFont="-*-helvetica-medium-r-normal-*-%d-*-*-*-*-*-*-*,-*-medium-r-normal-*-%d-*,-*-medium-r-normal-*"
korean2BoldSystemFont="-*-helvetica-bold-r-normal-*-%d-*-*-*-*-*-*-*,-*-medium-r-normal-*-%d-*,-*-medium-r-normal-*"
korean2WindowTitleFont="7x14,-*-gothic-medium-r-normal-*-16-*-*-*-*-*-ksc5601.1987-*"
korean2MenuTitleFont="7x14,-*-gothic-medium-r-normal-*-16-*-*-*-*-*-ksc5601.1987-*"
korean2MenuTextFont="7x14,-*-gothic-medium-r-normal-*-16-*-*-*-*-*-ksc5601.1987-*"
korean2IconTitleFont="-*-helvetica-medium-r-normal-*-10-*-*-*-*-*-*-*,-*-gothic-medium-r-normal-*-12-*-*-*-*-*-ksc5601.1987-*"
korean2ClipTitleFont="-*-helvetica-bold-r-normal-*-10-*-*-*-*-*-*-*,-*-gothic-medium-r-normal-*-12-*-*-*-*-*-ksc5601.1987-*"
korean2DisplayFont="-*-helvetica-medium-r-normal-*-12-*-*-*-*-*-*-*,-*-gothic-medium-r-normal-*-16-*-*-*-*-*-ksc5601.1987-*"
korean2MultiByteText="YES"

#
# End of area meant to hold everything you could need to modify...
################################################################################

#
# Get current locale automaticaly (--auto).
#
if [ -z "$family" ]; then
    if [ -n "$new_locale" ]; then
	locale="$new_locale"
    else
	locale=$LANG
	if [ -z "$locale" ]; then
	    locale=$LC_ALL
	fi
    fi

    if [ -z "$locale" ]; then
	family="default"
    else
	for i in $all_codings; do
	    eval _i="\$$i"
	    for j in ${_i}; do
		if [ "$j" = "${locale%_*}" ]; then
		    family="$i"
		    break 2
		fi
	    done
	done
    fi
fi

#
# Do we recognize passed coding?
#
is_supported=""
for i in $Supported; do
    if test "$family" = "$i"; then
	is_supported="YES"
	break
    fi
done
if test -z "$is_supported"; then			# unknown coding
    cat >/dev/stderr <<EOT
Sorry, font coding $family or locale $locale not recognized.
Type $PROGRAM with no arguments for list of supported font codings...
EOT
    exit 1
fi

#
# Nodef is supported only for elements of $NodefAble.
#
is_nodef_unable=""
for i in $NodefUnable; do
    if test "$family" = "$i"; then
	if test -n "$nodef"; then	# --nodef with one from $NodefUnable
	    cat >/dev/stderr <<-EOT
Sorry, --nodef argument is not allowed for these font codings:
    $NodefUnable
Run $PROGRAM without --nodef argument to set default fonts for your
font coding.
EOT
	    exit 1
	else
	    is_nodef_unable="YES"
	    break
	fi
    fi
done

#
# The real work...
#
if test -z "$nodef"; then
    for i in $AllKeys; do
	if test -n "$is_nodef_unable"; then		# japanese,... defaults
	    tmp="$family$i"
	    eval $i="\$${tmp}"
	else						# rest, load defaults
	    not_font=""
	    for j in $NotFont; do
		if test "$i" = "$j"; then
		    not_font="YES"
		    break
		fi
	    done
	    if test -n "$not_font"; then  # don't append coding (not default!)
		tmp="${family}$i"
		eval $i="\$${tmp}"
	    else					# append coding
		tmp="Default$i"
		tmp1="${family}Coding"
		eval $i="\$${tmp}\$${tmp1}"
	    fi
	fi
    done
else
    cat >/dev/stderr <<EOT
You used --nodef command line argument. You should note, that this
works with these font codings only:
    $NodefAble
You should not use it to change fonts from other font codings than
those listed above. If your WindowMaker does not appear to find fonts,
you should run $PROGRAM once again without --nodef argument.
EOT
fi

#
# Ok, in the following, `i' will be the domain file name,
# `j' is the actual key in that file we try to change...
#
for i in $DomainNames; do
    echo
    echo "Setting up $i domain file..."
    tmp="Domain$i"
    eval _tmp="\$${tmp}"
    for j in ${_tmp}; do
	if test -n "$nodef"; then		# --nodef, change coding only
	    not_font=""
	    for k in $NotFont; do
		if test "$k" = "$j"; then
		    not_font="YES"
		    break;
		fi
	    done
	    if test -n "$not_font"; then		# don't append coding
		tmp1="${family}$j"
		eval $j="\$${tmp1}"
	    else					# append coding
#TODO:make this funny sed script a lot smarter to let us make changes in font
#     lists, etc... (then we can enable --nodef for NodefUnable codings)
#TODO:better (or at least any) check, whether wmaker is installed...
		tmp1="${family}Coding"
		eval _tmp1="\$${tmp1}"
		eval $j=`$WDREAD $i $j | sed -e "s/\(^-.*-\).*-.*$/\1${_tmp1}/g"`
		eval _j="\$$j"
		if test -z "${_j}"; then		# failed, load default
		    tmp2="Default$j"
		    eval $j="\$${tmp2}"
		fi
	    fi
	fi
	tmp1="$j=\"`eval echo '${'$j'}'`\""			# cosmetic length workaround
        echo '<<<<'
	if [ ${#tmp1} -lt 80 ]; then
	    tmp1=`echo $tmp1 | sed -e 's/^\(.\{1,76\}\).*/\1/'`
	fi
	eval _j="\$$j"
	echo $j="${_j}"
	$WDWRITE $i $j \"${_j}\"
    done
done
echo

