#!/bin/sh 
#Tag 0x00001030
# $Revision: 1.26 $

# winterm 
#
# Presents an abstract command line syntax for the user's own terminal emulator.
# Terminal emulators supported include wsh, xwsh, xterm, iwsh and exterm.
# The user can preset their preferred termulator (with preferred options) by
# setting the environment variable WINTERM.  If WINTERM is unset, winterm
# provides a built-in default.
#
# Syntax:  winterm [options] [[-c|-e] command]
#
# Where options may be:
#   -name         	sets the name used by the winterm
#   -title or -t or -T	sets the title used by the winterm	
#   -icontitle or -n 	sets the title used by the winterm	
#   -p			sets the position of the winterm
#   -s			sets the size of the winterm
#   -font or -f or -fn	sets the font used by the winterm 
#   -fg			sets the foreground (text) color in the winterm
#   -bg			sets the background (page) color in the winterm
#   -hold or -H         holds winterm open (where supported) after command exits
#   -iconic		starts the winterm up iconified (minimized)
#   -console or -C	starts the winterm up running as the system console
#   -display		sets the display device where the window should appear
#   -geom or -geometry	sets window position and size, using X geometry syntax
#   -xrm		set X resource values
#   -c or -e		delimits the command to be executed within the winterm
#

msgfile=uxsgidesktop

# get the name of the shell to be execed. It could be xwsh or iwsh
intlshell=`gettxt ${msgfile}:450 'xwsh -name winterm'`
WINTERM=${WINTERM:-"${intlshell}"}

cmd=

#	Scan WINTERM for any invokation of a legal termulator.
#	If one is found, use it to determine arg parsing case (winsh).
#	However, use the actual command found in WINTERM for exec'ing.
#	This approach allows the user to specify commands of the flavor
#		nextcolor; xwsh
#	as legal WINTERM preferences.
#
for i in $WINTERM
do
    if [ $winsh ]
    then
	cmdprefs=$cmdprefs" "$i
    else
	bn=`basename $i`
	case $bn in
	    xwsh | iwsh | wsh | xterm | exterm)	winsh=$bn
				cmd=$cmd" "$i
				;;
	    *)			cmd=$cmd" "$i
				;;
	esac
    fi
done

#	if no legal termulator is found within WINTERM,
#	ignore all WINTERM specs; use xwsh by default
#
if [ ${winsh:=unknown} = unknown ]
then
    winsh=xwsh
    cmd=xwsh
    cmdprefs=
fi

#	Parse command line options.
#	Can't use getopts, 'cuz it can't handle the
#	multi-character flag names used in X-land
#
optind=0
opt="skip_opt"

for optarg in "$@" ""
do
    optind=`expr $optind + 1`

    if [ $opt != "skip_opt" ]
    then
	case $opt in
	    -H | -hold)		hold=1
				;;
	    -name)		winname=$optarg
				optarg="skip_opt"
				;;
	    -T | -t | -title)   wintitle=\"$optarg\"
				optarg="skip_opt"
				;;
	    -icontitle | -n)    icontitle=\"$optarg\"
				optarg="skip_opt"
				;;
	    -f | -fn | -font)	fontname=$optarg
				optarg="skip_opt"
				;;
	    -p)			position=$optarg
				optarg="skip_opt"
				;;
	    -s)			size=$optarg
				optarg="skip_opt"
				;;
	    -bg)		bgcolor=$optarg
				optarg="skip_opt"
				;;
	    -fg)		fgcolor=$optarg
				optarg="skip_opt"
				;;
	    -display)		displaydevice=$optarg
				optarg="skip_opt"
				;;
	    -geom | -geometry)	geometry=$optarg
				optarg="skip_opt"
				;;
	    -xrm)		resourcestring=$optarg
				optarg="skip_opt"
				;;
	    -iconic)		starticonic=1
				;;
	    -noPreEditing)	noPreEditing=1
				;;
	    -C | -console)	runconsole=1
				# set environment variable so .profile can tell
				# if this is a console.
				CONSOLE=1; export CONSOLE
				;;
	    -c | -e)		break
				;;
	    -*)			usage=`gettxt ${msgfile}:451 "usage: winterm [options] [[-c|-e] <CMD>]  
 where available options include 
 -name <NAME>\t\t\tsets the name used by the winterm 
 -title | -t | -T <TITLE>\tsets the title used by the winterm 
 -icontitle | -n <ITITLE>\tsets the title of the icon used by the winterm 
 -p <X,Y>\t\t\tsets the position of the winterm 
 -s <COLS,LINES>\t\tsets the size of the winterm 
 -font | -f | -fn <FONT>\tsets the font used by the winterm 
 -fg <COLOR>\t\t\tsets the winterm's foreground (text) color 
 -bg <COLOR>\t\t\tsets the winterm's background (page) color 
 -hold | -H\t\t\tholds winterm open after command exits 
 -iconic\t\t\tstarts the winterm up iconified (minimized) 
 -console | -C\t\t\tstarts the winterm running as system console 
 -display <HOST:SCREEN>\t\tsets the display where the window will appear 
 -geometry <COLSxLINES+X+Y>\tsets window size and position 
 -xrm <RESOURCE>\t\tset X resource values"`
				printf "${usage}\n"
				exit 2
				;;
	    *)			optind=`expr $optind - 1`
				break
				;;
	esac
    fi
    opt=$optarg; 
