#!/bin/sh
#
#*******************************************************************************
#
# Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#*******************************************************************************
#

# Sanitize environemnt
PATH=/bin:/usr/bin
export PATH

#
## Lists available descriptors or application list files
##
## param inType
##  the type of files to be listed, -a ( application descriptors ),
##  -A ( application lists ) or -s ( session descriptors )
##
## returns KIOSK_RESULT_SUCCESS if successful or if a specified file exists
##         KIOSK_RESULT_FAILED otherwise
#
listAllDescriptors()
{
 inType=$1
 inName=$2

 if [ $inType = "-a" ] ; then
  listAppDescriptors

 elif [ $inType = "-A" ] ; then
  listAppLists

 else 
  listSessionDescriptors
 fi
}

#
## Tests for the existence of a specified descriptor or application list file
##
## param inType
##  the type of files to be listed, -a ( application descriptors ),
##  -A ( application lists ) or -s ( session descriptors )
##
## param inName 
##  the name ( or absolute path ) of a descriptor or application list whose
##  existence should be checked
##
## returns KIOSK_RESULT_SUCCESS if successful or if a specified file exists
##         KIOSK_RESULT_FAILED otherwise
#
listDescriptor()
{
 inType=$1
 inName=$2

 if [ $inType = "-a" ] ; then
  existsApplicationDescriptor "$inName"

 elif [ $inType = "-A" ] ; then
  existsAppList "$inName"

 else 
  existsSessionDescriptor "$inName"
 fi
}

#
## Prints the contents of a specified descriptor or Application List file.
## Application & Session descriptors are dotted and resulting KIOSK_APP_* or
## KIOSK_SESSION_* variables from the environment are printed. The contents
## of Application List files are simply catted.
## 
##
## param inType
##  the type of files to be listed, -a ( application descriptors ),
##  -A ( application lists ) or -s ( session descriptors )
##
## param inName
##  the name ( or absolute path ) of the descriptor or application list whose
##  contents should be printed
##
## returns KIOSK_RESULT_SUCCESS if successful
##         KIOSK_RESULT_FAILED otherwise
#
printDescriptor()
{
 inType=$1
 inName=$2

 if [ $inType = "-a" ] ; then
  printAppDescriptor "$inName"
 elif [ $inType = "-A" ] ; then
  printAppList "$inName"
 else
  printSessionDescriptor "$inName"
 fi
}

#
## The list* functions perform the actual listing work for the individual
## types supported by the list function above
#
listAppDescriptors()
{
 listFiles $KIOSK_APPS_DIR conf
}
listAppLists()
{
 listFiles $KIOSK_APPS_DIR list
}
listSessionDescriptors()
{
 listFiles $KIOSK_SESSIONS_DIR conf
}
listFiles()
{
 inDir=$1
 inSuffix=$2	 

 if [ -d "$inDir" ] ; then
  theDescriptors="$inDir/*.$inSuffix"
  for theDescriptor in $theDescriptors ; do
   if [ -f "$theDescriptor" ] ; then
    basename "$theDescriptor" | sed "s/\.${inSuffix}$//"
   fi
  done
 fi
 return $KIOSK_RESULT_SUCCESS
}

#
## The exists* functions check for the existence of descriptors or application
## list files. 
#
existsApplicationDescriptor()
{
 inAppName=$1

 theAppDescriptor=`appNameToDescriptorFile "$inAppName"`
 if [ -f "$theAppDescriptor" ] ; then
  echo $theAppDescriptor
  theRC=$KIOSK_RESULT_SUCCESS
 else
  echo 1>&2 "Application Descriptor '$inAppName' not found"
  theRC=$KIOSK_RESULT_FAILED
 fi
 return $theRC
}
existsAppList()
{
 inAppList=$1

 theAppList=`appListNameToAppListFile "$inAppList"`
 if [ -f "$theAppList" ] ; then
  echo $theAppList
  theRC=$KIOSK_RESULT_SUCCESS
 else
  echo 1>&2 "Application List '$inAppList' not found"
  theRC=$KIOSK_RESULT_FAILED
 fi
 return $theRC
}
existsSessionDescriptor()
{
 inSessionName=$1

 theSessionDescriptor=`sessionNameToDescriptorFile "$inSessionName"`
 if [ -f "$theSessionDescriptor" ] ; then
  echo $theSessionDescriptor
  theRC=$KIOSK_RESULT_SUCCESS
 else
  echo 1>&2 "Session Descriptor '$inSessionName' not found"
  theRC=$KIOSK_RESULT_FAILED
 fi
 return $theRC
}

