#!/bin/ksh -p
#
# ident "@(#)M70PCSC.ksh	1.6 12/09/08 Oracle"
#
# Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.

#
# MODULE NAME: PCSC-lite
# AUTHOR     : Shefali Bhargava
# DESCRIPTION: Used for installing / uninstalling Sun Ray module for PCSC-lite
#
# 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.
# G_PID           : PID of the master task
#

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

#
# function PCSC_Check_Installed
#
# Description:
#   check whether PCSC-lite is installed
#
# Parameters:
#   (none)
#
#
function PCSC_Check_Installed {

  if AnyPackageInstalled $PCSC_PKG_RLIST; then
     _SW_INSTALLED="yes"
     _SW_COMPATIBLE="yes"

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

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

function Module_Init {

  _SW_INSTALLED="no"

  PCSC_Check_Installed

  if IsInstallRequired ; then
       _DO_INSTALL="yes"
       if [[ $_SW_INSTALLED = "yes" ]]; then
           _DO_REMOVE="yes"
	   AddPostInitMessage \
	   "Upgrade\t [ $PCSC_PROD_NAME $PCSC_INSTALLED_VERSION to $PCSC_VERSION ]"
       else
	   AddPostInitMessage "Install\t [ $PCSC_PROD_NAME $PCSC_VERSION ]"
       fi
  elif IsUninstallRequired && [[ ${_SW_INSTALLED} = "yes" ]]; then
       _DO_REMOVE="yes"
       AddPostInitMessage \
	"Remove\t [ $PCSC_PROD_NAME $PCSC_INSTALLED_VERSION ]"
  fi

   return 0

}

#
# function Module_Preserve
#
# Description:
#   If installed build is earlier than 1.3_10.b do not preserve data.
#   The config file is not necessary since libutsmon will use
#   a dynamic version if a real one does not exist.
#
# Parameters:
#   (none)
#
# Globals used:
#

function Module_Preserve {

  return 0

}

#
# function Module_Remove
#
# Description:
#
# Parameters:
#   (none)
#
# Globals used:
#    G_UT_PROD_NAME
#    G_UT_VERSION
#    G_ADMIN_FILE
#

function Module_Remove {

   RemoveMsg "$PCSC_PROD_NAME" "$PCSC_INSTALLED_VERSION"

   RemoveProduct $G_ADMIN_FILE $PCSC_PKG_RLIST

#
# if the removal didn't succeed, we exit with 1 so to keep the procedure
# going...
#
   if [[ $? != 0 ]]; then
      return 1
   fi

  return 0

}

#
# function Module_Install
#
# Description:
#
# Parameters:
#   (none)
#
# Globals used:
#    G_DEBUG
#    G_UT_PROD_NAME
#    G_UT_VERSION
#    G_ADMIN_FILE
#

function Module_Install {

    PCSC_PKG_CONFLICT=""
    pkgquerycmd=""

    if [[ "$G_OS" == "Linux" ]];then
	pkgquerycmd="rpm -q"
    else 
	pkgquerycmd="pkginfo"
    fi

  # Remove conflicting packages for pcsc-lite
    for pkg in $PCSC_PKG_CONFLICT_LIST; do
	$pkgquerycmd $pkg > /dev/null 2>&1
	if  [ $? -eq 0 ]; then
	    PCSC_PKG_CONFLICT="$PCSC_PKG_CONFLICT $pkg"
	fi      
    done
    
    if   [ -n "$PCSC_PKG_CONFLICT" ]; then
	print -u2 \
	    "Removing obsolete and conflicting packages for $PCSC_PROD_NAME"
	print "$PCSC_PKG_CONFLICT"
	RemoveProduct $G_ADMIN_FILE $PCSC_PKG_CONFLICT > /dev/null 2>&1
    fi
    vflag=""

    if [[ $G_DEBUG = "yes" ]]; then
	vflag="-v"
    fi
    
    InstallMsg "$PCSC_PROD_NAME" "$PCSC_VERSION"
    
    if ! InstallProduct $G_ADMIN_FILE $PCSC_DIR $PCSC_PKG_LIST; then
	print -u2 \
	    "$PCSC_PROD_NAME not successfully installed"
	
	return 2
    fi
    
    return 0

}

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

function Module_Restore {

  return 0

}

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

function Module_Abort {

   return 0

}

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

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"

typeset -r PCSC_PROD_NAME="Smart Card Services"
typeset -r PCSC_DIR=$(GetPackagePath Smart_Card_Services_1.6)
typeset -r PCSC_VERSION="1.6"

#
# BEGIN: Developers module variables definition here
#
if [[ "$G_OS" == "Linux" ]]; then
    typeset -r PCSC_PKG_LIST="SUNWpcsc-1.6-?*.i386.rpm SUNWpcscdtu-1.6-?*.i386.rpm"
    typeset -r PCSC_PKG_RLIST="SUNWpcsc SUNWpcscdtu"
    typeset -r PCSC_PKG_CONFLICT_LIST="esc coolkey-devel coolkey ccid ifd-egate pcsc-lite pcsc-lite-libs pcsc-lite-devel"
else
    typeset -r PCSC_PKG_LIST="SUNWpcsc SUNWpcscdtu"
    typeset -r PCSC_PKG_RLIST=$PCSC_PKG_LIST
    typeset -r PCSC_PKG_CONFLICT_LIST="SUNWsrcbp SUNWocfd SUNWocfh SUNWocfr SUNWpcsclite SUNWpcsclite-devel SUNWjcom"
fi



#
# END
#

#
# FRAMEWORK CODE
#

. ${G_MEDIA_DIR}/support_lib/framework_lib
