#!/bin/sh
#
# $Id$ Copyright (c) 2019 EMC Corporation
#

#
# Copyright (c) 2019 EMC Corporation. All Rights Reserved.
#
# This software contains the intellectual property of EMC
# Corporation or is licensed to EMC Corporation from third
# parties. Use of this software and the intellectual property
# contained therein is expressly limited to the terms and
# conditions of the License Agreement under which it is
# provided by or on behalf of EMC.
#

#
# This script is to configure and validate the run-time environment of
# the NetWorker server.
#
# Note that this script is sourced by the init.d script. To exit, 'return'
# must be used, not 'exit'.
#

# Set environment variables AUTHC_HOME, AUTHC_PKG_HOME
AUTHC_PKG_HOME=`rpm -q --qf "%{INSTPREFIXES}\n" lgtoauthc 2>/dev/null`
if [ ! -f "$AUTHC_PKG_HOME/script/authc_configure.sh" ]; then
	AUTHC_PKG_HOME=/opt/nsr/authc-server
fi

AUTHC_HOME="/nsr/authc"
export AUTHC_HOME

AUTHC_INSTALLRC=$AUTHC_HOME/conf/installrc

#
# Java services require JAVA_HOME and jsvc
# Read consolidated JAVA_HOME from lgtoauthc
#
JAVA_HOME=

[ -f "$AUTHC_INSTALLRC" ] && . "$AUTHC_INSTALLRC"

if [ -z "$JAVA_HOME" ]; then
	fmt 1>&2 <<EOF
Error:
    Unable to read consolidated JAVA_HOME from lgtoauthc.
    Execute the following script as root to configure lgtoauthc:
        ${AUTHC_PKG_HOME}/scripts/authc_configure.sh
EOF
	return 1
fi

export JAVA_HOME

JSVC_DIR=/opt/nsr/jsvc
export JSVC_DIR

# Check if qpidd is using port 5672 (TCP)
qpiddchk=`netstat -lnpt 2>&1 | grep ":5672 " | grep qpidd`
if [ -n "${qpiddchk}" ]; then
	echo 1>&2 "Error: Unable to use port 5672."
	echo NetWorker requires port 5672 but it is being used by qpidd.
	echo Run \"service qpidd stop\" to stop qpidd before trying to start NetWorker.
	return 1
fi

SQLITE_TMPDIR="/nsr/tmp"
export SQLITE_TMPDIR

return 0
