#!/bin/ksh -p
#
# ident "@(#)M38KIO.ksh	1.55 09/09/17 SMI"
#
# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

# DESCRIPTION: To install the kiosk bundled packages
#
# The following exported variables (initialized by the master task) are 
# available (read-only) for the module:
#
# G_PROGRAM_ID    : program name
# G_MEDIA_DIR     : pathname of the install directory on the media (CD,...)
# G_PRODUCT_DIR   : pathname of the bundles directory on the media (CD,...)
# G_ADMIN_FILE    : pathname of the admin file used by pkgadd. default provided.
# G_DEBUG         : debug on/off. Possible values "yes", "no"
# G_QUICK_INSTALL : used to force a quick install (no user inputs).
#                   possible values "yes", "no"
# G_TMP_DIR       : pathname of the temporary directory 
# G_UT_PROD_NAME  : product name
# G_UT_VERSION    : product version
# G_DAEMON_LOC    : location of OS daemon scripts
# G_LOGFILE       : pathname of the log file.
#

#
# Module developers to provide the following functions:
# Module_Init, Module_Preserve, Module_Remove, Module_Install, Module_Restore
# Module_Abort, Module_Exit
#

#
# function Kiosk_Preinstall
#
# Description:
#    Pre-install procedure: check whether Kiosk Packages are to be installed
#                           and whether CAM needs to be removed.
#
# Parameters:
#    (none)
#

function Kiosk_Preinstall {

   if [[ $BB_INSTALLED = "yes" ]]; then
      ProductMsg -n "$BB_PROD_NAME $BB_INSTALLED_VERSION"
      print "installed"
      _DO_REMOVE="yes"
   fi    

   if [[ $KIOSK_INSTALLED = "yes" ]]; then
      ProductMsg -n "$KIOSK_PROD_NAME $KIOSK_INSTALLED_VERSION"

      print "installed"

      # remove old instance of Kiosk
      # XXX possible future optimization - check versions to see if what is
      # installed is the same as what is in the package and if so skip
      # remove/install.
      #
      _DO_REMOVE="yes"
   else
      ProductMsg -n "$KIOSK_PROD_NAME $KIOSK_VERSION"

      print "not installed"
   fi

   if isTrustedSolaris ; then
      AddPostInitMessage \
       "Skip\t [ $KIOSK_PROD_NAME $KIOSK_VERSION is not supported with Solaris Trusted Extensions ]"
      return 0
   fi

   _DO_INSTALL="yes"

   if [[ $KIOSK_INSTALLED = "yes" ]]; then
	AddPostInitMessage \
            "Upgrade\t [ $KIOSK_PROD_NAME $KIOSK_INSTALLED_VERSION to $KIOSK_VERSION  ]"
   elif [[ $BB_CONFIGURED = "yes" ]]; then
	AddPostInitMessage \
            "Migrate\t [ $BB_PROD_NAME $BB_INSTALLED_VERSION to $KIOSK_PROD_NAME $KIOSK_VERSION  ]"
   elif IsDataPreserved; then
	AddPostInitMessage \
            "Upgrade\t [ $KIOSK_PROD_NAME $KIOSK_VERSION  ]"
   else
	AddPostInitMessage \
	    "Install\t [ $KIOSK_PROD_NAME $KIOSK_VERSION ]"
   fi

   return 0
}

#
# function Kiosk_Prepreserve
#
# Description:
#    Pre-preservation procedure: 
#             check whether Kiosk or CAM data needs to be preserved.
#
# Parameters:
#    (none)
#

function Kiosk_Prepreserve {

   if [[ $KIOSK_INSTALLED = "yes" ]]; then
	AddPostInitMessage \
            "Preserve [ $KIOSK_PROD_NAME $KIOSK_INSTALLED_VERSION configuration ]"
   fi
   if [[ $BB_CONFIGURED = "yes" ]]; then
	AddPostInitMessage \
            "Preserve [ $BB_PROD_NAME $BB_INSTALLED_VERSION configuration ]"
   fi

   return 0
}

#
# function Kiosk_Preremove
#
# Description:
#    Pre-install procedure: check whether Kiosk Packages have to be removed
#
# Parameters:
#    (none)
#