#
## The print* functions perform the actual print work for the individual
## types supported by the print function above
#
printAppDescriptor()
{
 inAppDescriptor=$1

 theDescriptor=`existsApplicationDescriptor "$inAppDescriptor"`
 if [ $? -eq $KIOSK_RESULT_SUCCESS ] ; then
  # leave $HOME / $USER / $DISPLAY unsubstituted
  $READCFG -x -f "$theDescriptor" -E KIOSK_ "KIOSK_APP_*"
  if [ $? -eq 0 ] ; then
   theRC=$KIOSK_RESULT_SUCCESS
  else
   echo 1>&2 "Failed to load Application Descriptor '$inAppDescriptor'"
   theRC=$KIOSK_RESULT_FAILED
  fi
 else
  theRC=$KIOSK_RESULT_FAILED
 fi
 return $theRC
}
printAppList()
{
 inAppList=$1

 theList=`existsAppList "$inAppList"`
 if [ $? -eq $KIOSK_RESULT_SUCCESS ] ; then
  theRC=$KIOSK_RESULT_SUCCESS
  cat "$theList"
 else
  theRC=$KIOSK_RESULT_FAILED
 fi
 return $theRC
}
printSessionDescriptor()
{
 inSessionDescriptor=$1

 theDescriptor=`existsSessionDescriptor "$inSessionDescriptor"`
 if [ $? -eq $KIOSK_RESULT_SUCCESS ] ; then
  theSessionDir=`echo $theDescriptor| 
		  /bin/sed 's/\.[A-Za-z]*$//'`
  if [ -d "$theSessionDir" ] ; then
    KIOSK_SESSION_DIR="$theSessionDir"
  else
    KIOSK_SESSION_DIR=   
  fi     
  export KIOSK_SESSION_DIR
  # leave $HOME / $USER / $DISPLAY unsubstituted
  $READCFG -x -f "$theDescriptor" \
  	-e KIOSK_SESSION_DIR -E KIOSK_ "KIOSK_SESSION_*"
  if [ $? -eq 0 ] ; then
   theRC=$KIOSK_RESULT_SUCCESS
  else
   echo 1>&2 "Failed to load Session Descriptor '$inSessionDescriptor'"
   theRC=$KIOSK_RESULT_FAILED
  fi
  unset KIOSK_SESSION_DIR
 else
  theRC=$KIOSK_RESULT_FAILED
 fi
 return $theRC
}

short_usage()
{
	echo 1>&2 "Usage:"
	echo 1>&2 "      kioskdesc <subcommand> <options>"
	echo 1>&2 "      kioskdesc -h"
	exit 1
}


show_usage()
{
	echo 1>&2 "Usage:"
	echo 1>&2 "      kioskdesc list  { -a | -A | -s } [<name>]"
	echo 1>&2 "      kioskdesc print { -a | -A | -s } <name>"
	echo 1>&2 "      kioskdesc help"
	echo 1>&2 "      kioskdesc -h"
}

usage()
{
	show_usage
	exit ${1:-1}	
}

help()
{
	show_usage
	echo 1>&2  
	echo 1>&2 "Subcommands:"
	echo 1>&2 "      list    list one or all kiosk descriptors of a type."
	echo 1>&2 "      print   print a kiosk descriptor."
	echo 1>&2 "      help    show this help message."
	echo 1>&2  
	echo 1>&2 "Options:"
	echo 1>&2 "      -a      show application descriptors."
	echo 1>&2 "      -A      show application lists."
	echo 1>&2 "      -s      show session descriptors."
	echo 1>&2 "      -h      show a usage message."
	exit 0
}

#
## Main
#

#
## Load utilities and setup basic Kiosk Environment
#
theUtilsFile=/opt/SUNWkio/lib/utils.sh
if [ ! -r $theUtilsFile ] ; then
 echo 1>&2 "Error: can't read Kiosk utils file '$theUtilsFile'"
 exit 1
else
 . $theUtilsFile
 if [ $? -ne 0 ] ; then
  echo 1>&2 "Error: failed to load Kiosk utils file '$theUtilsFile'"
  exit 1
 fi
fi

READCFG=$KIOSK_LIB_DIR/readcfg
export KIOSK_CONFIG_DIR # /etc/opt/SUNWkio
export KIOSK_APPS_DIR   # /etc/opt/SUNWkio/applications
export KIOSK_SESSIONS_DIR # /etc/opt/SUNWkio/sessions
export KIOSK_PROTOS_DIR   # /etc/opt/SUNWkio/prototypes

if [ $# -lt 1 ] ; then
 short_usage
fi

theAction=$1
needName=usage # Should never be put to use, but ..
shift

case "$theAction" in
 list)
  needName=false
  ;;
 print)
  needName=true
  ;;
 help)
  help
  ;; 
 -h|-\?)
  usage 0
  ;;
 -*)
  echo 1>&2 "Error: Invalid option '$theAction' specified."
  short_usage
  ;;
 *)
  echo 1>&2 "Error: Invalid command '$theAction' specified."
  usage
  ;;
esac

theType=
while getopts "aAsh" theOption ; do
 case $theOption in
  a|A|s) 
   if [ -n "$theType" ] ; then
    echo 1>&2 "Error: only a single descriptor type can be specified."
    usage
   fi 
   theType="-$theOption"
   ;;

  h)
   help $theCommand
   ;;

  \?)
   usage
   ;;
 esac
done

if [ -z "$theType" ] ; then
    echo 1>&2 "Error: descriptor type option is required."
 usage
 exit $KIOSK_RESULT_FAILED
fi

shift `expr $OPTIND - 1`

if [ $# -eq 0 ] ; then
 if "$needName" ; then
  echo 1>&2 "Error: a descriptor name is required."
  usage
 fi
 "${theAction}AllDescriptors" $theType 

else
 if [ $# -gt 1 ] ; then
  echo 1>&2 "Error: only a single descriptor name can be specified."
  usage
 fi
 theName=$1

 if [ -z "$theName" ] ; then
  echo 1>&2 "Error: descriptor name is empty."
  exit 1
 fi
 "${theAction}Descriptor" $theType "$theName"
fi
