#!/bin/ksh
#
#*******************************************************************************
#
# 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.
#

theModule=kiosk:jds3-session

#
## ensure the version of JDS installed is correct and issue a warning otherwise.
#
checkJDSVersion()
{
 inModule=$1

 theVersion=`pkgparam SUNWgnome-base-libs VERSION`
 if [ -n "$theVersion" ] ; then
  theVersion=${theVersion%%,REV=*}
  if [ "$theVersion" != "2.6.0" ] ; then
   logWarning -m $inModule \
    -l "Warning: this session is certified to work with gnome version 2.6.0\
 but the version of gnome on this system is '$theVersion'. The session may not\
 function correctly as a result."
  fi
 fi
}

KIOSKLIBDIR=/opt/SUNWkio/lib
GCONFTOOL=gconftool-2
#
##  load ( via. gconftool-2 --load ) all gconf available settings
#
loadGconfData()
{
 inModule=$1
 inGconfDataDir=$2

 if [ -d "$inGconfDataDir" ] ; then
  theGConfFiles="$inGconfDataDir/*.xml"
  for theGConfFile in $theGConfFiles ; do
   logDebug -m $inModule -l "loading gconf data from '$theGConfFile'"
   nawk '{
    gsub( "_KIOSK_HOME_", home )
    gsub( "_KIOSK_LIB_", libdir )
    gsub( "_KIOSK_SESSION_DIR_", sessiondir )
    print $0
   }' home=$HOME libdir=$KIOSKLIBDIR sessiondir=$KIOSK_SESSION_DIR $theGConfFile | $GCONFTOOL --load=-
   if [ $? -ne 0 ] ; then
    logError -m $inModule \
             -l "Error: failed to load gconf data file '$theGConfFile'"
    return $KIOSK_RESULT_FAILED
   fi
  done
 fi
 return $KIOSK_RESULT_SUCCESS
}

