#!/bin/ksh

# fiper4websphere.sh: Create, Configure, or Deploy EAR/WAR files to WebSphere

usage() {
    cat <<!
Usage: $(basename "$script") PropFile Option
  Create, Configure, or Deploy EAR/WAR files to WebSphere.

  PropFile - property file that contains SEE server configuration.
          Edit a copy of file fiper4all.prop in this directory to crete.

  Option - The option to perform:
     create  - Create the WebSphere profile
     config  - Configure WebSphere
     deploy  - Deploy EAR and (optionally) WAR files.
     all     - default - same as 'deploy'

   The following options are standlone commands:

     start   - Start WebSphere if it is not running.
     stop    - Stop WebSphere if it is running.
     restart - Restart (stop/start) Websphere.
!
}

# Load useful shell functions.  Location is always relative to this file.
script="$0"
shell_dir=$(dirname "$script")
. "$shell_dir/fiper-functions.sh"

##########################################################################
# Get command-line options.
##########################################################################

propfile=$(makeAbsolute "$1")
operation=$2

if [ ! "$propfile" -o ! "$operation" ]; then
    usage
    exit 1
fi
 
case $operation in
    create) op_create=1 ;;
    config) op_config=1 ;;
    deploy) op_deploy=1 ;;
    restart)  op_restart=1 ;;
    start)  op_start=1 ;;
    stop)   op_stop=1 ;;
    all)
        # Universal default.  Currently same as deploy
        op_deploy=1
        ;;
    help)
        usage
        exit 0;
        ;;
    *)
        err "Unknown operation: $operation."
        usage
        exit 1
        ;;
esac

# Cascade features
if [ "$op_create" ]; then
    op_config=1
fi
if [ "$op_config" ]; then
    op_deploy=1
fi
msg "WebSphere operations create=$op_create config=$op_config deploy=$op_deploy stop=$op_stop start=$op_start restart=$op_restart"

# Default (and normalize) FIPER_HOME and other variables.
export FIPER_HOME=$(cannonPath ${FIPER_HOME:-$shell_dir/../../..})

# Read the properties file
msg "Reading property file '$propfile'"
. "$propfile" || fail "Error reading properties file $propfile"

##########################################################################
# Check variable settings from properties file
##########################################################################

# normalize (but don't set) some variables
WAS_HOME=$(stdPath "$WAS_HOME")
FIPER_FILEMGR=$(stdPath "$FIPER_FILEMGR")

# Check required variables.
checkVar FIPER_HOME reffiles/SMAFIPserver/websphere/deploy # Location of RTV and deploy scripts.
checkVar FIPER_FILEMGR .    # File Manager directory.  Must exist.
checkVar FIPER_TEMP .       # ACS 'temporary' directory.  Must NOT be automatically cleaned.  Must exist.
checkVar WAS_HOME bin/startServer$SCRIPT    # Home directory of WebSphere install
checkVar SERVER_NAME        # Name of the server, used for cpr file.
checkVar SERVER_HOST        # Domain name of WebSphere, or 'localhost' if only local usage.
checkVar ACS_SECURITY       # ACS Security setting 1=none, 2=server, 3=runas
checkVar DB_TYPE            # Database type, DB2 or ORACLE
checkVar FIPER_FILEMGR      # Location of Fiper file manager files
checkVar WAS_PROFILE        # WebSphere profile name
checkVar SEE_USER           # User who is admin to DB and to WebSphere
checkVar SEE_USER_PW        # password for SEE_USER
checkVar DB_USER            # User who owns Oracle or DB2 schema/database.
checkVar DB_USER_PW         # password for DB_USER
checkVar tmp4all   .        # Make sure temp directory exists.
defaultVar FIPER_CONF "${FIPER_HOME}/../config"  # Location of configuration files
defaultVar LOG_DIR "${tmp4all}/fiper4all"  # directory for log files
# Following variables are turned off by NOT setting them, so cannot checkVar
#  WEBTOP_INSTALL       # set to install webtop, unset not to
#  WEBDASH_INSTALL      # set to install web dashboard, unset not to
#  B2B_INSTALL          # Set to install B2B, unset not to.

checkErr

# Directory containing deployment scripts.
SERVER_DIR="$FIPER_HOME/reffiles/SMAFIPserver/websphere"
DEPLOY_DIR="$SERVER_DIR/deploy"
CLIENT_DIR="$FIPER_HOME/reffiles/ejbclient/websphere"

