#!/bin/ksh
# Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
#
# @(#)startwebserver.sh	1.9 05/10/13
#
# This software is the confidential and proprietary information of Sun
# Microsystems, Inc. ("Confidential Information"). You shall not
# disclose such Confidential Information and shall use it only in
# accordance with the terms of the license agreement you entered into
# with Sun.
#
# SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
# THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
# TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
# ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
# DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
#

###############################################
# Defines
###############################################

AWK=/usr/bin/awk
BASENAME=/usr/bin/basename
CAT=/usr/bin/cat
CP=/usr/bin/cp
ECHO=/usr/bin/echo
EXPR=/usr/bin/expr
GREP=/usr/bin/grep
ID=/usr/bin/id
LN=/usr/bin/ln
MV=/usr/bin/mv
NAWK=/usr/bin/nawk
RM=/usr/bin/rm
SED=/usr/bin/sed
UNIQ=/usr/bin/uniq

exitStatus=0
WEB_BASEDIR="`pkginfo -r SUNWwbsvr`"
BELL_CHAR='\a'
#STATE_FILE="/etc/`pkginfo -r SUNWps`/SUNWps/PSConfig.properties"
PS_FILE="PSConfig.properties"
if [ -s "/etc`pkginfo -r SUNWps`/${PS_FILE}" ]; then
    STATE_FILE="/etc`pkginfo -r SUNWps`/PSConfig.properties"
elif [ -s "/etc`pkginfo -r SUNWps`/SUNWps/${PS_FILE}" ]; then
    STATE_FILE="/etc`pkginfo -r SUNWps`/SUNWps/PSConfig.properties"
fi
AM_ROOT="`pkginfo -r SUNWamcon`"

usage() {
    echo 
    echo "usage: $PROGNAME -h"
    echo "       $PROGNAME"
    echo 
    echo "Options:"
    echo "none: Starts the Portal Web Server"
    echo "-h  : prints this message"
    echo
    exit 1
}

get_opts() {
    while getopts :h gopt
    do 
	    case $gopt in
	    h) usage
	        ;;
	    *) usage
	        ;;
	    esac
    done
    shift `$EXPR $OPTIND - 1`
}

###############################################
# Get configuration from file
###############################################
GrabConfig() {
  local FILE=$1
  local KEY=$2
  local SEPARATOR=$3

  ANSWER=`$GREP "^$KEY$SEPARATOR" $FILE | $UNIQ | $SED -e "s/$KEY$SEPARATOR//"`
}

###############################################
# Main
###############################################
PROGNAME=`$BASENAME $0`
get_opts $*

#
# Must be root to perform these functions
#
if [ `$ID | $AWK '{print $1}'` != "uid=0(root)" ]; then
  $ECHO
  $ECHO "You must be root user. $BELL_CHAR"
  $ECHO
  exit 1
fi

#
# Need portal server config properties to perform these functions
#
if [ ! -f $STATE_FILE ]; then
  $ECHO
  $ECHO "Error: $STATE_FILE does not exist. $BELL_CHAR"
  $ECHO
  exit 1
fi

#
# Need portal server config properties to perform these functions
#
GrabConfig $STATE_FILE "IDSAME_BASEDIR" "="
if [ "$ANSWER" != "" ]; then
  IDSAME_BASEDIR=$ANSWER
else
  $ECHO
  $ECHO "Error: Cannot determine IDSAME_BASEDIR. $BELL_CHAR"
  $ECHO
  exit 1
fi

#
# Need portal server config properties to perform these functions
#
GrabConfig $STATE_FILE "BASEDIR" "="
if [ "$ANSWER" != "" ]; then
  PS_BASEDIR=$ANSWER
else
  $ECHO
  $ECHO "Error: Cannot determine BASEDIR. $BELL_CHAR"
  $ECHO
  exit 1
fi

#
# Need portal server config properties to perform these functions
#
GrabConfig $STATE_FILE "DEPLOY_INSTANCE" "="
if [ "$ANSWER" != "" ]; then
  DEPLOY_INSTANCE=$ANSWER
else
  $ECHO
  $ECHO "Error: Cannot determine DEPLOY_INSTANCE. $BELL_CHAR"
  $ECHO
  exit 2
fi

FILE="$IDSAME_BASEDIR/SUNWam/lib/AMConfig.properties"
HOST="$DEPLOY_INSTANCE"

#
# start the Portal Web Servers
#
$ECHO "Starting Web Servers.."
${WEB_BASEDIR}/https-admserv/start > /dev/null 2>&1
exitStatus=$?
if [ $exitStatus = 0 ]; then
    $ECHO "Web Admin Server Started.."
else
    $ECHO "Web Admin Server Start already Started.."
fi
${WEB_BASEDIR}/https-${HOST}/start > /dev/null 2>&1
exitStatus=$?
if [ $exitStatus = 0 ]; then
    $ECHO "Web Server Started.."
else
    $ECHO "Web Server Start already Started.."
fi
exit $exitStatus
