#!/bin/ksh

#--------------------------------------------------------------------------
# This script must be run from the secureMain script
# and it must run as root
#--------------------------------------------------------------------------

#--------------------------------------------------------------------------
# Licensed Materials - Property of IBM
# 5724-C04
#
# (C) Copyright IBM Corporation 2006. All Rights Reserved.
# US Government Users Restricted Rights - Use, duplication
# or disclosure restricted by GSA ADP Schedule Contract
# with IBM Corp.
#--------------------------------------------------------------------------
#
# Description:
#
#set and validate the user ID of the installation owner
#set and validate the installation directory
#Unlock permissions so that we can install additional products
#as someone other than the root ID or the original installation ID
#we are running as root, so we will change permissions for all users
#open up the global permissions for the installation directory tree
#check for any servers in the installation directory tree
#open up the permissions for TEPS to apply new application support files
#open up the permissions for TEP client to apply new application support files
#open up the permissions for TEMS to apply new application support files
#--------------------------------------------------------------------------

if [[ $# != 3 ]]
then
   #$NLS-E$ "Parameters missing, exiting"
   nls_replace 'KCI0515E'
   exit
fi

#gather the calling parms
owner=$1
commongroup=$2
appcodes=$3

#validate the user ID of the installation owner
if [[ -z "$owner" ]]
then
   #$NLS-E$ "Installation Owner ID not supplied, exiting"
   nls_replace 'KCI0510E'
   exit
fi

#validate the common group specified
if [[ -z "$commongroup" ]]
then
   #$NLS-E$ "Common Group not supplied, exiting"
   nls_replace 'KCI0512E'
   exit
fi

#validate the application list
if [[ -z "$appcodes" ]]
then
   #$NLS-E$ "Application list not supplied, exiting"
   nls_replace 'KCI0513E'
   exit
fi

#set and validate the installation directory
[[ -z "$CANDLEHOME" ]] && CANDLEHOME=$(cd $(dirname $0) ; dirname $(pwd) )
if [[ ! (-d "$CANDLEHOME/bin" && -d "$CANDLEHOME/config" && 
      -d "$CANDLEHOME/logs" && -d "$CANDLEHOME/registry") ]]
then
   #$NLS-E$ "$CANDLEHOME is not a valid installation, exiting"	  
   nls_replace 'KCI0530E' $CANDLEHOME
   exit
fi

#Source the helper functions
. $CANDLEHOME/bin/secureHelper

#set and validate the list of platform directories in the installation directory
platform_dirs=$(print $(ls -d aix* hp* sol* l* 2> /dev/null | grep -v licenses | grep -v logs))
if [[ -z "$platform_dirs" ]]
then
   #$NLS-E$ "No platform directories were found, exiting"
   nls_replace 'KCI0514E'
   exit
fi

#change directory to the installation directory
cd "$CANDLEHOME"

secureMode=""
if [[ -r "$SecureFile" ]]
then
   secureMode=$(cat $SecureFile)
fi
if [[ -z "$secureMode" ]]
then
   secureMode="unlock:$owner:@none@"
fi

oldMode=$(print "$secureMode" | cut -d: -f1)
oldUser=$(print "$secureMode" | cut -d: -f2)
oldGroup=$(print "$secureMode" | cut -d: -f3)

#we are running as root, so we will change permissions for all users
#open up the global permissions for the installation directory tree

install_dirs="BACKUP bin classes config InstallITM old_registry registry JRE LAP tables tmaitm6 tmp */ui"
install_dirs=`returnExisting $install_dirs`
chmod a+rwx . *
chmod -R a+rwx $install_dirs

#check for any servers in the installation directory tree
cqver=$(ls registry/cq[ahsl]*.ver 2> /dev/null | grep '^registry' | head -1)
cjver=$(ls registry/cj[ahsl]*.ver 2> /dev/null | grep '^registry' | head -1)
msver=$(ls registry/ms[ahsl]*.ver 2> /dev/null | grep '^registry' | head -1)

#open up the permissions for TEPS to apply new application support files
if [[ -n "$cqver" ]]
then
      cqplat=$(basename $cqver |cut -d. -f1 | cut -c 3- )
      chmod -R a+rwx $cqplat/cq $cqplat/cw
fi

#open up the permissions for TEP client to apply new application support files
if [[ -n "$cjver" ]]
then
      cjplat=$(basename $cjver |cut -d. -f1 | cut -c 3- )
      chmod -R a+rwx $cjplat/cj
fi

#open up the permissions for TEMS to apply new application support files
if [[ -n "$msver" ]]
then
      msplat=$(basename $msver |cut -d. -f1 | cut -c 3- )
      chmod -R a+rwx $msplat/ms tables
fi

print "unlock:$oldUser:oldGroup" > $SecureFile