function Kiosk_Preremove { 
     if [[ $KIOSK_INSTALLED = "yes" ]]; then
         _DO_REMOVE="yes"
         AddPostInitMessage "Remove\t [ $KIOSK_PROD_NAME $KIOSK_INSTALLED_VERSION ]"
         return 0
     fi
     if [[ $_SW_INSTALLED != "yes" ]]; then
	 Note "No $KIOSK_PROD_NAME packages installed." 
     fi
     return 0
}

#
# function Kiosk_Check_Installed
#
# Description:
#   check whether Kiosk Session Services is installed
#
# Parameters:
#   (none)
#
#
function Kiosk_Check_Installed {

  if AnyPackageInstalled $KIOSK_PKG_RLIST; then
     _SW_INSTALLED="yes"
     _SW_COMPATIBLE="yes"
     KIOSK_INSTALLED="yes"

     if $G_MEDIA_DIR/utprodinfo -t installed SUNWkio; then
        KIOSK_INSTALLED_VERSION=$($G_MEDIA_DIR/utprodinfo -p \
				SUNWkio VERSION | cut -d_ -f1)
     fi
  fi
}

#
# function BB_Preremove
#
# Description:
#    Pre-install procedure: check whether BlackBox Packages have to be removed
#
# Parameters:
#    (none)
#

function BB_Preremove {
     if [[ $BB_INSTALLED = "yes" ]]; then
         _DO_REMOVE="yes"
         AddPostInitMessage "Remove\t [ $BB_PROD_NAME $BB_INSTALLED_VERSION ]"
     fi
     return 0
}

#
# function BB_Check_Installed
#
# Description:
#   check whether Controlled Access Mode is installed
#
# Parameters:
#   (none)
#
#
function BB_Check_Installed {
  typeset bb_version_pkg="SUNWbbcho"    

  if AnyPackageInstalled $BB_PKG_RLIST $CAM_PKG_RLIST ; then
     BB_INSTALLED="yes"
     _SW_INSTALLED="yes"
     _SW_COMPATIBLE="no"

     if $G_MEDIA_DIR/utprodinfo -t installed $bb_version_pkg; then
	BB_INSTALLED_VERSION=$($G_MEDIA_DIR/utprodinfo -p \
			    $bb_version_pkg VERSION | cut -d_ -f1)
     fi
     if [ -s $BB_USER_CONF  ] ; then
	BB_CONFIGURED="yes"
     fi	    
 fi
}

#
# function Module_Init
#
# Description:
#   initialization of the module for installation, uninstallation
#
# Parameters:
#   (none)
#

function Module_Init {
  typeset exit_no="0"

  _SW_INSTALLED="no"
  KIOSK_INSTALLED="no"
  BB_INSTALLED="no"
  BB_CONFIGURED="no"

  Kiosk_Check_Installed
  if [ -n "$BB_PKG_RLIST" ] ; then
      BB_Check_Installed
  fi
 
  if IsInstallRequired; then
     Kiosk_Preinstall
     exit_no=$?

  elif IsPreserveRequired; then
     Kiosk_Prepreserve
     exit_no=$?

  elif IsUninstallRequired; then
     BB_Preremove
     Kiosk_Preremove
     exit_no=$?
  fi

  return $exit_no
}

#
# function Module_Preserve
#
# Description:
#   
#
# Parameters:
#   (none)
#

function Module_Preserve {

# Save our config to the preserve file
   typeset result=0
   typeset madefile=false

   if [[ -x "$KIOSKUSERADM" ]] ; then 
       if $KIOSKUSERADM show -p > $KIOSK_USER_CONF_PRESERVE ; then
	   madefile=true
       else
	   # kioskuseradm had nothing to save	   
	   rm -f $KIOSK_USER_CONF_PRESERVE
       fi	       
   fi	   

   SaveFiles $PRESERVE_FILES_ALL
   if [[ $? != 0 ]]; then
      result=2
   fi
   
   # Remove the file, to get back to a clean state
   $madefile && rm -f $KIOSK_USER_CONF_PRESERVE

   return $result
}

#
# function Kiosk_Remove
#
# Description:
#    Remove Kiosk Packages
#
# Parameters:
#   (none)
#

function Kiosk_Remove {

  RemoveMsg "$KIOSK_PROD_NAME" "$KIOSK_INSTALLED_VERSION"

  if [[ -x "$KIOSKUSERADM" ]] ; then
      $KIOSKUSERADM delete -fq 2> /dev/null
  fi	  
  if [[ -x "$KIOSKCONFIG" ]] ; then
      $KIOSKCONFIG reset > /dev/null 2>&1
  fi	  

  if ! RemoveProduct $G_ADMIN_FILE $KIOSK_PKG_RLIST; then
     Note "$KIOSK_PROD_NAME not successfully removed"
     return 1
  fi

  return 0

}

