#!/bin/posix/sh
#
# This script will start M$ applications for M$ documents.
# Usage: ./remwin <doctype> <doc>
# eg. PowerPoint:  remwin ppt /tmp/foo/fuzzy.ppt
#
# T. Nuutinen & Goodi
# 09.10.97
#
#*****************************************************************
# $Id: remwin,v 1.17 1997/10/28 11:33:09 martikka Exp $
# Src location: /tools/env/src/showwin
#*****************************************************************

#*****************************************************************
#*
#* User or site configurable parameters
#*
#*****************************************************************
APPSERVER=nmsdd01he
REMOTEUSER=${LOGNAME}
DOMAIN=ntc.nokia.com
WINDDRESOLUTION=1024x768
#
CHKCMD="echo"
#
geometry="1024x768"
colors="16"
#
# This specifies the directory (under your NT-domain homedir)
# where the tmp-files are kept.
tmpdir='tmp'
#
pwfile="$HOME/sec/auth"

###################################################################
#
app=$1
filename=$2
#
###################################################################
# Do basic checkups

# If ASPL user then use ASPL systems :)
if ypcat netgroup|grep aspl|grep $LOGNAME >/dev/null ; then
	exec /net/share/bin/showwin $*
	exit
fi

# DISPLAY
if [ -z "$DISPLAY" ]; then
	echo "We need correct DISPLAY env. settings."
	exit -1
fi

# Args
if [ $# -gt 2 ]; then
	/net/share/bin/xdialog -t "Invalid args" -m "$0 [doc|xls|ppt|mpt] [filename]"
	exit 1
fi
# File really exsists?
# File must be a) full pathname. eq /tmp/foo/foo.doc or b) path relative to user
# homedir ie docs/omat/foo.doc
# If no filename given, then just start the application.
if [ -n "$filename" ]; then
	cd $HOME
	if [ ! -r $filename ]; then
		/net/share/bin/xdialog -t "File not found: $filename" -m "Filename path MUST be full (/tmp/foo.doc) OR relative to your homedir (pc/doc/omadoc.doc)."
		exit 1
	fi
fi

##################################################################
#
#
DIRNAME=`dirname $filename`
BASENAME=`basename $filename"`
HOST=`/bin/uname -n`
#
RSH="remsh"
#

# Save this for later use
NTBASENAME=$BASENAME

# Select application
# Append .doc,.ppt or .xls if that ending is not there (or is something else)
case $app in
doc)    APP='d:\msoffice\winword\winword' 
	if echo $BASENAME | grep -e \.doc$ > /dev/null ; then 
		echo s > /dev/null
	elif echo $BASENAME | grep -e \.DOC$ > /dev/null ; then 
		echo s > /dev/null
	else 
		NTBASENAME="$BASENAME.doc"
	fi
	;;
ppt)    APP='d:\msoffice\powerpnt\powerpnt' 
	if echo $BASENAME | grep -e \.ppt$ > /dev/null ; then 
		echo s > /dev/null
	elif echo $BASENAME | grep -e \.PPT$ > /dev/null ; then 
		echo s > /dev/null
	else 
		NTBASENAME="$BASENAME.ppt"
	fi
	;;
xls)    APP='d:\msoffice\excel\excel' 
	if echo $BASENAME | grep -e \.xls$ > /dev/null ; then 
		echo s > /dev/null
	elif echo $BASENAME | grep -e \.XLS$ > /dev/null ; then 
		echo s > /dev/null
	else 
		NTBASENAME="$BASENAME.xls"
	fi
	;;
mpt)    APP='d:\apps\winproj\winproj' 
	if echo $BASENAME | grep -e \.mp+$ > /dev/null ; then 
		echo s > /dev/null
	elif echo $BASENAME | grep -e \.MP+$ > /dev/null ; then 
		echo s > /dev/null
	else 
		NTBASENAME="$BASENAME.mpt"
	fi
	;;
"")	
	# No application selected. Start just WinDD.
	;;
*) 
	/net/share/bin/xdialog -t "WinDD application error: $app" -m "Available applications: doc,xls,mpt and ppt. Usage: $0 [doc|xls|mpt|ppt] [filename]"
	exit 1
	;;