# More complex verification
if [ "$ACS_SECURITY" != '1' -a "$ACS_SECURITY" != '2' -a "$ACS_SECURITY" != '3' ]; then
    err "ACS_SECURITY=$ACS_SECURITY.  Should be '1', '2', or '3'."
fi
if [ "$ACS_SECURITY" != '1' ]; then
    warn "TODO ACS Security level $ACS_SECURITY is not implemented yet."
fi

if [ "$op_deploy" ]; then
   if [ ! -f "$SERVER_DIR/fiper.ear" ]; then
       err "fiper.ear not found in directory $SERVER_DIR.  You must run 'fiper4all build' in a development workspace to build it."
   fi
   if [ "$WEBTOP_INSTALL" -a ! -f "$SERVER_DIR/webtop.war" ]; then
       err "webtop.war not found in directory $SERVER_DIR"
   fi
   if [ "$WEBDASH_INSTALL" -a ! -f "$SERVER_DIR/webdashboard.war" ]; then
       err "webdashboard.war not found in directory $SERVER_DIR"
   fi
   if [ "$B2B_INSTALL" -a ! -f "$SERVER_DIR/federation.ear" ]; then
       err "federation.ear not found in directory $SERVER_DIR"
   fi
fi

DB_TYPE=$(upcase "$DB_TYPE")
if [ "$DB_TYPE" = "DB2" ]; then
    # DB2 requires host and port
    checkVar DB2_SCHEMA      # DB schema name for DB2 or ORACLE
    checkVar DB2_HOST       # Host running DB2.  May be 'localhost' if same as WebSphere host.
    checkVar DB2_PORT       # Port, usually 50000 for Windows, 50001 for Unix.
elif [ "$DB_TYPE" = "ORACLE" ]; then
    if [ ! "$ORACLE_HOME" ]; then
        # Try to default ORACLE_HOME to parent of directory containing sqlplus.exe
        oracmd=$(which sqlplus$CMD)
        if [ $? -eq 0 ]; then
            ORACLE_HOME=$(cannonPath $(dirname "$oracmd")/..)
        fi
    fi
    checkVar ORACLE_HOME jdbc/lib/ojdbc6.jar  # Oracle, required for JDBC driver.
    checkVar ORACLE_SID     # Connect string for Oracle server
    checkVar ORACLE_HOST    # Host of Oracle database
    checkVar ORACLE_PORT    # Port for Oracle database
else
    err "DB_TYPE=$DB_TYPE.  Should be 'DB2' or 'ORACLE'"
fi

if [ ! -d "$LOG_DIR" ]; then
    mkdir -p "$LOG_DIR" || err "Unable to create log directory $LOG_DIR"
fi

checkErr

# Figure out architecture in FIPER_HOME.
OS=$(basename "$FIPER_HOME")

##########################################################################
# Check version of WebSphere
##########################################################################

