#!/bin/sh
# this script will initialize settings in ~/.xmms/config
# so that they play well with cyrillic tags and rusxmms;
# needs a fonts-bitmap-cyr_rfx-* package for 8-bit fonts
# and recent fonts-bitmap-75dpi for unicode fonts

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

XMMSDIR="$HOME/.xmms"
CONFIG="$XMMSDIR/config"
BINARY="xmms-bin"
# rusxmms patch included?
RECODE="yes"
# default output plugin: shipped within main package
OUTPUT=libOSS.so

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

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

[ -e "$CONFIG" ] && {
	grep -q "^output_plugin=" "$CONFIG" || {
		[ -f /usr/lib64/xmms/Output/libALSA.so ] && OUTPUT=libALSA.so
		subst "s,^\[xmms\],&\noutput_plugin=$OUTPUT," "$CONFIG"
	}
	runxmms "$@"
}

unset BIGFONT SMALLFONT USE_FONTSETS

FONTDIR_R_NEW="/usr/share/fonts/bitmap/cyr_rfx-koi8-u/75dpi"
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_NEW="/usr/share/fonts/bitmap/cyr_rfx-koi8-u/75dpi"
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_NEW="/usr/share/fonts/bitmap/cyr_rfx-cp1251/75dpi"
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"

FONTDIR_UTF8="/usr/share/fonts/bitmap/misc/"
BIGFONT_UTF8="-misc-dejavu sans condensed-bold-r-semicondensed-*-11-*-*-*-p-*-iso10646-1"
SMALLFONT_UTF8="-misc-dejavu sans condensed-medium-r-semicondensed-*-9-*-*-*-p-*-iso10646-1"

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
	*.UTF-8)
		# might have to check for older path on ALC3.0
		[ -d "$FONTDIR_UTF8" ] && {
			BIGFONT="$BIGFONT_UTF8"
			SMALLFONT="$SMALLFONT_UTF8"
			USE_FONTSETS="TRUE"
		}
		;;
	*.CP1251)
		[ -d "$FONTDIR_1251_NEW" -o -d "$FONTDIR_1251" ] &&
			BIGFONT="$BIGFONT_1251"
			SMALLFONT="$SMALLFONT_1251"
			USE_FONTSETS="FALSE"
		;;
	*.KOI8-*)
		[ -d "$FONTDIR_UB_NEW" -o -d "$FONTDIR_UB" ] && {
			BIGFONT="$BIGFONT_UB"
			SMALLFONT="$SMALLFONT_UB"
			USE_FONTSETS="FALSE"
		} || {
			[ -d "$FONTDIR_R_NEW" -o -d "$FONTDIR_R" ] && {
				BIGFONT="$BIGFONT_R"
				SMALLFONT="$SMALLFONT_R"
				USE_FONTSETS="FALSE"
			}
		}
		;;
	*)
		runxmms "$@";;
esac

[ ! -e "$CONFIG" -a -n "$BIGFONT" ] && { 
	echo "$0: init $CONFIG for $LC_CTYPE"
	mkdir -p "$XMMSDIR" && \
	cat >> "$CONFIG" << __EOF__
[xmms]
playlist_font=$BIGFONT
mainwin_font=$SMALLFONT
shade_font=$SMALLFONT
mainwin_use_xfont=$MAINWIN_USE_XFONT
__EOF__

	# rusxmms
	[ "$RECODE" = "yes" ] && cat >> "$CONFIG" << __EOF__
autocharset=$AUTOCHARSET
autocharset_fs=$AUTOCHARSET
__EOF__

	# unicode
	[ -n "$USE_FONTSETS" ] && echo "use_fontsets=$USE_FONTSETS" >> "$CONFIG"

	# bonus tracks ;)
	cat >> "$CONFIG" << __EOF__
vis_type=0
vis_refresh_rate=2
analyzer_peaks=FALSE
analyzer_falloff=1
analyzer_type=0
analyzer_mode=1
__EOF__

	# yes, repeat ;)
	[ -f /usr/lib64/xmms/Output/libALSA.so ] && OUTPUT=libALSA.so
	echo -e "output_plugin=$OUTPUT\n" >> "$CONFIG"

	[ -f /usr/lib64/xmms/Output/libcrossfade.so ] \
	&& grep -q "^output_plugin=libcrossfade.so" "$CONFIG" \
	|| {
		subst "s,^\(output_plugin\)=$OUTPUT,\1=libcrossfade.so," "$CONFIG"
		cat >> "$CONFIG" << __EOF__
[Crossfade]
output_method=1
output_plugin=$OUTPUT
__EOF__
	}

	XMMS_CYR_CREATED="true"

}

runxmms "$@"