#
# function BB_Remove
#
# Description:
#    Remove Controlled Access Mode Packages
#
# Parameters:
#   (none)
#

function BB_Remove {

  RemoveMsg "$BB_PROD_NAME" "$BB_INSTALLED_VERSION"

  if ! RemoveProduct $G_ADMIN_FILE $BB_PKG_RLIST $CAM_PKG_RLIST ; then
     Note "$BB_PROD_NAME not successfully removed"
     return 1
  fi

  return 0

}

#
# function Module_Remove
#
# Description:
#    Remove Kiosk or Controlled Access Mode Packages
#
# Parameters:
#   (none)
#

function Module_Remove {
  typeset exit_no="0"

  if [[ $BB_INSTALLED != "no" ]]; then    
      if ! BB_Remove; then
	  exit_no=1
      fi
  fi
  if [[ $KIOSK_INSTALLED != "no" ]]; then    
      if ! Kiosk_Remove; then
	  exit_no=1
      fi
  fi

  return $exit_no

}

#
# function CheckProdVer
#
# Description:
#   Helper function to test, if a given product version of a package
#   is installed.
#
# Parameters
#   Package name
#   Product version to match (may be a pattern for ksh test builtin)
#
# Returns
#   0 if version matches
#   non-zero otherwise
#

function CheckProdVer {
  typeset pkg="$1"
  typeset ver="$2"
  typeset prod_ver

  prod_ver="$($G_MEDIA_DIR/utprodinfo -p $pkg PRODVERS 2> /dev/null)"

  [[ "$prod_ver" = $ver ]]
}

#
# function Module_Install
#
# Description:
#    To install Kiosk Session Service Packages
#
# Parameters:
#   (none)
#

function Module_Install {

  typeset vflag=""

  if [[ $G_DEBUG = "yes" ]]; then
     vflag="-v"
  fi

  InstallMsg "$KIOSK_PROD_NAME" "$KIOSK_VERSION"

  # cleanup old instances
  RemoveProduct $G_ADMIN_FILE $KIOSK_PKG_RLIST || \
    Error "failed to completely remove $KIOSK_PROD_NAME"

  # select optional packages
  typeset pkg_list="$KIOSK_PKG_LIST"
  if [ -n "$KIOSK_OPTIONAL_PKG_LIST" ] ; then
      for sess_pkg in $KIOSK_OPTIONAL_PKG_LIST ; do
	 # install package unless we find a reason not to
	  typeset reason=""
	  case $sess_pkg in
	  *kio-cde)
	      if ! ProductInstalled SUNWdtwm SUNWdtbas ; then
		  reason="CDE is not available."
	      fi
	      ;;
	  *kio-jds3)
	      typeset -r jds3_prodver="2.6.0"
	      if ! ProductInstalled SUNWgnome-session ; then
		  reason="GNOME is not available"
	      elif ! CheckProdVer SUNWgnome-session $jds3_prodver ; then
		  reason="GNOME $jds3_prodver is not available."
	      fi
	      ;;
	  *)
	      reason="Unknown optional package."
	      ;;
	  esac
	  if [ -z "$reason" ] ; then
	      pkg_list="$pkg_list $sess_pkg"
	  else
	      Note "Skipping package $sess_pkg: $reason"
	  fi
      done
  fi

  if ! InstallProduct $G_ADMIN_FILE ${KIOSK_DIR} $pkg_list; then
      Error "$KIOSK_PROD_NAME not successfully installed"
  fi

  return 0
}

#
# function Module_Restore
#
# Description:
#    Restore saved configuration data
#
# Parameters:
#   (none)
#

