#!/bin/sh
# this script will initialize settings in ~/.xmms/config
# so that they play well with Cyrillic tags and rusxmms;
# needs some XFree86-cyr_rfx-fonts-* package

# by Michael Shigorin <mike@altlinux.org> for xmms-1.2.8-alt3+

XMMSDIR="$HOME/.xmms"
CONFIG="$XMMSDIR/config"
BINARY="/usr/bin/xmms-bin"
#WRAPPED="/usr/bin/soundwrapper $BINARY"
WRAPPED="$BINARY"
RECODE="yes"

runxmms() {
	export XLIB_SKIP_ARGB_VISUALS=1
	# http://bugs.xmms.org/show_bug.cgi?id=1907
	[ -z "$XMMS_CYR_SOURCE" ] && exec $WRAPPED "$@"
}

[ $# = "1" ] && {
	case "$*" in
		-*) exec $BINARY "$@";;
	esac
}

[ -e "$CONFIG" ] && {
	/bin/grep -q "^output_plugin=" "$CONFIG" || {
		/usr/bin/subst "s,^\[xmms\],&\noutput_plugin=libOSS.so,g" "$CONFIG"
	}
	runxmms "$@"
}

unset BIGFONT SMALLFONT

FONTDIR_R="/usr/X11R6/lib/X11/fonts/koi8-r/75dpi"
BIGFONT_R="-rfx-helvetica-bold-r-normal-*-*-100-*-*-p-*-koi8-r"
SMALLFONT_R="-rfx-helvetica-medium-r-normal-*-*-80-*-*-p-*-koi8-r"

FONTDIR_UB="/usr/X11R6/lib/X11/fonts/koi8-u/75dpi"
BIGFONT_UB="-rfx_koi8_ub-helvetica-bold-r-normal-*-*-100-*-*-p-*-koi8-ub"
SMALLFONT_UB="-rfx_koi8_ub-helvetica-medium-r-normal-*-*-80-*-*-p-*-koi8-ub"

FONTDIR_1251="/usr/X11R6/lib/X11/fonts/cp1251/75dpi"
BIGFONT_1251="-rfx-helvetica-bold-r-normal-*-*-100-*-*-p-*-microsoft-cp1251"
SMALLFONT_1251="-rfx-helvetica-medium-r-normal-*-*-80-*-*-p-*-microsoft-cp1251"

BIGDEFFONT="-adobe-helvetica-bold-r-*-*-10-*"
SMALLDEFFONT="-adobe-helvetica-medium-r-*-*-8-*"

MAINWIN_USE_XFONT="TRUE"
AUTOCHARSET="1"

[ -z "$LC_CTYPE" ] && eval `locale 2>/dev/null | grep ^LC_CTYPE`

case "$LC_CTYPE" in
	be_BY*|ru_RU*|uk_UA*)
		[ -d "$FONTDIR_R" ] && {
			BIGFONT="$BIGFONT_R"
			SMALLFONT="$SMALLFONT_R"
		}
		[ -d "$FONTDIR_UB" ] && {
			BIGFONT="$BIGFONT_UB"
			SMALLFONT="$SMALLFONT_UB"
		}
		[ -d "$FONTDIR_1251" -a "$RECODE" != "yes" ] && {
			BIGFONT="$BIGFONT_1251"
			SMALLFONT="$SMALLFONT_1251"
		}
		[ -n "$XMMS_CYR_SOURCE" ] && {
			/bin/fgrep -q "$BIGDEFFONT" "$CONFIG" 2>/dev/null && \
			/bin/fgrep -q "$SMALLDEFFONT" "$CONFIG" 2>/dev/null && \
			[ -n "$BIGFONT" ] && BROKENFONTS="true"
		}
		;;
	*)
		runxmms "$@";;
esac
	
[ ! -e "$CONFIG" -a -n "$BIGFONT" ] && { 
	echo "$0: init $CONFIG for $LC_CTYPE"
	/bin/mkdir -p "$XMMSDIR" && \
	/bin/cat >> "$CONFIG" << __EOF__
[xmms]
playlist_font=$BIGFONT
mainwin_font=$SMALLFONT
shade_font=$SMALLFONT
mainwin_use_xfont=$MAINWIN_USE_XFONT
__EOF__
	[ "$RECODE" = "yes" ] && /bin/cat >> "$CONFIG" << __EOF__
autocharset=$AUTOCHARSET
autocharset_fs=$AUTOCHARSET
__EOF__
	XMMS_CYR_CREATED="true"
}

runxmms "$@"