esac

# Select resolution
case "X$WINDDRESOLUTION" in
X|X640x480|X800x600|X1024x768|X1280x1024)
	geometry=$WINDDRESOLUTION
	;;
*)	
	/net/share/bin/xdialog -t "WinDD resolution error: $WINDDRESOLUTION" -m "Setup WINDDRESOLUTION env variable correctly."
	exit 1
	;;
esac

# For PC-users, we need to mangle the DISPLAY...
# dhcp-displaynames seem to be too long for WinDD to handle correctly.
case $DISPLAY in
*-dhcp-*)	DISPLAY="`echo $DISPLAY|awk -F. '{ print $1 }'`:0" ;;
*)	;;
esac

# No security... :)
if (! (xhost | grep -e "$APPSERVER" -e "access control disabled" > /dev/null)) ; then
   if xhost $APPSERVER 2>&1 1>/dev/null | grep -e "local machine" > /dev/null ; then
	/net/share/bin/xdialog -t "X authentication error for $APPSERVER" -f /net/share/lib/remwin.message.1
	exit 1
   fi
fi

# If we don't have connection already. Create one.
if $RSH $APPSERVER rwindd $CHKCMD | grep 'No current' > /dev/null ; then
    # Plain text passwd in $HOME/sec/auth... dont do it man!
    if [ -r $HOME/sec/auth ]; then
    	$RSH $APPSERVER -l $LOGNAME c:\\windd\\windd.exe -h $APPSERVER \
    	-display $DISPLAY -geometry $geometry -colors $colors \
    	-force_xcursor -login $LOGNAME -domain $DOMAIN \
    	-pass `cat $pwfile` \
    	> /dev/null &
    else
    	$RSH $APPSERVER -l $LOGNAME c:\\windd\\windd.exe -h $APPSERVER \
    	-display $DISPLAY -geometry $geometry -colors $colors \
    	-force_xcursor -login $LOGNAME -domain $DOMAIN \
    	> /dev/null &
    fi
    # Sleep until the connection has been really established.
    sleep 5 
    let "i=0"
    while $RSH $APPSERVER rwindd $CHKCMD | grep 'No current' > /dev/null
    do
        sleep 2
	let "i=i+1"
	if [ $i -gt 9 ]; then
		/net/share/bin/xdialog -t "WinDD startup error on $APPSERVER" -f /net/share/lib/remwin.message.2
		exit 1
	fi
    done
fi
# We (now) have connection. Use that.
#  Exit if no application selected
if [ -z "$app" ]; then
	exit
fi

# Just start the application if no filename specified.
if [ -z "$filename" ]; then
    $RSH $APPSERVER rwindd $APP > /dev/null
    exit
fi
# Create (if needed) tmp directory for files to homedir.
$RSH $APPSERVER rwindd mkdir \\$tmpdir > /dev/null

# Copy file to WinDD server: HOMEDISK:\tmp\filename
$RSH $APPSERVER rwindd rcp -b $HOST:$DIRNAME/$BASENAME \\$tmpdir\\$NTBASENAME > /dev/null

# I still don't get this...(?)
#if ps -fu $LOGNAME | grep 'MO' > /dev/null ; then
#	touch $filename
#fi

# Start application 
$RSH $APPSERVER rwindd $APP \\$tmpdir\\$NTBASENAME > /dev/null
# Delete files away. Some cases this cannot be done if previous RSH does not block
# but continues. Then this delete fails.
$RSH $APPSERVER rwindd del \\$tmpdir\\$NTBASENAME > /dev/null 2>&1

# Security back on...
xhost -$APPSERVER > /dev/null  2>&1

# Delataan mahdollinen tmp-file jonka pine, yms vekotin on tehnyt.
if echo $DIRNAME | grep -e '^/tmp' -e '^/usr/tmp' > /dev/null ; then
	if [ -r $DIRNAME/$BASENAME ]; then
		if echo $BASENAME | grep -v -e '^MO' ; then 
			rm -f $DIRNAME/$BASENAME > /dev/null 2>&1
		fi
	fi
fi

# End.
