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

# If the effective locale variable (defined and "non-null" environment 
# variable in the order of precedence LC_ALL, LC_CTYPE, LANG) 
# already indicates UTF-8, nothing needs to be modified.
# Otherwise the first of these variables that is defined and "non-null" 
# needs to be manipulated to have the suffix ".UTF-8".
case "${LC_ALL:-${LC_CTYPE:-$LANG}}" in
*.UTF-8 | *.utf8)	true;;
*)	def=en_US	# .UTF-8 locale most likely to be installed at least
	case "${LC_ALL:-null}" in
	"$LC_ALL")	LC_ALL=`echo ${LC_ALL:-$def} | sed -e "s,[.@].*,,"`.UTF-8
			export LC_ALL;;
	*)	case "${LC_CTYPE:-null}" in
		"$LC_CTYPE")	LC_CTYPE=`echo ${LC_CTYPE:-$def} | sed -e "s,[.@].*,,"`.UTF-8
				export LC_CTYPE;;
		*)	case "${LANG:-null}" in
			"$LANG")	LANG=`echo ${LANG:-$def} | sed -e "s,[.@].*,,"`.UTF-8
					export LANG;;
			*)	LC_CTYPE=$def.UTF-8
				export LC_CTYPE;;
			esac
		esac
	esac
esac
#echo LC_ALL=${LC_ALL-<undefined>}, LC_CTYPE=${LC_CTYPE-<undefined>}, LANG=${LANG-<undefined>}

#############################################################################
# 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`"
}

mincolors () {
	echo ForegroundColour=`color WindowText | sed -e 's/ /,/g'`
	echo BackgroundColour=`color Window | sed -e 's/ /,/g'`
}

minfontsize () {
	echo FontHeight=`fontsize`
}

# 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

if type mintty > /dev/null 2>&1
then
	(mincolors
	 #echo FontHeight=...
	 echo Codepage=UTF-8
	 egrep -v -e "^(Codepage|ForegroundColour|BackgroundColour)=" \
	 	"$HOME/.minttyrc"
	) > "$HOME/.minttyrc.win"
	mintty -c "$HOME/.minttyrc.win" -e mined "$@" &
else
  case "$WINDIR" in
  "")	# not cygwin
	rxvt -tn rxvt +sb -e mined "$@" &
	;;
  *)	setcolors
	case "$DISPLAY" in
	"")	setfont
		rxvt -tn rxvt +sb -fn "$font" $colors -e mined "$@" &
		;;
	*)	rxvt -tn rxvt +sb $colors -e mined "$@" &
		;;
	esac
	;;
  esac
fi

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