#!/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>

VERSION="Version 0.5  1998/12/07"

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

help_msg() {
    echo -e "\n"\
	"$PROGRAM - font/language setting utility for Window Maker\n"\
	"\n"\
	"Usage:                              $VERSION\n"\
	"        $PROGRAM [--nodef] [--auto] <font_family>" >/dev/stderr
    cat >/dev/stderr <<EOF
-----------------------------------------------------------------------
[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)
 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
*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";;
    *)
	if echo $i |grep "^--locale=" &>/dev/null; then
	    new_locale=$(echo $i |sed -e "s|--locale=||" -)
	elif [ -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"
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"

#
# 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"
    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"
NodefUnable="japanese korean korean2"
Supported="$NodefAble $NodefUnable"

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

defaultMultiByteText="AUTO"
 latin1MultiByteText="NO"
 latin2MultiByteText="YES"
  greekMultiByteText="YES"
russianMultiByteText="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
	    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
    echo -e "\n"\
	"Sorry, font coding $family or locale $locale not recognized.\n"\
	"Type $PROGRAM with no arguments for list of supported font codings...\n"\
	"" >/dev/stderr
    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
	    echo -e "\n"\
		"Sorry, --nodef argument is not allowed for these font codings:\n"\
		"    $NodefUnable\n"\
		"Run $PROGRAM without --nodef argument to set default fonts for your\n"\
		"font coding.\n"\
		"" >/dev/stderr
	    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
    echo -e "\n"\
	"You used --nodef command line argument. You should note, that this\n"\
	"works with these font codings only:\n"\
	"    $NodefAble\n"\
	"You should not use it to change fonts from other font codings than\n"\
	"those listed above. If your WindowMaker does not appear to find fonts,\n"\
	"you should run $PROGRAM once again without --nodef argument." >/dev/stderr
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"
    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 $j=$($WDREAD $i $j | sed -e "s/\(^-.*-\).*-.*$/\1${!tmp1}/g")
		if test -z "${!j}"; then		# failed, load default
		    tmp2="Default$j"
		    eval $j=${!tmp2}
		fi
	    fi
	fi
	tmp1="$j=\"${!j}\""			# cosmetic length workaround
	if (( ${#tmp1} > 80 )); then
	    tmp1=$(echo "${tmp1:0:76}...\"")
	fi
	echo "$tmp1"
	$WDWRITE $i $j \"${!j}\"
    done
done
echo

