#!/bin/sh
# $Id: dynwin.sh,v 1.5 2004/05/21 15:38:58 lreiter Exp $
#
#	dynwin.sh:
#		Profile-based provisioning technology
#		Dynamic Profile launch script
#
#	Copyright 1984-2004 NeTraverse, Inc.
#

# Print to the user when trace is on.
Log()
{
	[ "x$TRACE" = "xtrace" ] && echo $EPT "$*"
}

# Print out to the user if not set to silent
Echo()
{
	[ "$DO_SILENT" = "notsilent" ] && echo $EPT "$*"
}

# Print out program usage information and exit with error code
Usage()
{
	echo "usage: $PROGNAME <master-home> [<target-config>] [-a <win-args>]"
	echo ""
	echo "   <master-home>   home directory of master profile to inherit"
	echo "   <target-config> optional transient configuration name"
	echo "   -a <win-args>   optional arguments to pass to 'win'"
	echo ""
	echo "  NOTE: -a must appear at the end of the command line, followed"
	echo "        by arguments to pass to the 'win' command."
	echo ""
	echo " examples:"
	echo "   $PROGNAME /home/master"
	echo "    - launches a dynamic session, without transient data or "
	echo "      registry changes, from the master profile installed in"
	echo "      /home/master"
	echo ""
	echo "   $PROGNAME /home/master -a -g 800x600"
	echo "    - same as above, but fixes Windows desktop size to 800x600"
	echo ""
	echo "   $PROGNAME /home/master master"
	echo "    - launches a dynamic session and runs it as the configuration"
	echo "      named 'master' for this user; subsequent launches of this"
	echo "      configuration will apply preserve, copy, omit, and registry"
	echo "      rules as specified in the master profile account."
	exit 1
}

# Extract title and/or geometry, if available, from command line arguments,
# and shift arguments to omit these from the remainder
Extract_Args()
{
	for i in 1 2; do
		if [ "$1" = "-title" ]; then
			shift
			WINTITLE="-title $1"
			shift
		elif [ "$1" = "-g" ]; then
			shift
			WINGEOM="-g $1"
			shift
		fi
	done
}

# system setup, lmf domain setup, log setup
. /etc/mrgssv.sh
. $STATICMERGE/set_nls.sh
. $STATICMERGE/statusmsg.sh
DM="LCC.MERGE.UNIX.WIN_PROFILE"
[ "$SYSTEMTYPE" = "linux" ] && EPT="-e"
DO_SILENT="notsilent"

PROGNAME=`basename $0`

# check usage
if [ "$1" != "" -a "$1" != "-a" ]; then
	MASTERHOME="$1"
	shift
	if [ "$1" != "-a" ]; then
		TARGETCFG="$1"
		shift
	fi
	if [ "$1" = "-a" ]; then
		shift
	elif [ "$1" != "" ]; then
		Usage
	fi
else
	Usage
fi

if [ "$TARGETCFG" != "" ]; then
	TARGETNUM=""
	TARGETDIR=$HOME/dynwin-$TARGETCFG
	TARGETOLD=$TARGETDIR".old"
else
	TARGETNUM=`$STATICMERGE/publicbin/sysnum`
	TARGETCFG="dynwin-$TARGETNUM"
	TARGETDIR="$HOME/.dynwin-$TARGETNUM"
fi

# quick check to make sure there is not already a session running
if [ -r $HOME/.merge/$TARGETCFG.cfw ]; then
	"$STATICMERGE"/bin/win_probe -x $TARGETCFG
	if [ $? = 18 ]; then
		DM="LCC.MERGE.UNIX.WIN"
		x_msg_error STRING SESSION_ALREADY \
			"Cannot start another session from the same location."
		exit 9
	fi
fi

if [ ! -r $MASTERHOME/.merge-profile/profile-info ]; then
	x_msg_error STRING GETPROFILE_READMASTER_ERROR \
		"Unable to read master profile information."
	exit 4
fi
. $MASTERHOME/.merge-profile/profile-info
if [ "$MERGE_PROFILE_TYPE" != "master" ]; then
	x_msg_error STRING GETPROFILE_INVALIDMASTER_ERROR \
		"Specified master profile is not type master."
	exit 5
fi
PROFMETA=$MASTERHOME/.merge-profile/profile-meta

# get the config and rename it appropriately
mkdir -p $HOME/.merge
mkdir -p $HOME/mydata
cp $MASTERHOME/.merge/win.cfw $HOME/.merge/$TARGETCFG.cfw