done

if [ "$winsh" = "wsh" ]
then
    # note: only the original winterm/wsh flags are supported here,
    # to maintain compatibility with 3.2, 3.3 release

    cmd=$cmd" -n winterm "
    if [ $hold ] 
    then
        holdflag=" -H -n winout "
    fi
    if [ $fontname ]
    then
	fontflag=" -f "
    fi
    if [ "$position" -o "$size" ]
    then
    	#set initial position
    	if [ $position ]
    	then
		possizegeom=" -p "$position
    	fi
    	# set initial size
    	if [ $size ]
    	then
		possizegeom=$possizegeom" -s "$size
    	fi
    fi
    if [ $starticonic ]
    then
	iconicflag=" -Z7 "
    fi
    if [ $runconsole ]
    then
	consoleflag=" -Z2 "
    fi
    # these flags not supported by wsh compatibility
    bgcolor=
    fgcolor=
    icontitle=
    displaydevice=
    resourcestring=
    geometry=
    ####
    winflag=" -t "
    nameflag=" -n "
    cmdflag=" -c "
elif [ "$winsh" = "xwsh" ]
then
    cmd=$cmd" -name winterm "
    if [ $hold ] 
    then
        holdflag=" -hold -name winout "
    fi
    if [ $fontname ]
    then
	fontflag=" -fn "
    fi
    if [ "$position" -o "$size" ]
    then
    	#set initial position
    	if [ $position ] 
    	then
		position="+"`echo $position | sed -e 's/,/+/'`" " 
    	fi
    	# set initial size
    	if [ $size ] 
    	then
		size=`echo $size | sed -e 's/,/x/'`
    	fi
	possizegeom=" -geometry "$size$position
    fi
    if [ $bgcolor ]
    then
	bgcolor=" -bg "$bgcolor
    fi
    if [ $fgcolor ]
    then
	fgcolor=" -fg "$fgcolor
    fi
    if [ $icontitle ]
    then
	icontitleflag=" -icontitle "
    fi
    if [ $displaydevice ]
    then
	displaydevice=" -display "$displaydevice
    fi
    if [ $resourcestring ]
    then
	resourcestring=" -xrm "$resourcestring
    fi
    if [ $geometry ]
    then
	# note: if an explicit geometry flag is provided on the command line,
	# it will supercede preceding position and/or size arguments
	geometry=" -geometry "$geometry
    fi
    if [ $starticonic ]
    then
	iconicflag=" -iconic "
    fi
    if [ $runconsole ]
    then
	consoleflag=" -console "
    fi
    winflag=" -title "
    nameflag=" -name "
    cmdflag=" -e "
elif [ "$winsh" = "iwsh" ]
then
    cmd=$cmd" -name winterm "
    if [ $hold ] 
    then
        holdflag=" -hold -name winout "
    fi
    if [ $fontname ]
    then
	fontflag=" -fn "
    fi
    if [ "$position" -o "$size" ]
    then
    	#set initial position
    	if [ $position ] 
    	then
		position="+"`echo $position | sed -e 's/,/+/'`" " 
    	fi
    	# set initial size
    	if [ $size ] 
    	then
		size=`echo $size | sed -e 's/,/x/'`
    	fi
	possizegeom=" -geometry "$size$position
    fi
    if [ $bgcolor ]
    then
	bgcolor=" -bg "$bgcolor
    fi
    if [ $fgcolor ]
    then
	fgcolor=" -fg "$fgcolor
    fi
    if [ $icontitle ]
    then
	icontitleflag=" -icontitle "
    fi
    if [ $displaydevice ]
    then
	displaydevice=" -display "$displaydevice
    fi
    if [ $resourcestring ]
    then
	resourcestring=" -xrm "$resourcestring
    fi
    if [ $geometry ]
    then
	# note: if an explicit geometry flag is provided on the command line,
	# it will supercede preceding position and/or size arguments
	geometry=" -geometry "$geometry
    fi
    if [ $starticonic ]
    then
	iconicflag=" -iconic "
    fi
    if [ $noPreEditing ]
    then
	preflag=" -noPreEditing"
    fi
    if [ $runconsole ]
    then
	consoleflag=" -console "
    fi
    winflag=" -title "
    nameflag=" -name "
    cmdflag=" -e "
