#! /bin/sh

# invoke terminal with Windows look-and-feel (color settings)
# choose one of
#	mintty (stand-alone) in UTF-8 mode
#	rxvt (stand-alone mode, old non-Unicode rxvt)
# optionally invoke mined in it
#	- if script name ends with "mined"

#############################################################################
# select invocation mode
case "$0" in
*ed)	prog="-e mined";;
*)	prog=;;
esac

#############################################################################
# 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
	confattr="Codepage|ForegroundColour|BackgroundColour|Scrollbar|WindowShortcuts|EditShortcuts|BoldAsBright"
	(mincolors
	 #echo FontHeight=...
	 echo Codepage=UTF-8
	 echo Scrollbar=0
	 echo WindowShortcuts=0
	 echo EditShortcuts=0
	 echo BoldAsBright=0
	 egrep -v -e "^($confattr)=" \
	 	"$HOME/.minttyrc"
	) > "$HOME/.minttyrc.win"
	(mintty -c "$HOME/.minttyrc.win" $prog "$@"
	 # adopt configuration changes for parameters not set here
	 egrep -e "^($confattr)=" \
	 	"$HOME/.minttyrc" > "$HOME/.minttyrc.new"
	 egrep -v -e "^($confattr)=" \
	 	"$HOME/.minttyrc.win" >> "$HOME/.minttyrc.new"
	 /bin/mv "$HOME/.minttyrc.new" "$HOME/.minttyrc"
	) &
else
  if rxvt -help 2>&1 | grep -e "-.*uas" > /dev/null
  then	rxvt="rxvt -uas"
  else	rxvt=rxvt
  fi
  unset DISPLAY

  case "$WINDIR" in
  "")	# not cygwin
	$rxvt -tn rxvt +sb $prog "$@" &
	;;
  *)	setcolors
	case "$DISPLAY" in
	"")	setfont
		$rxvt -tn rxvt +sb -fn "$font" $colors $prog "$@" &
		;;
	*)	$rxvt -tn rxvt +sb $colors $prog "$@" &
		;;
	esac
	;;
  esac
fi

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