function Module_Restore {

    typeset path
    typeset file
    typeset do_restore_users="no"

    if GetPreservedFilePath "$KIOSK_USER_CONF_PRESERVE" ; then
	print "\nRestoring $KIOSK_PROD_NAME configuration ...\n"
	path=${_RETURN_VAL}
	cp -p "$path" "$KIOSK_USER_CONF_PRESERVE" &&
	    do_restore_users="yes"    
    elif GetPreservedFilePath "$BB_USER_CONF" ; then
	print "\nImporting $BB_PROD_NAME $BB_INSTALLED_VERSION configuration ...\n"
	path=${_RETURN_VAL}
	BB_MigrateConf "$path" "$KIOSK_USER_CONF_PRESERVE" 
        # don't do automatic restore   
    fi

    if [[ "$do_restore_users" = "yes" ]]
    then
	CreateKioskUsers "$KIOSK_USER_CONF_PRESERVE"
	if [[ $? = 0 ]]; then
		rm -f "$KIOSK_USER_CONF_PRESERVE"
	fi
    fi
    if [[ -f "$KIOSK_USER_CONF_PRESERVE" ]]
    then
	fmt <<-!

		$KIOSK_PROD_NAME user account configuration has been 
		saved in file $KIOSK_USER_CONF_PRESERVE.

		Please use 'utconfig -k' to configure $KIOSK_PROD_NAME user 
		accounts.
    
	!
    fi

    echo "Restoration of $KIOSK_PROD_NAME data complete"

    return 0

}

#
# function BB_MigrateConf
#
# Description:
#    Create kiosk user configuration from a CAM configuration file
#    Adds the group name "utkiosk" (hardcoded)
#    The resulting config can be used as defaults for a Kiosk configuration
#
# Parameters:
#    $1 - srcfile - the CAM users.conf file to migrate
#    $2 - dstfile - the kioskuser.conf file to generate
#
function BB_MigrateConf {
    typeset -r srcfile="$1"
    typeset -r dstfile="$2"
    typeset prefix
    typeset firstid
    typeset numacct
    typeset dummy
    typeset retval=1
    
    if [[ ! -r "$srcfile" ]] ; then
	print "Cannot read $BB_PROD_NAME configuration from file $srcfile !"
	return 1
    fi

# This relies on the private format of the $BB_USER_CONF file, as did the original CAM installation.
    if ! read prefix firstid numacct dummy < "$srcfile" ; then 
	print "Reading $BB_PROD_NAME configuration from file $srcfile failed !"
	return 1
    fi

    if [ -n "$dummy" ] ; then
	print "File $srcfile is not a $BB_PROD_NAME user configuration file (too many entries) !"
	return 1
    fi
    
    if [ -z "$numacct" ] ; then
	print "File $srcfile is not a valid $BB_PROD_NAME user configuration file (too few entries) !"
	return 1
    fi

    mkdir -m 0755 -p "$(dirname $dstfile)"
    if print -- "-l $prefix -u $firstid -c $numacct" > "$dstfile" ; then
	return 0;
    else
	print "Writing $KIOSK_PROD_NAME configuration to file "$dstfile" failed"
	return 1
    fi

}
 
#
# function CreateKioskUsers
#
# Description:
#    Recreate kiosk users from restored kiosk user configuration
#
# Parameters:
#    conffile - the configuration file to restore from
#
function CreateKioskUsers {
    typeset conffile="$1"
    typeset kioskbase
	
    print "\nRestoring $KIOSK_PROD_NAME user accounts ...\n"

    if [[ ! -x "$KIOSKUSERADM" ]] ; then
	print "Cannot restore $KIOSK_PROD_NAME user accounts: $KIOSKUSERADM is not available! " 
	return 1
    fi	
    
    if ! $KIOSKUSERADM cleanup ; then
	print "Removal of leftover $KIOSK_PROD_NAME user accounts failed! " 
	return 1
    fi    
		      
# If the parameters in our preserved file are not valid or complete
# or if kiosk config already exists, this will fail.
# In both cases 'utconfig -k' can be used to interactively reconfigure.    
    if ! $KIOSKUSERADM create `cat "$conffile"` ; then
	print "$KIOSK_PROD_NAME user account configuration failed!"  
	return 1
    fi    
		      
    return 0
}

#
# function Module_Abort
#
# Description:
#    Abort procedure
#
# Parameters:
#   (none)
#

function Module_Abort {

   return 0
}

#
# function Module_Exit
#
# Description:
#    Exit procedure (normal termination)
#
# Parameters:
#   (none)
#

function Module_Exit {

   return 0

}

#
# END
#

#
# MAIN STARTS HERE
#

trap "exit 2" HUP INT QUIT TERM

#
# include libraries
#

. ${G_MEDIA_DIR}/support_lib/iu_lib
. ${G_MEDIA_DIR}/support_lib/module_lib
. ${G_MEDIA_DIR}/support_lib/upgrade_lib