msg "Checking WebSphere version"
log="$LOG_DIR/wasVersion.txt"
cmd "'$WAS_HOME/bin/versionInfo$SCRIPT'" "$log"
version=$(grep 'Version      ' "$log")
version=$(echo ${version#Version})     # Remove word Version from front and extra spaces.
if [ "$version" = '' ]; then
    fail "Unable to determine the version of WebSphere installed in $WAS_HOME"
elif expr "$version" : '8\.5\.[0-9]*\.[0-9]*'; then
    note "WebSphere has expected version:  $version"
else
    warn "WebSphere has unexpected version.  Expected 8.5.0.0 or higher, found '$version'".
fi
wasArch=$(grep 'Architecture   ' "$log")
wasArch=$(echo ${wasArch#Architecture})     # Remove word Architecture and extra spaces
if [ "$wasArch" = 'Intel (32 bit)' ]; then
    if [ "$OS" = 'intel_a' ]; then
        msg "WebSphere architecture '$wasArch' matches SEE architecture '$OS'"
    elif [ "$OS" = 'win_b64' ]; then
        fail "WebSphere is 32-bit but SEE is 64 bit.  This won't work."
    fi
elif [ "$wasArch" = 'Intel (64 bit)' ]; then
    if [ "$OS" = 'intel_a' ]; then
        fail "WebSphere is 64-bit but SEE is 32 bit.  This won't work."
    elif [ "$OS" = 'win_b64' ]; then
        msg "WebSphere architecture '$wasArch' matches SEE architecture '$OS'"
    fi
else
    warn "Unrecognized WebSphere architecture '$wasArch'.  Continuing, but it probably won't work."
fi

##########################################################################
# Check to see if profile exists and if server is running.
##########################################################################

profile_exists=''
server_running=''
config_exists='unknown'
if [ -d "$WAS_HOME/profiles/$WAS_PROFILE/bin" ]; then
    msg "Profile $WAS_PROFILE exists."
    profile_exists=1

    # Now check if server is running.  wsadmin does NOT produce useful exit code.
    # Note use of -c with empty command to just check connection.
    log="$LOG_DIR/wascheck.log"
    cmd "'$WAS_HOME/bin/wsadmin$SCRIPT' -profileName '$WAS_PROFILE' -user '$SEE_USER' -password '$SEE_USER_PW' -f '$DEPLOY_DIR/wasTestConfig.py'" "$log"
    if grep 'Connected to process' "$log" >$NUL; then
        server_running=1
        msg "WebSphere server $WAS_PROFILE is running."
        if grep 'CONFIG EXISTS' "$log" >$NUL; then
            config_exists=1
        elif grep 'PARTLY CONFIGURED' "$log" >$NUL; then
            config_exists='part'
        elif grep 'NOT CONFIGURED' "$log" >$NUL; then
            config_exists=''
        fi
    else
        msg "WebSphere server $WAS_PROFILE is not running."
        # leave config_exists as unknown.
    fi
else
    # No profile means no config.
    config_exists=''
    msg "Profile $WAS_PROFILE does not exist."
fi

##########################################################################
# Request to stop (or restart) server and nothing else.
##########################################################################

if [ "$op_stop" -o "$op_restart" ]; then
    if [ "$server_running" ]; then
        # Must stop server
        note "Stopping WebSphere server $WAS_PROFILE"
        log="$LOG_DIR/wasstop.log"
        cmd "'$WAS_HOME/bin/stopServer$SCRIPT' server1 -profileName '$WAS_PROFILE' -username '$SEE_USER' -password '$SEE_USER_PW'" "$log"
        if grep 'operation requires elevation' $log >$NUL; then
            fail "Unable to stop WebSphere.  Must run script as Administrator on Windows-7"
        elif grep 'cannot be reached' $log >$NUL; then
            warn "Warning: WebSphere is up but not running??????  Continuing, but there may be errors."
        elif grep 'stop completed' $log >$NUL; then
            msg "WebSphere stop was successful."
        else
            cat "$log"
            fail "Unable to stop WebSphere.  For details see: $log"
        fi
    else
        # Server not running.  Note that and quit.
        note "WebSphere is not running."
    fi
    #Allow restart to fall through to next section
    if [ "$op_restart" ]; then
        server_running=''
    else
        exit 0
    fi
fi

##########################################################################
# Request to start (or restart) server and nothing else.
##########################################################################

if [ "$op_start" -o "$op_restart" ]; then
    if [ ! "$server_running" ]; then
        note "Starting WebSphere server $WAS_PROFILE"
        log="$LOG_DIR/wasstart.log"
        cmd "'$WAS_HOME/bin/startServer$SCRIPT' server1 -profileName '$WAS_PROFILE'" "$log"
        if grep 'operation requires elevation' $log >$NUL; then
            fail "Unable to Start WebSphere.  Must run script as Administrator on Windows-7"
        elif grep 'may already be running' $log >$NUL; then
            warn "Warning: WebSphere is running but not connecting????? Strange."
        elif grep 'open for e-business' $log >$NUL; then
            msg "WebSphere start was successful."
        else
            cat "$log"
            fail "Unable to start WebSphere.  For details see: $log"
        fi
    fi
    exit 0
fi

##########################################################################

# Cannot continue if partly configured but 'create' not passed.
if [ "$config_exists" = 'part' -a ! "$op_create" ]; then
    fail "WebSphere is partly configured.  Cannot continue.  Re-run fiper4all with argument 'create,nodb'"
fi

##########################################################################
# Remove profile if told to
##########################################################################

if [ "$op_create" -a "$profile_exists" ]; then

    if [ "$server_running" ]; then
        note "Stopping WebSphere server $WAS_PROFILE"
        log="$LOG_DIR/wasstop.log"
        cmd "'$WAS_HOME/bin/stopServer$SCRIPT' server1 -profileName '$WAS_PROFILE' -username '$SEE_USER' -password '$SEE_USER_PW'" "$log"
        if grep 'operation requires elevation' $log >$NUL; then
            fail "Unable to stop WebSphere.  Must run script as Administrator on Windows-7"
        elif grep 'cannot be reached' $log >$NUL; then
            warn "Warning: WebSphere is up but not running??????  Continuing, but there may be errors."
        elif grep 'stop completed' $log >$NUL; then
            msg "WebSphere stop was successful."
        else
            cat "$log"
            fail "Unable to stop WebSphere.  For details see: $log"
        fi
        server_running=''
    fi

    note "Removing WebSphere profile $WAS_PROFILE."
    cmd "'$WAS_HOME/bin/manageprofiles$SCRIPT' -delete -profileName '$WAS_PROFILE'" "$LOG_DIR/wasdelete.log"
    if [ $? != 0 ]; then
        fail "Unable to remove WebSphere profile.  See log $LOG_DIR/wasdelete.log"
    fi
    sleep 2
    cmd "rm -rf '$WAS_HOME/profiles/$WAS_PROFILE'" "$LOG_DIR/wasrm1.log"
    sleep 1
    cmd "rm -rf '$WAS_HOME/profiles/$WAS_PROFILE'" "$LOG_DIR/wasrm2.log"
    if [ $? -ne 0 ]; then
        fail "Unable to clean up WebSphere profile.  Remove directory $WAS_HOME/profiles/$WAS_PROFILE and re-run fiper4all."
    fi
    profile_exists=''
    config_exists=''
fi

##########################################################################
# Create profile if needed
##########################################################################

if [ ! "$profile_exists" ]; then
    note "Creating WebSphere profile $WAS_PROFILE"
    profArg="-profileName $WAS_PROFILE -profilePath '$WAS_HOME/profiles/$WAS_PROFILE' -templatePath '$WAS_HOME/profileTemplates/default' -winserviceCheck false"
    if [ $ACS_SECURITY -gt 1 ]; then
        adminArg="-enableAdminSecurity true -adminUserName '$SEE_USER' -adminPassword '$SEE_USER_PW'"
    else
        adminArg=""
    fi

    cmd "'$WAS_HOME/bin/manageProfiles$SCRIPT' -create $profArg $adminArg" "$LOG_DIR/wascreate.log"
    if [ $? != 0 ]; then
        fail "Could not create WebSphere profile.  See log $LOG_DIR/wascreate.log"
    fi

    config_exists=''
fi

##########################################################################
# Start server if not running
##########################################################################

if [ ! "$server_running" ]; then
    note "Starting WebSphere server $WAS_PROFILE"
    log="$LOG_DIR/wasstart.log"
    cmd "'$WAS_HOME/bin/startServer$SCRIPT' server1 -profileName '$WAS_PROFILE'" "$log"
    if grep 'operation requires elevation' $log >$NUL; then
        fail "Unable to Start WebSphere.  Must run script as Administrator on Windows-7"
    elif grep 'may already be running' $log >$NUL; then
        warn "Warning: WebSphere is running but not connecting????? Strange."
    elif grep 'open for e-business' $log >$NUL; then
        msg "WebSphere start was successful."
    else
        cat "$log"
        fail "Unable to start WebSphere.  For details see: $log"
    fi
    server_running=1
fi

##########################################################################
# If created configuration, must download security certificate.
##########################################################################

if [ "$op_create" -o ! "$profile_exists" ]; then
    clientTrustFile="$CLIENT_DIR/ClientTrustFile.jks"
    # Make ClientTrustFile writable.  The 'chmod' works on Windows also!
    chmod +w "$clientTrustFile"
    
    # Retrieve the signing certificate
    ### Can't user retrieveSigners as the local store is not a standard client!
    ### So I'll have to live with just making it writable
    #cmd "'$WAS_HOME/bin/retrieveSigners$SCRIPT' 

    ###TODO or copy AppServer/profiles/$WAS_PROFILE/etc/DummyClientTrustFile.jks
    ###  to $FIPER_HOME/reffiles/ejbclient/websphere/ClientTrustFile.jks
fi

##########################################################################
# Check if server is configured if we didn't detect that above
##########################################################################

if [ "$config_exists" = 'unknown' ]; then
    # Check configuration now that server is running.
    note "Checking if WebSphere is configured for SEE."
    log="$LOG_DIR/wascheck2.log"
    cmd "'$WAS_HOME/bin/wsadmin$SCRIPT' -profileName '$WAS_PROFILE' -user '$SEE_USER' -password '$SEE_USER_PW' -f '$DEPLOY_DIR/wasTestConfig.py'" "$log"
    if grep 'Connected to process' "$log" >$NUL; then
        if grep 'CONFIG EXISTS' "$log" >$NUL; then
            config_exists=1
        elif grep 'PARTLY CONFIGURED' "$log" >$NUL; then
            config_exists='part'
        elif grep 'NOT CONFIGURED' "$log" >$NUL; then
            config_exists=''
        else
            fail "Error checking if WebSphere is configured.  See log $log"
        fi
    else
        fail "Cannot connect to websphere.  See log $log"
    fi
fi

# Check for partial configuration
if [ "$config_exists" = 'part' ]; then
    fail "WebSphere is partly configured.  Must re-configure by running fiper4all with 'create,nodb' argument"
fi

##########################################################################
### Configure WebSphere
##########################################################################

# Write out Python assignments for required properties.
# These are needed by both config and deploy, so do it here.
# Note that WebSphere uses the SEE user name to log in to the database
propfile="$LOG_DIR/wasproperties.py"
cat <<!EOF >$propfile
# Configuration settings for WebSphereScript.py
# Format is 'key = value'.  Spaces are ignored.
FIPER_HOME  = $FIPER_HOME
FIPER_CONF  = $FIPER_CONF
BSF_ROOT    = $BSF_ROOT
DB_USER     = $DB_USER
DB_USER_PW  = $DB_USER_PW
DB_TYPE     = $DB_TYPE
DB2_SCHEMA  = $DB2_SCHEMA
DB2_HOST    = $DB2_HOST
DB2_PORT    = $DB2_PORT
SEE_USER    = $SEE_USER
SEE_USER_PW = $SEE_USER_PW
WAS_HOST    = $SERVER_HOST
SERVER_NAME = $SERVER_NAME
ORACLE_HOME = $ORACLE_HOME
ORACLE_SID  = $ORACLE_SID
ORACLE_HOST = $ORACLE_HOST
ORACLE_PORT = $ORACLE_PORT
WEBTOP_INSTALL= $WEBTOP_INSTALL
WEBDASH_INSTALL= $WEBDASH_INSTALL
B2B_INSTALL = $B2B_INSTALL
ACS_SECURITY= $ACS_SECURITY
!EOF


# Configure server if needed. 
if [ "$config_exists" = '' ]; then

    note "Create CPR file $SERVER_NAME.cpr"

    profileInfo="$WAS_HOME/profiles/$WAS_PROFILE/logs/AboutThisProfile.txt"
    a=$(grep 'Bootstrap port' "$profileInfo")
    if [ $? -ne 0 ]; then
        err "Unable to get Bootstrap Port from file $profileInfo"
    fi
    bootstrapPort=${a#*: }
    cprFile="${FIPER_CONF}/$SERVER_NAME.cpr"
    cat <<!EOF >"$cprFile"
# Fiper Connection Profile for server $SERVER_NAME
server=$SERVER_HOST
port=$bootstrapPort
Sys_ServerSupportDir=websphere
Sys_LibraryType=shared
Sys_ProfileDescription=$SERVER_NAME
Sys_LocalLibraryDir=
!EOF
    if [ ! -r "$cprFile" ]; then
        err "Unable to create CPR file $cprFile"
    fi

    confSrc="$FIPER_HOME/reffiles/SMAFIPconfig"
    acsPropFile="$FIPER_CONF/acs-$SERVER_NAME.properties"
    note "Creating ACS Properties File $acsPropFile"
    sedFile="$LOG_DIR/sedFileAcs"
    echo "# Sed commands to update $acsPropFile" >$sedFile
    sedProp() {
        p=$1
        v=$2
        echo "s|#* *$p *=.*|$p = $v|" >> $sedFile
    }
    # These are no longer specified in properties file, only in server JVM parms
    #sedProp fiper.system.esiHome "$FIPER_HOME"
    #sedProp fiper.system.configdir "$FIPER_CONF"
    sedProp fiper.system.filemgr.rootFilePath "$FIPER_FILEMGR"
    sedProp fiper.system.temp "$FIPER_TEMP"
    #TODO rest are not done yet
    #sedProp fiper.system.drm fiper
    #sedProp fiper.security.runas.enabled true
    #sedProp fiper.security.runas.domain $runasDomain
    #sedProp fiper.sce.url "$SCE_URL"
    msg "Cmd: sed -f '$sedFile' '$confSrc/acs.properties' >'$acsPropFile'"
    sed -f "$sedFile" "$confSrc/acs.properties" >"$acsPropFile"
    if [ $? -ne 0 ]; then
        fail "Unable to update ACS properties file $acsPropFile"
    fi

    webtopPropFile="$FIPER_CONF/webtop-$SERVER_NAME.properties"
    note "Creating WebTop Properties File $webtopPropFile"
    sedFile="$LOG_DIR/sedFileWeb"
    echo "# Sed commands to update $webtopPropFile" >$sedFile
    sedProp fiper.webtop.acs.cprfile "$cprFile"
    #sedProp fiper.system.esihome "$FIPER_HOME"
    #sedProp fiper.system.configdir "$FIPER_CONF"
    msg "Cmd: sed -f '$sedFile' '$confSrc/webtop.properties' >'$webtopPropFile'"
    sed -f "$sedFile" "$confSrc/webtop.properties" >"$webtopPropFile"
    if [ $? -ne 0 ]; then
        fail "Unable to update WebTop properties file $webtopPropFile"
    fi

    webdashPropFile="$FIPER_CONF/webdashboard-$SERVER_NAME.properties"
    note "Creating Web Dashboard Properties File $webdashPropFile"
    sedFile="$LOG_DIR/sedFileDash"
    echo "# Sed commands to update $webdashPropFile" >$sedFile
    sedProp fiper.webdashboard.acs.cprfile "$cprFile"
    #sedProp fiper.system.esihome "$FIPER_HOME"
    #sedProp fiper.system.configdir "$FIPER_CONF"
    msg "Cmd: sed -f '$sedFile' '$confSrc/webdashboard.properties' >'$webdashPropFile'"
    sed -f "$sedFile" "$confSrc/webdashboard.properties" >"$webdashPropFile"
    if [ $? -ne 0 ]; then
        fail "Unable to update Web Dashboard properties file $webdashPropFile"
    fi

    note "Configuring websphere server $WAS_PROFILE."

    wsadminArgs="-lang jython -profileName $WAS_PROFILE -conntype SOAP -user '$SEE_USER' -password '$SEE_USER_PW'"
    scriptArgs="-profile '$DEPLOY_DIR/WebSphereConfigProcs.py' -f '$DEPLOY_DIR/WebSphereScript.py'"
    log="$LOG_DIR/wsadmin.log"
    cmd "'$WAS_HOME/bin/wsadmin' $wsadminArgs $scriptArgs '$propfile'" "$log"
    if [ $? != 0 ]; then
        fail "wsadmin failed.  See log file $log"
    fi

    # Stop and restart WebSphere.
    note "Restarting WebSphere so all changes take effect."
    log="$LOG_DIR/wasrestart.log"
    cmd "'$WAS_HOME/bin/stopServer$SCRIPT' server1 -profileName '$WAS_PROFILE' -username '$SEE_USER' -password '$SEE_USER_PW'" "$log"
    if grep 'operation requires elevation' $log >$NUL; then
        fail "Unable to stop WebSphere.  Stop and restart websphere and re-run script."
    elif grep 'cannot be reached' $log >$NUL; then
        warn "Warning: WebSphere is up but not running??????  Continuing, but there may be errors."
    elif grep 'stop completed' $log >$NUL; then
        msg "WebSphere stop was successful."
    else
        fail "Unable to stop WebSphere.  For details see: $log"
    fi
    cmd "'$WAS_HOME/bin/startServer$SCRIPT' server1 -profileName '$WAS_PROFILE'" "$log"
    if grep 'operation requires elevation' $log >$NUL; then
        fail "Unable to Start WebSphere.  Must run script as Administrator on Windows-7"
    elif grep 'may already be running' $log >$NUL; then
        warn "Warning: WebSphere is running but not connecting????? Strange."
    elif grep 'open for e-business' $log >$NUL; then
        msg "WebSphere start was successful."
    else
        cat "$log"
        fail "Unable to stop WebSphere.  For details see: $log"
    fi
fi

##########################################################################
# Final step - deploy EAR and/or WARs
##########################################################################

if [ "$op_deploy" ]; then
    note "Deploying FIPER to websphere server $WAS_PROFILE."

    wsadminArgs="-lang jython -profileName $WAS_PROFILE -conntype SOAP -user '$SEE_USER' -password '$SEE_USER_PW'"
    scriptArgs="-profile '$DEPLOY_DIR/WebSphereConfigProcs.py' -f '$DEPLOY_DIR/WebSphereDeploy.py'"
    log="$LOG_DIR/wsdeploy.log"
    cmd "'$WAS_HOME/bin/wsadmin' $wsadminArgs $scriptArgs '$propfile'" "$log"
    if [ $? != 0 ]; then
        fail "Deploy failed.  See log file $log"
    fi
fi
