#! /bin/sh

# invoke mined in a newly started rxvt, 
# especially on Windows, using the stand-alone (non-X) mode of rxvt

#############################################################################
# set font
size="<SIZE>"	# to be replaced later (if used)
font="Lucida Console"
font="Lucida Console-$size"
#font=fixedsys


#############################################################################
# try to configure UTF-8 for rxvt;
# this does not work with the currently available rxvt port to Windows, though
LC_ALL=en_US.UTF-8
export LC_ALL


#############################################################################
# acquire Windows system color and font size settings

# acquire Windows system color settings
color () {
	regtool -v get "/HKEY_CURRENT_USER/Control Panel/Colors/$1" 2> /dev/null ||
	reg query 'HKEY_CURRENT_USER\Control Panel\Colors' /v $1 |
	sed -e "s,.*	,," -e t -e d
}

rgb () {
	printf "#%02X%02X%02X" `color $1`
}

setcolors () {
	colors="-bg `rgb Window` -fg `rgb WindowText`"
}


# acquire Windows font size settings
# Configured font size from the registry (e.g. "MenuHeight") 
# cannot be used as especially Lucida Console scales much larger 
# than other fonts and sizes are not comparable among different fonts.
# So use a fixed base size and scale it by configured screen resolution.
metrics () {
	regtool -v get "/HKEY_CURRENT_USER/Control Panel/Desktop/WindowMetrics/$1" 2> /dev/null ||
	printf "%d" `reg query 'HKEY_CURRENT_USER\Control Panel\Desktop\WindowMetrics' /v $1 |
			sed -e "s,.*	,," -e t -e d`
}

fontsize () {
#	expr 0 - `metrics MenuHeight` / 16
	expr 14 \* `metrics AppliedDPI` / 100
}

setfont () {
	fontsize=`fontsize`
	font=`echo "$font" | sed -e "s/\\\$size/$size/" -e "s/$size/$fontsize/"`
}


#############################################################################
# start rxvt with appropriate parameters
#case `uname` in
#CYG*)	;;
#esac

case "$WINDIR" in
"")	rxvt -tn rxvt +sb -e mined "$@" &
	;;
*)	unset DISPLAY
	setcolors
	setfont
	rxvt -tn rxvt +sb -fn "$font" $colors -e mined "$@" &
	;;
esac


#############################################################################
# end