export _EVENT=$1
export _MODULE_NAME=$(basename $0)
export _VARS_LIST=""
export _EXIT_CODE=0
export _RETURN_VAL=0
export _VAR_STORAGE_FILE="${G_TMP_DIR}/.${G_PROGRAM_ID}.${_MODULE_NAME#???}"

DeclareModuleVar _DO_INSTALL="no"
DeclareModuleVar _DO_REMOVE="no"
DeclareModuleVar _SW_INSTALLED="no"
DeclareModuleVar _SW_COMPATIBLE="yes"

#
# BEGIN: Developers module variables definition here
#

typeset -r KIOSK_PROD_NAME="Kiosk Mode"
typeset -r KIOSK_DIR=$(GetPackagePath Kiosk_Mode_4.3)
if [[ "$G_OS" = "Linux" ]]; then
 if [[ "`uname -p`" = *64* ]] ; then 
     typeset -r KIOSK_PKG_64="SUNWkio-libs-64"
     typeset -r KIOSK_PKG_LIST_64="\
      ${KIOSK_PKG_64}-4.3-?*.x86_64.rpm"
 else     
     typeset -r KIOSK_PKG_64=""
     typeset -r KIOSK_PKG_LIST_64=""
 fi
 typeset -r KIOSK_PKG_LIST="\
  SUNWkior-4.3-?*.i386.rpm  \
  SUNWkio-libs-4.3-?*.i386.rpm \
  SUNWkio-4.3-?*.i386.rpm \
  SUNWkiom-4.3-?*.i386.rpm \
  ${KIOSK_PKG_LIST_64}"
 typeset -r KIOSK_OPTIONAL_PKG_LIST=""
 typeset -r KIOSK_PKG_RLIST="SUNWkiom \
  SUNWkio SUNWkio-libs \
  SUNWkior \
  ${KIOSK_PKG_64}"
else
 typeset -r KIOSK_PKG_LIST="SUNWkior \
  SUNWkio-libs \
  SUNWkio SUNWkiom"
 typeset -r KIOSK_OPTIONAL_PKG_LIST="SUNWkio-cde \
  SUNWkio-jds3"
 typeset -r KIOSK_PKG_RLIST="$KIOSK_PKG_LIST $KIOSK_OPTIONAL_PKG_LIST"
fi
typeset -r KIOSK_VERSION="4.3"

DeclareModuleVar KIOSK_INSTALLED="no"
DeclareModuleVar KIOSK_INSTALLED_VERSION=""

# Variables needed for removing the old CAM BlackBox packages

typeset -r BB_PROD_NAME="Controlled Access Mode"

if [[ "$G_OS" = "Linux" ]]; then
 typeset -r BB_PKG_RLIST=""
 typeset -r BB_VERSION_PKG=""
else
 typeset -r BB_PKG_RLIST="SUNWbbcho SUNWbbchr"
 typeset -r CAM_PKG_RLIST="SUNWutkir SUNWutkio"
 typeset -r BB_VERSION_PKG="SUNWbbcho"
fi

DeclareModuleVar BB_INSTALLED="no"
DeclareModuleVar BB_INSTALLED_VERSION=""
DeclareModuleVar BB_CONFIGURED="no"

# Variables needed for preserve/restore of user configuration
typeset -r KIOSKUSERADM=/opt/SUNWkio/bin/kioskuseradm
typeset -r KIOSKCONFIG=/opt/SUNWkio/bin/kioskconfig

# Use a file we own to preserve and restore
typeset -r UT_ETC_DIR="/etc/opt/SUNWut"
typeset -r KIOSK_USER_CONF_PRESERVE="$UT_ETC_DIR/kioskuser.preserved"

# For CAM config we'll continue to use the private file directly
typeset -r BB_VAR_DIR="/var/opt/SUNWbb"
typeset -r BB_USER_CONF="$BB_VAR_DIR/users.conf"

if [[ "$G_OS" = "Linux" ]]; then
 typeset -r PRESERVE_FILES_ALL="$KIOSK_USER_CONF_PRESERVE"
else
 typeset -r PRESERVE_FILES_ALL="$KIOSK_USER_CONF_PRESERVE $BB_USER_CONF"
fi

#
# END
#

#
# FRAMEWORK CODE 
#

. ${G_MEDIA_DIR}/support_lib/framework_lib