elif [ "$winsh" = "xterm" ]
then
    # no hold flag supported yet
    #set initial position
    if [ "$position" -o "$size" ]
    then
    	#set initial position
    	if [ $position ] 
    	then
		position="+"`echo $position | sed -e 's/,/+/'`" " 
    	fi
    	# set initial size
    	if [ $size ] 
    	then
		size=`echo $size | sed -e 's/,/x/'`
    	fi
	possizegeom=" -geometry "$size$position
    fi
    if [ $fontname ]
    then
	fontflag=" -fn "
    fi
    if [ $bgcolor ]
    then
	bgcolor=" -bg "$bgcolor
    fi
    if [ $fgcolor ]
    then
	fgcolor=" -fg "$fgcolor
    fi
    if [ $icontitle ]
    then
	icontitleflag=" -n "
    fi
    if [ $displaydevice ]
    then
	displaydevice=" -display "$displaydevice
    fi
    if [ $resourcestring ]
    then
	resourcestring=" -xrm "$resourcestring
    fi
    if [ $geometry ]
    then
	# note: if an explicit geometry flag is provided on the command line,
	# it will supercede preceding position and/or size arguments
	geometry=" -geometry "$geometry
    fi
    if [ $starticonic ]
    then
	iconicflag=" -iconic "
    fi
    if [ $runconsole ]
    then
	consoleflag=" -C "
    fi
    winflag=" -title "
    nameflag=" -name "
    cmdflag=" -e "
elif [ "$winsh" = "exterm" ]
then
    # no hold flag supported yet
    if [ "$position" -o "$size" ]
    then
    	#set initial position
    	if [ $position ] 
    	then
		position="+"`echo $position | sed -e 's/,/+/'`" " 
    	fi
    	# set initial size
    	if [ $size ] 
    	then
		size=`echo $size | sed -e 's/,/x/'`
    	fi
	possizegeom=" -geometry "$size$position
    fi
    if [ $fontname ]
    then
	fontflag=" -fn "
    fi
    if [ $bgcolor ]
    then
	bgcolor=" -bg "$bgcolor
    fi
    if [ $fgcolor ]
    then
	fgcolor=" -fg "$fgcolor
    fi
    if [ $icontitle ]
    then
	icontitleflag=" -n "
    fi
    if [ $displaydevice ]
    then
	displaydevice=" -display "$displaydevice
    fi
    if [ $resourcestring ]
    then
	resourcestring=" -xrm "$resourcestring
    fi
    if [ $geometry ]
    then
	# note: if an explicit geometry flag is provided on the command line,
	# it will supercede preceding position and/or size arguments
	geometry=" -geometry "$geometry
    fi
    if [ $starticonic ]
    then
	iconicflag=" -iconic "
    fi
    if [ $runconsole ]
    then
	consoleflag=" -C "
    fi
    winflag=" -title "
    nameflag=" -name "
    cmdflag=" -e "
fi

if [ $optind -gt $# ]
then
    # no arguments found after flags; no trailing command to be given
    cmdflag=
    cmditself=
    cmdargs=
    # if no title given, use no title
    if [ "$wintitle" = "" ]
    then
	    winflag=
    fi
else
    # capture arguments found after flags as the command to be given
    shift `expr $optind - 1`
    cmditself=$1
    cmdargs=$*

    shift 1
    # if no title given, use command string as title
    if [ "$wintitle" = "" ]
    then
	    winflag=
#	    wintitle=\"`basename $cmditself`" "$*\"
    fi
fi

# if no name given, add no name arguments
if [ "$winname" = "" ]
then
    nameflag=
fi

cmd=$cmd$cmdprefs$holdflag$fontflag$fontname$winflag$wintitle$nameflag$winname$possizegeom$geometry$bgcolor$fgcolor$icontitleflag$icontitle$displaydevice$resourcestring$iconicflag$preflag$consoleflag$cmdflag$cmdargs

# echo '->'$cmd'<-'

eval exec $cmd&