# set the personal drive properly
winconfig $TARGETCFG set drive=c,unix,$TARGETDIR,map_win95
if [ $? != 0 ]; then
	x_msg_error STRING GETPROFILE_LOADFAILED_ERROR \
		"Failed to load profile information."
	exit 7
fi

if [ "$TARGETNUM" != "" ]; then
	# completely dynamic session type, no transient data
	x_msg_status STRING PROFILE_LOAD_STATUS \
		"Loading profile information, please wait..."
	mkdir -p $TARGETDIR
	cd $TARGETDIR
	$STATICMERGE/adminbin/profile-load $MASTERHOME/win <$PROFMETA
	RETVAL=$?
	x_msg_close
	if [ $RETVAL != 0 ]; then
		x_msg_error STRING GETPROFILE_LOADFAILED_ERROR \
			"Failed to load profile information."
		exit 7
	fi

	# fix the registry to make the machine name unique, and the user name
	# match the host login name if using VNET
	cd
	grep -q "^NETWORK_TYPE=vnet" $TARGETDIR/.boot/info.txt
	if [ $? = 0 ]; then

		# figure out the unique machine name, not to exceed 15
		# characters: <short-host>-d<sysnumber>; short-host must be
		# trimmed to no more than 8 characters
		SHORTHOST=`hostname -s |cut -b 1-8`
		VNETHOST="$SHORTHOST-d$TARGETNUM"
		USERNAME=`id -un`

		# import the changes into the registry
		cat <<EOF |$STATICMERGE/publicbin/regedit -k $TARGETCFG -
REGEDIT4

[HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\ComputerName\ComputerName]
"ComputerName"="$VNETHOST"

[HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\VxD\VNETSUP]
"ComputerName"="$VNETHOST"
"Comment"="dynwin session $TARGETNUM of $USERNAME on $SHORTHOST"

[HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\VxD\MSTCP]
"HostName"="$VNETHOST"

[HKEY_LOCAL_MACHINE\Network\Logon]
"username"="$USERNAME"

EOF
	fi

	# make sure -title and -g args are at the beginning, with everything
	# else at the end of the win command line
	Extract_Args $*
	for i in "$WINTITLE" "$WINGEOM"; do
		if [ "$i" != "" ]; then
			shift
			shift
		fi
	done

	# launch Windows for this config
	$STATICMERGE/publicbin/win $WINTITLE $WINGEOM +k$TARGETCFG $*
	RETVAL=$?

	# remove dynamic data
	rm -rf $TARGETDIR
	rm .merge/$TARGETCFG.cfw
	exit $RETVAL
else
	export TARGETCFG TARGETDIR TARGETOLD
	x_msg_status STRING PROFILE_LOAD_STATUS \
		"Loading profile information, please wait..."
	$STATICMERGE/adminbin/get-profile $MASTERHOME
	RETVAL=$?
	x_msg_close
	case $RETVAL in
	0)
		$STATICMERGE/adminbin/erase-old-profile 3 &
		export MERGE_WIN_EXEC_FROM_WINPROFILE=true
		Log "about to exec win"
		unset LOGFILE

		# make sure -title and -g args are at the beginning, with 
		# everything else at the end of the win command line
		Extract_Args $*
		for i in "$WINTITLE" "$WINGEOM"; do
			if [ "$i" != "" ]; then
				shift
				shift
			fi
		done

		# launch Windows for this config
		$STATICMERGE/publicbin/win $WINTITLE $WINGEOM +k$TARGETCFG $*
		RETVAL=$?

		# remove dynamic data
		rm .merge/$TARGETCFG.cfw
		;;
	3)
		x_msg_error STRING GETPROFILE_NOMASTER_ERROR \
			"Cannot determine master profile location."
		;;
	4)
		x_msg_error STRING GETPROFILE_READMASTER_ERROR \
			"Unable to read master profile information."
		;;
	5)
		x_msg_error STRING GETPROFILE_INVALIDMASTER_ERROR \
			"Specified master profile is not type master."
		;;
	6)
		x_msg_error STRING GETPROFILE_MASTERNOWIN_ERROR \
			"Master profile does not contain Windows installation."
		;;
	7)
		x_msg_error STRING GETPROFILE_LOADFAILED_ERROR \
			"Failed to load profile information."
		;;
	8)
		x_msg_error STRING GETPROFILE_SCRIPTFAILED_ERROR \
			"User script failed during profile load."
		;;
	*)
		x_msg_error STRING PROFILE_UNKNOWN_ERROR \
			"Unknown error occurred: $RETVAL" "$RETVAL"
		;;
	esac
	exit $RETVAL
fi

