#!/bin/false
# ksh helper-script - this file is not intended to be run directly
#
# ident "$Id$ SMI"
#
#*******************************************************************************
#
# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#*******************************************************************************
#

#
## WARNING: This file must not be altered in any way.
##          Please consult your Kiosk Session Service documentation for details
##          on building your own Kiosk Sessions.
#

#
## kiosk-appintegrate
##
## Helper function for the CDE kiosk session to manage integration of kiosk 
## applications into the session.
##
## This file is intended to be sourced from the cde-kiosk-session script and 
## performs its work in collaboration with the 'applauncher' script.
##
## This file expects functions and settings from the kiosk utils.sh script to 
## be available.
#

#
## Global session files.
#
KIOSK_AUTOAPPFILE_=$HOME/.dt/sessions/sessionetc
KIOSK_DTMENUFILE_=$HOME/.dt/dtwmrc
KIOSK_DTLOCALIZEDMENU_=$HOME/.dt/${LANG:-C}/dtwmrc

#
## Helper function to localize embedded quoted text strings in a file 
## according to KIOSK_SESSION_TEXTDOMAIN and KIOSK_SESSION_TEXTDOMAINDIR
#
localizeEmbeddedStrings() 
{
 typeset textdomain=${KIOSK_SESSION_TEXTDOMAIN:-$KIOSK_TEXTDOMAIN}
 
 if [ -z "$textdomain" ] ; then
 	cat
	return
 fi	

 typeset textdomaindir=${KIOSK_SESSION_TEXTDOMAINDIR:-$KIOSK_TEXTDOMAINDIR}

 TEXTDOMAIN=$textdomain TEXTDOMAINDIR=$textdomaindir \
 	perl -n -e '@part=split(/"/,$_,3) ; 
	            $part[1] = `gettext "$part[1]"` 
		        unless ($part[0] =~ /#/) or ($part[1] eq "") ;
	            print join("\"",@part) '
}

#
## prepareAppIntegration
#
## Prepare the session integration files
## - dtwm menu file which will contain the user applications.
## - sessionetc file which will contain the auto applications.
##
## param inModule
##    the module name to use for logging
##    
## param inDoMenu
##    'yes' to create a dtwm menu file
##    
## return
##    0 on success
##    non-zero on failure
#    
prepareAppIntegration() {
    typeset inModule=$1
    typeset inDoMenu=$2

    logDebug -m "$inModule" -l "Preparing application integration (do_menu=$inDoMenu)"

    # Prepare session autostart file
    mkdir -p "`dirname $KIOSK_AUTOAPPFILE_`"
    echo "# Generated session file for CDE kiosk session" > $KIOSK_AUTOAPPFILE_
    if [ $? != 0 ] ; then
	logError -m "$inModule" -l "Cannot generate session file $KIOSK_AUTOAPPFILE_"
	return 1
    fi	

    # Prepare workspace menu file
    rm -f $KIOSK_DTMENUFILE_ $KIOSK_DTLOCALIZEDMENU_
    if [ $? != 0 ] ; then
	# This should never happen, but to be defensive ...
	logError -m "$inModule" -l "Cannot  remove leftover menu file"
	return 1
    fi	
    
    if [ "$inDoMenu" = "no" ] ; then
	return 0
    fi	    

    cat $KIOSK_SESSION_DIR/dtwmrc.header > $KIOSK_DTMENUFILE_
    if [ $? != 0 ] ; then
	logError -m "$inModule" \
	    -l "Cannot generate session file $KIOSK_DTMENUFILE_"
	return 1
    fi	

    # Prepare localized workspace menu file
    if [ -n "$LANG" ] ; then
	mkdir -p "`dirname $KIOSK_DTLOCALIZEDMENU_`"

	localizeEmbeddedStrings \
		< $KIOSK_SESSION_DIR/dtwmrc.header \
		> $KIOSK_DTLOCALIZEDMENU_
	
	if [ $? != 0 ] ; then
	    logWarning -m "$inModule" \
		-l "Cannot generate session file $KIOSK_DTLOCALIZEDMENU_"
	    # Continue with unlocalized file
	fi	
    fi
    return 0
}

#
## buildAppIntegration
#
## Build the main body of the session integration files
##
## Go through each application descriptor
## - add user+auto applications to the dtwm menu file
## - add auto+critical applications to the sessionetc file 
##
## The main work is done by the 'applauncher' script
##
## param inModule
##    the module name to use for logging
##    
## return
##    0 on success
##    non-zero on failure
#    
buildAppIntegration() {
    typeset inModule=$1

    # Export file locations for 'applaunch'
    export KIOSK_DTMENUFILE_
    export KIOSK_DTLOCALIZEDMENU_
    export KIOSK_AUTOAPPFILE_

    processApplicationList "$inModule"
    if [ $? -ne $KIOSK_RESULT_SUCCESS ] ; then
	# process application list should do the logging
	return 1
    fi
    return 0
}

#
## finishAppIntegration
#
## Wrap up the session integration files
## - dtwm menu file which will contain the user applications.
## - sessionetc file which will contain the auto applications.
##
## param inModule
##    the module name to use for logging
##    
## return
##    0 on success
##    non-zero on failure
#    
finishAppIntegration() {
    typeset inModule=$1

    logDebug -m "$inModule" -l "Finishing application integration"
    #
    ## Make sure the session startup file is executable
    #
    if [ -f $KIOSK_AUTOAPPFILE_ ] ; then	
	chmod +x $KIOSK_AUTOAPPFILE_
	if [ $? != 0 ] ; then
	    logError -m "$inModule" -l "Cannot finish session file $KIOSK_AUTOAPPFILE_"
	    return 1
	fi	
    fi	    

    #
    ## Finish the menu file and the locale-specific file
    #
    if [ -f $KIOSK_DTMENUFILE_ ] ; then
	cat $KIOSK_SESSION_DIR/dtwmrc.footer >> $KIOSK_DTMENUFILE_
	if [ $? != 0 ] ; then
	    logError -m "$inModule" -l "Cannot finish session file $KIOSK_DTMENUFILE_"
	    return 1
	fi	

	if [ -n "$LANG" -a -f $KIOSK_DTLOCALIZEDMENU_ ] ; then
	
	    localizeEmbeddedStrings \
		    < $KIOSK_SESSION_DIR/dtwmrc.footer \
		    >> $KIOSK_DTLOCALIZEDMENU_
	
	    if [ $? != 0 ] ; then
		logWarning -m "$inModule" \
		    -l "Cannot create session file $KIOSK_DTLOCALIZEDMENU_"
		    
		rm -f $KIOSK_DTLOCALIZEDMENU_
		if [ $? != 0 ] ; then
		    logError -m "$inModule" \
			-l "Cannot discard unfinished session file $KIOSK_DTLOCALIZEDMENU_"
		    return 1
		fi	
	    fi	
	fi	
    fi		

    logDebug -m "$inModule" -l "Application integration complete"
    return 0
}
