#!/bin/sh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos720 src/bos/usr/bin/cdat/types/psrasinit/psrasinit.sh 1.3 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2010 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 

usage()
{
    mlog -1 "Usage: psrasinit -s ca_server -p mgmt_port -P cmd_port"
    exit 128
}

phase_check()
{
    # Only AIX LPARs are valid CA clients
    [ "$CDAT_TYPE" = 'LPAR' ] || {
	mlog -1 "psrasinit is not supported on $CDAT_TYPE"
        exit 1	# IGNORE
    }

    # Check that the pureScale.client.rte fileset is installed
    fileset="pureScale.client.rte"
    remote_cmd "/usr/bin/lslpp -lq $fileset 2>/dev/null | grep -wq COMMITTED"
    [ $? -ne 0 ] && {
        mlog -1 "Fileset $fileset not installed on $CDAT_HOST"
        exit 1	# IGNORE
    }

    # Check that we have the ibm.ps.client.bind authorization required by
    # "psbind register".
    remote_cmd "/usr/bin/ckauth -A ibm.ps.client.bind"
    [ $? -ne 0 ] && {
        mlog -1 "ibm.ps.client.bind authorization required"
        exit 1	# IGNORE
    }
}

phase_init()
{
    # Install our client configuration script onto the remote node
    push_file "$CDAT_TYPE_DIR/config.sh" /tmp/ca_config.sh
    [ $? -ne 0 ] && {
        mlog -1 "Cannot install configuration script on $CDAT_HOST"
	exit 128
    }
}

phase_execute()
{
    # Run our configuration script with the specified options
    remote_cmd "/bin/sh /tmp/ca_config.sh $caserver $mgmtport $cmdport"
    [ $? -ne 0 ] && {
        mlog -1 "Configuration script failed on $CDAT_HOST"
	exit 128
    }
}

phase_clean()
{
    # Remove our configuration script from the remote node
    remote_cmd "rm -f /tmp/ca_config.sh"
}

PATH=$CDAT_SRVC_DIR:${PATH}

# Parse collect type options
while getopts s:p:P: opt
do
    case $opt in
    s)
	caserver="$OPTARG"
	;;
    p)
	mgmtport="$OPTARG"
	;;
    P)
	cmdport="$OPTARG"
	;;
    ?)
	usage
	;;
    esac
done

[ -z "$caserver" -o -z "$mgmtport" -o -z "$cmdport" ] && usage

case $CDAT_PHASE in
check)
    phase_check
    ;;
init)
    phase_init
    ;;
execute)
    phase_execute
    ;;
clean)
    phase_clean
    ;;
esac

exit 0
