#!/bin/sh
# $Id: win-profile.sh,v 1.9 2004/05/16 12:38:40 lreiter Exp $
#
#	win-profile.sh:
#		Profile-based provisioning technology
#		"profile-aware" Windows startup script
#
#	Copyright 1984-2004 NeTraverse, Inc.
#

# Print out to the log file when ok to log, and to the user when trace is on.
Log()
{
	[ "$OK_TO_LOG" = "ok_to_log" ] && echo $EPT "$*" >> "$LOGFILE"
	[ "x$TRACE" = "xtrace" ]       && echo $EPT "$*"
}

# Print out to the user, and to the log file when ok to log.
Echo()
{
	[ "$OK_TO_LOG" = "ok_to_log" ] && echo $EPT "$*" >> "$LOGFILE"
	[ "$DO_SILENT" = "notsilent" ] && echo $EPT "$*"
}

# 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"
OK_TO_LOG="no_log"
DO_SILENT="notsilent"
LOGFILE="$HOME"/.merge/win-profile.sh.log
touch "$LOGFILE"
if [ -f "$LOGFILE" -a -w "$LOGFILE" -a ! -L "$LOGFILE" ]
then
	date > "$LOGFILE"
	OK_TO_LOG="ok_to_log"
else
	LOGFILE=/dev/null
	OK_TO_LOG="no_log"
fi

PROGNAME=`basename $0`

cd
if [ ! -r .merge-profile/profile-info -o ! -d win -o ! -d .merge ]; then
	if [ "$MERGE_WIN_PROFILE_FROM_WIN" = "yes" ]; then
		x_msg_error STRING PROFILE_NEEDS_GET_ERROR \
			"Your Windows account is not provisioned correctly."
		exit 7
	fi
	usrMsg STRING PROFILE_NOPROFILE_WARNING \
		"WARNING: profile-based Windows not found, \
running normal Windows startup instead..."
	Echo "$USRMSG_STRING"
	exec $STATICMERGE/publicbin/win $*
fi

# quick check to make sure there is not already a session running
"$STATICMERGE"/bin/win_probe -x win >>"$LOGFILE"
if [ $? = 18 ]; then
	DM="LCC.MERGE.UNIX.WIN"
	RETVAL=1
	if [ -x "$STATICMERGE"/publicbin/pksession -a \
			"$MERGE_MERGEAUTH_SESSION" = "yes" ]; then
		"$STATICMERGE"/publicbin/pksession win
		RETVAL=$?
	fi
	if [ $RETVAL != 0 ]; then
		x_msg_error STRING SESSION_ALREADY \
		"Cannot start another session from the same location."
		exit 9
	fi
fi

. .merge-profile/profile-info
if [ "$MERGE_PROFILE_TYPE" = "master" ]; then
	Log "Profile type is master"
	PROFMETA=.merge-profile/profile-meta
	rm -f $PROFMETA
	export MERGE_WIN_EXEC_FROM_WINPROFILE=true
	Log "about to run win"
	$STATICMERGE/publicbin/win $*
	chmod -R a+r .merge
	x_msg_status STRING PROFILE_UPDATE_STATUS \
		"Updating profile information, please wait..."
	$STATICMERGE/adminbin/profile-update win >$PROFMETA.tmp
	RETVAL=$?
	x_msg_close
	if [ "$RETVAL" = "0" ]; then
		mv $PROFMETA.tmp $PROFMETA
		chmod 644 $PROFMETA
		exit 0
	else
		x_msg_error STRING PROFILE_UPDATE_FAILED \
			"Failed to update profile information."
		exit 10
	fi
elif [ "$MERGE_PROFILE_TYPE" = "child" ]; then
	Log "Profile type is child"
	x_msg_status STRING PROFILE_LOAD_STATUS \
		"Loading profile information, please wait..."
	$STATICMERGE/adminbin/get-profile
	RETVAL=$?
	x_msg_close
	OLD_LOGFILE="$HOME"/.merge.profile-old/win-profile.sh.log
	[ -f "$OLD_LOGFILE" ] && mv "$OLD_LOGFILE" "$LOGFILE"
	case $RETVAL in
	0)
		$STATICMERGE/adminbin/erase-old-profile 3 &
		export MERGE_WIN_EXEC_FROM_WINPROFILE=true
		Log "about to exec win"
		unset LOGFILE
		exec $STATICMERGE/publicbin/win $*
		;;
	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
else
	x_msg_error STRING PROFILE_UNKNOWNTYPE_ERROR \
		"Unknown profile type for this user."
	exit 11
fi