# 
## configure the background gnome shows
## Syntax is
##   solid color: #XXXXXX
##   picture:     [<mode>:]/path/to/pic
##                <mode>:relative/path/to/pic
##      e.g.:     stretched:/usr/share/pixmaps/backgrounds/Sun-Default.png
##   The path may be relative to $KIOSK_SESSION_DIR, if mode is given
#
configureBackground()
{
  inModule=$1
  inBackground=$2

  case $inBackground in
  /*)
    theMode="stretched"
    thePicture=$inBackground
    ;;
  *:/*)
    theMode=${inBackground%%:*}
    thePicture=${inBackground#*:}
    ;;
  *)
    theMode="none"
    theColor=$inBackground
    ;;
  esac

  theBgKey=/desktop/gnome/background
  
  if [ -n "$theMode" ] ; then
    $GCONFTOOL -t string -s $theBgKey/picture_options "$theMode"
    if [ $? -ne 0 ] ; then
      logError -m $inModule -l "Error: background setup failed"
      return $KIOSK_RESULT_FAILED
    fi
  fi  
            
  if [ -n "$theColor" ] ; then
    $GCONFTOOL -t string -s $theBgKey/color_shading_type "solid" &&
    $GCONFTOOL -t string -s $theBgKey/primary_color "$theColor"
    if [ $? -ne 0 ] ; then
      logError -m $inModule -l "Error: background setup failed"
      return $KIOSK_RESULT_FAILED
    fi
  fi  
            
  if [ -n "$thePicture" ] ; then
    if [ ! -r "$thePicture" ] ; then
      logError -m $inModule \
               -l "Error: background picture '$thePicture' is missing"
      return $KIOSK_RESULT_FAILED
    fi
    
    $GCONFTOOL -t string -s $theBgKey/picture_filename "$thePicture"
    if [ $? -ne 0 ] ; then
      logError -m $inModule -l "Error: background setup failed"
      return $KIOSK_RESULT_FAILED
    fi
  fi  
            
  logDebug -m $inModule \
       -l "Desktop background set to '$inBackground' (picture_options=$theMode)"

  return $KIOSK_RESULT_SUCCESS
}

#
## configure Nautilus so that
## 1. Default icons aren't added/displayed
## 2. show_desktop is enabled if configured
#
configureNautilus()
{
 inModule=$1
 inDesktopPolicy=$2    

 theFileName=`echo file://$HOME/Desktop.xml | sed 's/\//%2F/g'`
 mkdir $HOME/.nautilus/metafiles &&
 cat << ! > $HOME/.nautilus/metafiles/$theFileName
<?xml version="1.0"?>
<directory/>
!
 if [ $? -ne 0 ] ; then
  logError -m $inModule -l "Error: nautilus metafiles setup failed"
  return $KIOSK_RESULT_FAILED
 fi

 logDebug -m $inModule "Show nautilus desktop for session: $inDesktopPolicy"
 $GCONFTOOL -t bool -s /apps/nautilus/preferences/show_desktop $inDesktopPolicy
 if [ $? -ne 0 ] ; then
  logError -m $inModule -l "Error: nautilus gconf setup failed"
  return $KIOSK_RESULT_FAILED
 fi

 return $KIOSK_RESULT_SUCCESS
}

#
## Updates the given desktop entry file replacing _KIOSK_SESSION_DIR_ with
## the value of $KIOSK_SESSION_DIR and localizing user-visible entries
#
updateDesktopFile()
{
 inModule=$1
 theFile=$2

 theName=$(sed -n -e 's/^Name=//p' $theFile)
 theComment=$(sed -n -e 's/^Comment=//p' $theFile)

 theTextdomain=${KIOSK_SESSION_TEXTDOMAIN:-$KIOSK_TEXTDOMAIN}
 theTextdomaindir=${KIOSK_SESSION_TEXTDOMAINDIR:-$KIOSK_TEXTDOMAINDIR}
 
 sed -e "s,_KIOSK_SESSION_DIR_,$KIOSK_SESSION_DIR,g" \
     -e "s/Name=$theName/Name=$(localize "$theName" "$theTextdomain" "$theTextdomaindir")/" \
     -e "s/Comment=$theComment/Comment=$(localize "$theComment" "$theTextdomain" "$theTextdomaindir")/" \
	 $theFile > $theFile.tmp &&
     mv $theFile.tmp $theFile
 if [ $? -ne 0 ] ; then
  logError -m $inModule -l "Error: failed to update file '$theFile'"
  return $KIOSK_RESULT_FAILED
 fi
 return $KIOSK_RESULT_SUCCESS
}


#
## Main
#

logger -i -p user.debug -t $theModule "Starting jds3-session"

#
## Load utilities and setup basic Kiosk Environment
#
theUtilsFile=$KIOSKLIBDIR/utils.sh
if [ ! -r $theUtilsFile ] ; then
 logger -i -p user.error -t $theModule \
        "Error: can't read Kiosk utils file '$theUtilsFile'"
 exit 1
else
 . $theUtilsFile
 if [ $? -ne 0 ] ; then
  logger -i -p user.error -t $theModule \
         "Error: failed to load Kiosk utils file '$theUtilsFile'"
  exit 1
 fi
fi

checkJDSVersion $theModule

#
## Evaluate session options
#
show_desktop="false"
background="#000000" # Default to solid black
while getopts "b:s" theOption ; do
 case $theOption in
  b)
    background="$OPTARG"
    ;;
  s)
    show_desktop="true"
    ;;
  *)
    logWarning -m $theModule -l "Warning: invalid session option"
    logDebug -m $theModule -l "[args($#)=$*]"
    exit $KIOSK_RESULT_FAILED
    ;;
 esac
done 

if [ $OPTIND -le $# ] ; then
  logWarning -m $theModule -l "Warning: spurious session argument ($OPTIND)"
  logDebug -m $theModule -l "[args($#)=$*]"
fi  
#
## load available gconf data to configure panel, menu etc.
#
loadGconfData $theModule "$KIOSK_SESSION_DIR/gconfdata"
if [ $? -ne $KIOSK_RESULT_SUCCESS ] ; then
 exit $KIOSK_RESULT_FAILED
fi

#
## ensure that nautilus doesn't display default icons etc.
#
configureNautilus $theModule $show_desktop
if [ $? -ne $KIOSK_RESULT_SUCCESS ] ; then
 exit $KIOSK_RESULT_FAILED
fi

#
## setup the desktop background
#
configureBackground $theModule $background
if [ $? -ne $KIOSK_RESULT_SUCCESS ] ; then
 exit $KIOSK_RESULT_FAILED
fi

#
## add any added apps.
#
processApplicationList $theModule
if [ $? -ne $KIOSK_RESULT_SUCCESS ] ; then
 exit $KIOSK_RESULT_FAILED
fi

#
## update the application directory entry
#
updateDesktopFile $theModule "$HOME/.kiosk/menus/applications/.directory"
if [ $? -ne $KIOSK_RESULT_SUCCESS ] ; then
 exit $KIOSK_RESULT_FAILED
fi

#
## update the logout desktop entry
#
updateDesktopFile $theModule "$HOME/.kiosk/menus/logout.desktop"
if [ $? -ne $KIOSK_RESULT_SUCCESS ] ; then
 exit $KIOSK_RESULT_FAILED
fi

#
## finally, launch gnome-session
#
exec gnome-session
