#!/bin/sh
#############################################################################
# Copyright (C)2004-2005, Sun Microsystems Inc. All rights reserved.
# Use is subject to license terms.
#
# Name: enable
# Description: enable for SUNWcctpx pkg.
#
# pragma ident	"$Id: enable.sh,v 1.2.2.4 2005/06/10 17:30:37 rc5273 Exp $"
##############################################################################

################################################################################
#  Function of the enable script 
#  1.  do and/or verify required configuration
#  2.  add entry to /etc/init.d for controlling executable from rcX.d scripts
#################################################################################

#
# control logging method
#
log=echo
#log="logger -pdaemon.err"

#
# Global definitions
#
debug="0"
configdir="/usr/lib/cc-cfw/platform/transport/etc/"
configname="transport.cfg"
configfile="${configdir}/${configname}"
sockdir="/var/tmp/cc-transport"
tracefile="/var/tmp/cctclient.trace" 

fw_pkg="SUNWccfw"

#########################
# Main processing logic #
#########################

#
# build the correct command prefix and identify the bin directory
#

bindir="/usr/lib/cc-cfw/platform/transport/bin"
if [ ! -d $bindir ]
then
    $log "enable: $bindir does not exist"
    exit 1
fi

#
# check for the cctransport executable
#

if [ ! -x $bindir/cctransport ]
then
    $log "enable: cannot find cctransport executable"
    exit 1
fi

#
# check for the cctrunner executable
#
if [ ! -x $bindir/cctrunner ]
then
    $log "enable: cannot find cctrunner executable"
    exit 1
fi

#
# check for proxy configuration file directory
#

if [ ! -d ${configdir} ]
then
    $log "enable: ${configdir} does not exist"
    exit 1
fi

#
# check for config file
#
if [ ! -f $configfile ]
then
    $log "enable: cannot find Connected Client Transport configuration file"
    exit 1
fi

/usr/bin/chmod 640 $configfile
ret=$?
if [ $ret -ne 0 ]
then
    $log "chmod failed for $configfile"
    exit 1
fi

#
# Make sure that sockdir exists
#
/usr/bin/mkdir -m 0755 -p ${sockdir}
ret=$?
if [ $ret -ne 0 ]
then
    $log "mkdir -m 0755 -p  failed for ${sockdir}"
    exit 1
fi

#
# change mode of the sock directory
#
/usr/bin/chmod 755 ${sockdir}
ret=$?
if [ $ret -ne 0 ]
then
    $log "chmod failed for ${sockdir}"
    exit 1
fi

#
# Cleanup the imapi socket file in from a previously running proxy
#
if [ -f ${sockdir}/CCT_AGENT_SOCK ] ; then
    /usr/bin/rm -f ${sockdir}/CCT_AGENT_SOCK
fi

#
# set owner on trace output file, if it exists
#
if [ -f ${tracefile} ]
then
    /usr/bin/chown root:root ${tracefile}
    if [ $ret -ne 0 ]
    then
        $log "chown failed for $tracefile"
        exit 1
    fi 
fi

#
# Execute cctrunner -q which creates and initializes the proxy queue
# space, including the install queue
#
${bindir}/cctrunner -q
if [ $? -ne 0 ]
then
    $log "proxy queue initialization failed"
    exit 1
fi

#
#  Check for the presence of the Configuration Framework Package
#
pkginfo $fw_pkg > /dev/null
if [ $? -ne 0 ] ; then
    $log "Required package $fw_pkg is not installed, please install it."
    exit 1
fi

#
# Invoke the framework's create_init_entry function 
#

##  Send in the path to the executable and the runlevel
runlevel_file=/usr/lib/cc-cfw/platform/transport/runlevel
executable_script=/usr/lib/cc-cfw/platform/transport/cc-transport

$log Creating initd entry for transport
`/usr/lib/cc-cfw/framework/lib/get_interface CREATE_INITD_ENTRY`  $executable_script $runlevel_file

$log Setting the status to enabled for transport
`/usr/lib/cc-cfw/framework/lib/get_interface SET_COMPONENT_STATUS`  platform transport enabled

#
# exit with success status
#

exit 0
