#!/bin/sh
#set -v

myname=$0					# setup for print_usage function

function print_usage
{
	exec use $myname
}

function notice
{
	if test -x /usr/lib/windows/bin/Notice; then
		/usr/lib/windows/bin/Notice -Et"Launching Wterm Program" $*
	else
		print $*
	fi
	exit 1
}

if ( sin | grep Dev32 > /dev/null 2>&1 ); then
  # have Dev 32 
  if test $QW_WIN_DEV; then
    device=$QW_WIN_DEV
  else
    device="/D16/win"
  fi
  dev_prefix=${device%/*}
else
  # no Dev 32 -> Dev 16 stuff
  if test $QW_WIN_DEV; then
    device=$QW_WIN_DEV
  else
    device="/dev/win"
  fi
  dev_prefix=${device%/*}
fi

if test ! -d $dev_prefix; then
  /bin/Dev16 -N$dev_prefix &
  sleep 1
else
  # test to see if we are seeing a remote dev prefix through the magic
  # of transparent networking

  # /dev/shmem is ALWAYS local
  var1=`fullpath -t /dev/shmem`
  var2=${var1%"/dev/shmem"}

  var3=`fullpath -t $dev_prefix`
  var4=${var3%$dev_prefix}

  if (test x$var2 != x$var4); then
    # remote dev_prefix
    /bin/Dev16 -N$dev_prefix &
    sleep 1
  fi
fi

if test ! -c ${device}1; then

	server=${WTERM:-/usr/lib/windows/apps/Wterm/Dev.win}

	if test -x $server; then
		$server $WTERMARGS -N$device &
	elif test -f $server; then
		if test -u $server; then
			notice "$server must have setuid bit on and owned by root"
		else
			notice "$server must be executable"
		fi
	else
		notice "$server is missing"
	fi
	typeset -i i=0
	while test $i -lt 10 -a ! -c ${device}1; do
		sleep 1;
		let i=i+1;
	done
	unset i
	if test ! -c ${device}1; then
		notice "
				$server was not able to start.\\n
				Make sure that Dev has enough free device entries.\\n
				The default is \"Dev -n32\" and wterm defaults\\n
				to using 8 devices."
	fi
fi

test -n "$WINENV" && test -r $WINENV && export OLDENV=$ENV ENV=$WINENV
                                                              
test -f ${TERMINFO:-"/usr/lib/terminfo"}//q/qnxw && TERM=qnxw

if test $# -lt 1; then
	eval ontty $device ${SHELL:-/bin/sh}
	exit 0
fi

typeset -i rows=0 cols=0 closed=0

while getopts r:c:p:st:l:i:I: opt
do
	case $opt in
		r)	rows=$OPTARG		;;
		c)	cols=$OPTARG		;;
		p)	prop=$OPTARG		;;
		s)	closed=1			;;
	   +s)	closed=0			;;
		t)	title="$OPTARG"		;;
		l)	long="$OPTARG"		;;
		i)	icon="$OPTARG"		;;
		I)	file=$OPTARG		;;
	   \?)	print ; print_usage	;;
	esac
done

if test $OPTIND -le 1; then
	if test -r $HOME/windows/$1.wt -o -r /usr/lib/windows/config/$1.wt; then
		prop=$1
	else
		eval ontty $device ${*:-${SHELL:-/bin/sh}}
		exit 0
	fi
else
	shift OPTIND-1
fi

exec 0<>$device 1<>&0

if test ! -t 0; then
	notice "Couldn't open a wterm window"
fi

ESC=""

if test -n "$prop"; then
	print $ESC"\"p$prop\""
elif test -r $HOME/windows/$1.wt -o -r /usr/lib/windows/config/$1.wt; then
	print $ESC"\"p$1\""
fi

if test $rows -ne 0 -o $cols -ne 0; then
	if test $rows -eq 0; then
		rows=25
	elif test $cols -eq 0; then
		cols=80
	fi
	print $ESC"/8;${rows};${cols}t"
fi

if test $closed -eq 1; then
	print $ESC"/2t"
fi

if test -n "$title"; then
	print $ESC"\"t$title\""
fi

if test -n "$long"; then
	print $ESC"\"l$long\""
fi

if test -n "$icon"; then
	print $ESC"\"i$icon\""
fi

if test -n "$file"; then
	print $ESC"\"I$file\""
fi

eval ontty $(tty) ${*:-${SHELL:-/bin/sh}}

exit 0

#ifdef __USAGE
#%C [options] [script [script_args]*]
#Options:
# -r rows           Number of visible rows on startup
# -c columns        Number of visible columns on startup
# -p prop file      Property file to load
# -s                Start small (as an Icon)  
# -t title          Title for window
# -l long-term      Long-term message for window
# -i icon label     Label for icon
# -I icon file      Picture file with an "Icon" symbol for icon 
#endif
