#!/bin/sh

# This script is run by smhstartd when smhstartd is configured by smhconfig.
#
# This script will start hpsmhd server and generate a page that will
# redirect a web browser to the real hpsmhd port.


function replyOK
{
  echo "HTTP/1.1 200 OK"
  echo "Date: $(/bin/date)"
  echo "Server: HPSMH"
  echo "Cache-Control: no-cache"
  echo "Connection: close"
  echo "Content-Type: text/html; charset=iso-8859-1"
  echo
  echo '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">'
  echo '<html>'
  echo '<head>'
  echo "<META HTTP-EQUIV=\"Cache-Control\" CONTENT=\"no-cache\">"
  echo "<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">"
  echo "<META HTTP-EQUIV=\"Expires\" CONTENT=\"0\">"
  echo '<title>System Management Homepage</TITLE>'
  echo "<meta http-equiv=\"REFRESH\" CONTENT=\"0;URL=${1}\">"
  echo '<style>'
  echo '  table.applicationMastheadSmall,table.applicationMastheadSmall td,table.applicationMastheadLarge,table.applicationMastheadLarge td {'
  echo '    color:#fff;'
  echo '           vertical-align:top;'
  echo '  }'
  echo '  table.applicationMastheadSmall a:link,table.applicationMastheadSmall a:visited,table.applicationMastheadSmall a:active,table.applicationMastheadSmall a:hover,'
  echo '  table.applicationMastheadLarge a:link,table.applicationMastheadLarge a:visited,table.applicationMastheadLarge a:active,table.applicationMastheadLarge a:hover{'
  echo '    color:#fff;'
  echo '    text-decoration:underline;'
  echo '  }'
  echo '  table.applicationMastheadSmall,'
  echo '  table.applicationMastheadSmall td,'
  echo '  table.applicationMastheadLarge,'
  echo '  table.applicationMastheadLarge td {'
  echo '        background-color:#036;'
  echo '  }'
  echo '  td.mastheadTitle {'
  echo '           padding-top:8px;'
  echo '           padding-left:9px;'
  echo '  }'
  echo '  table.applicationMastheadLarge td.mastheadTitle {'
  echo '           padding-top:16px;'
  echo '           padding-left:16px;'
  echo '  }'
  echo '  td.mastheadTitle h1{'
  echo '           padding:0px;'
  echo '           margin:0px;'
  echo '           padding-right:16px;'
  echo '           color:#fff;'
  echo '           white-space:nowrap;'
  echo '  }'
  echo '  table.applicationMastheadLarge td.mastheadTitle h1 {'
  echo '           font-size:200%;'
  echo '  }'
  echo '</style>'
  echo '</HEAD>'
  echo
  echo '<BODY><center><font size=4 face=Arial>'
  echo 'System Management Homepage'
  echo '</font></center>'
  echo '<BR><BR><BR><BR>'
  echo "<table class=\"applicationMastheadSmall\" border=0 width=100%><tr>"
  echo '<td align=center colspan=2 width=100%>'
  echo '<h1>Please Wait...'
  echo '<BR>'
  echo 'You will be redirected to the System Management Homepage.'
  echo '</h1></td></tr></table>'
  echo '<BR><BR><BR><BR><BR>'
  echo '<hr><table border=0 width=100%><tr><td align=left class=pageheader>'
  echo '</td><td align=right class=pageheader>&copy; 2004-2010 Hewlett-Packard Development Company, L.P.</td></tr></table>'
  echo '</body>'
  echo '</html>'
}

function replyFAIL
{
  echo "HTTP/1.1 200 OK"
  echo "Date: $(/bin/date)"
  echo "Server: HPSMH"
  echo "Cache-Control: no-cache"
  echo "Connection: close"
  echo "Content-Type: text/html; charset=iso-8859-1"
  echo
  echo '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">'
  echo '<html>'
  echo '<head>'
  echo "<META HTTP-EQUIV=\"Cache-Control\" CONTENT=\"no-cache\">"
  echo "<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">"
  echo "<META HTTP-EQUIV=\"Expires\" CONTENT=\"0\">"
  echo '<title>System Management Homepage</TITLE>'
  echo '<style>'
  echo '  table.applicationMastheadSmall,table.applicationMastheadSmall td,table.applicationMastheadLarge,table.applicationMastheadLarge td {'
  echo '    color:#fff;'
  echo '           vertical-align:top;'
  echo '  }'
  echo '  table.applicationMastheadSmall a:link,table.applicationMastheadSmall a:visited,table.applicationMastheadSmall a:active,table.applicationMastheadSmall a:hover,'
  echo '  table.applicationMastheadLarge a:link,table.applicationMastheadLarge a:visited,table.applicationMastheadLarge a:active,table.applicationMastheadLarge a:hover{'
  echo '    color:#fff;'
  echo '    text-decoration:underline;'
  echo '  }'
  echo '  table.applicationMastheadSmall,'
  echo '  table.applicationMastheadSmall td,'
  echo '  table.applicationMastheadLarge,'
  echo '  table.applicationMastheadLarge td {'
  echo '        background-color:#036;'
  echo '  }'
  echo '  td.mastheadTitle {'
  echo '           padding-top:8px;'
  echo '           padding-left:9px;'
  echo '  }'
  echo '  table.applicationMastheadLarge td.mastheadTitle {'
  echo '           padding-top:16px;'
  echo '           padding-left:16px;'
  echo '  }'
  echo '  td.mastheadTitle h1{'
  echo '           padding:0px;'
  echo '           margin:0px;'
  echo '           padding-right:16px;'
  echo '           color:#fff;'
  echo '           white-space:nowrap;'
  echo '  }'
  echo '  table.applicationMastheadLarge td.mastheadTitle h1 {'
  echo '           font-size:200%;'
  echo '  }'
  echo '</style>'
  echo '</HEAD>'
  echo
  echo '<BODY><center><font size=4 face=Arial>'
  echo 'System Management Homepage'
  echo '</font></center>'
  echo '<BR><BR><BR><BR>'
  echo "<table class=\"applicationMastheadSmall\" border=0 width=100%><tr>"
  echo '<td align=center colspan=2 width=100%>'
  echo '<h1>Error:'
  echo '<BR>'
  echo 'The URL you requested is not a valid System Management Homepage URL.'
  echo '</h1></td></tr></table>'
  echo '<BR><BR><BR><BR><BR>'
  echo '<hr><table border=0 width=100%><tr><td align=left class=pageheader>'
  echo '</td><td align=right class=pageheader>&copy; 2004-2010 Hewlett-Packard Development Company, L.P.</td></tr></table>'
  echo '</body>'
  echo '</html>'
}

function replyFAILServer
{
  echo "HTTP/1.1 200 OK"
  echo "Date: $(/bin/date)"
  echo "Server: HPSMH"
  echo "Cache-Control: no-cache"
  echo "Connection: close"
  echo "Content-Type: text/html; charset=iso-8859-1"
  echo
  echo '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">'
  echo '<html>'
  echo '<head>'
  echo "<META HTTP-EQUIV=\"Cache-Control\" CONTENT=\"no-cache\">"
  echo "<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">"
  echo "<META HTTP-EQUIV=\"Expires\" CONTENT=\"0\">"
  echo '<title>System Management Homepage</TITLE>'
  echo '<style>'
  echo '  table.applicationMastheadSmall,table.applicationMastheadSmall td,table.applicationMastheadLarge,table.applicationMastheadLarge td {'
  echo '    color:#fff;'
  echo '           vertical-align:top;'
  echo '  }'
  echo '  table.applicationMastheadSmall a:link,table.applicationMastheadSmall a:visited,table.applicationMastheadSmall a:active,table.applicationMastheadSmall a:hover,'
  echo '  table.applicationMastheadLarge a:link,table.applicationMastheadLarge a:visited,table.applicationMastheadLarge a:active,table.applicationMastheadLarge a:hover{'
  echo '    color:#fff;'
  echo '    text-decoration:underline;'
  echo '  }'
  echo '  table.applicationMastheadSmall,'
  echo '  table.applicationMastheadSmall td,'
  echo '  table.applicationMastheadLarge,'
  echo '  table.applicationMastheadLarge td {'
  echo '        background-color:#036;'
  echo '  }'
  echo '  td.mastheadTitle {'
  echo '           padding-top:8px;'
  echo '           padding-left:9px;'
  echo '  }'
  echo '  table.applicationMastheadLarge td.mastheadTitle {'
  echo '           padding-top:16px;'
  echo '           padding-left:16px;'
  echo '  }'
  echo '  td.mastheadTitle h1{'
  echo '           padding:0px;'
  echo '           margin:0px;'
  echo '           padding-right:16px;'
  echo '           color:#fff;'
  echo '           white-space:nowrap;'
  echo '  }'
  echo '  table.applicationMastheadLarge td.mastheadTitle h1 {'
  echo '           font-size:200%;'
  echo '  }'
  echo '</style>'
  echo '</HEAD>'
  echo
  echo '<BODY><center><font size=4 face=Arial>'
  echo 'System Management Homepage'
  echo '</font></center>'
  echo '<BR><BR><BR><BR>'
  echo "<table class=\"applicationMastheadSmall\" border=0 width=100%><tr>"
  echo '<td align=center colspan=2 width=100%>'
  echo '<h1>Error:'
  echo '<BR>'
  echo 'The System Management Homepage server could not be started successfully.'
  echo '</h1></td></tr></table>'
  echo '<BR><BR><BR><BR><BR>'
  echo '<hr><table border=0 width=100%><tr><td align=left class=pageheader>'
  echo '</td><td align=right class=pageheader>&copy; 2004-2010 Hewlett-Packard Development Company, L.P.</td></tr></table>'
  echo '</body>'
  echo '</html>'
}
function replyFAILServerRestart
{
  echo "HTTP/1.1 200 OK"
  echo "Date: $(/bin/date)"
  echo "Server: HPSMH"
  echo "Cache-Control: no-cache"
  echo "Connection: close"
  echo "Content-Type: text/html; charset=iso-8859-1"
  echo
  echo '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">'
  echo '<html>'
  echo '<head>'
  echo "<META HTTP-EQUIV=\"Cache-Control\" CONTENT=\"no-cache\">"
  echo "<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">"
  echo "<META HTTP-EQUIV=\"Expires\" CONTENT=\"0\">"
  echo '<title>System Management Homepage</TITLE>'
  echo '<style>'
  echo '  table.applicationMastheadSmall,table.applicationMastheadSmall td,table.applicationMastheadLarge,table.applicationMastheadLarge td {'
  echo '    color:#fff;'
  echo '           vertical-align:top;'
  echo '  }'
  echo '  table.applicationMastheadSmall a:link,table.applicationMastheadSmall a:visited,table.applicationMastheadSmall a:active,table.applicationMastheadSmall a:hover,'
  echo '  table.applicationMastheadLarge a:link,table.applicationMastheadLarge a:visited,table.applicationMastheadLarge a:active,table.applicationMastheadLarge a:hover{'
  echo '    color:#fff;'
  echo '    text-decoration:underline;'
  echo '  }'
  echo '  table.applicationMastheadSmall,'
  echo '  table.applicationMastheadSmall td,'
  echo '  table.applicationMastheadLarge,'
  echo '  table.applicationMastheadLarge td {'
  echo '        background-color:#036;'
  echo '  }'
  echo '  td.mastheadTitle {'
  echo '           padding-top:8px;'
  echo '           padding-left:9px;'
  echo '  }'
  echo '  table.applicationMastheadLarge td.mastheadTitle {'
  echo '           padding-top:16px;'
  echo '           padding-left:16px;'
  echo '  }'
  echo '  td.mastheadTitle h1{'
  echo '           padding:0px;'
  echo '           margin:0px;'
  echo '           padding-right:16px;'
  echo '           color:#fff;'
  echo '           white-space:nowrap;'
  echo '  }'
  echo '  table.applicationMastheadLarge td.mastheadTitle h1 {'
  echo '           font-size:200%;'
  echo '  }'
  echo '</style>'
  echo '</HEAD>'
  echo
  echo '<BODY><center><font size=4 face=Arial>'
  echo 'System Management Homepage'
  echo '</font></center>'
  echo '<BR><BR><BR><BR>'
  echo "<table class=\"applicationMastheadSmall\" border=0 width=100%><tr>"
  echo '<td align=center colspan=2 width=100%>'
  echo '<h1>Error:'
  echo '<BR>'
  echo 'The System Management Homepage server could not be started successfully.'
  echo '<h3>'
  echo 'Restart the hpsmhd from command line..'
  echo '</h3>'
  echo '</h1></td></tr></table>'
  echo '<BR><BR><BR><BR><BR>'
  echo '<hr><table border=0 width=100%><tr><td align=left class=pageheader>'
  echo '</td><td align=right class=pageheader>&copy; 2004-2010 Hewlett-Packard Development Company, L.P.</td></tr></table>'
  echo '</body>'
  echo '</html>'
}


function replyHttpsINUSE
{
  echo "HTTP/1.1 200 OK"
  echo "Date: $(/bin/date)"
  echo "Server: HPSMH"
  echo "Cache-Control: no-cache"
  echo "Connection: close"
  echo "Content-Type: text/html; charset=iso-8859-1"
  echo
  echo '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">'
  echo '<html>'
  echo '<head>'
  echo "<META HTTP-EQUIV=\"Cache-Control\" CONTENT=\"no-cache\">"
  echo "<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">"
  echo "<META HTTP-EQUIV=\"Expires\" CONTENT=\"0\">"
  echo '<title>System Management Homepage</TITLE>'
  echo '<style>'
  echo '  table.applicationMastheadSmall,table.applicationMastheadSmall td,table.applicationMastheadLarge,table.applicationMastheadLarge td {'
  echo '    color:#fff;'
  echo '           vertical-align:top;'
  echo '  }'
  echo '  table.applicationMastheadSmall a:link,table.applicationMastheadSmall a:visited,table.applicationMastheadSmall a:active,table.applicationMastheadSmall a:hover,'
  echo '  table.applicationMastheadLarge a:link,table.applicationMastheadLarge a:visited,table.applicationMastheadLarge a:active,table.applicationMastheadLarge a:hover{'
  echo '    color:#fff;'
  echo '    text-decoration:underline;'
  echo '  }'
  echo '  table.applicationMastheadSmall,'
  echo '  table.applicationMastheadSmall td,'
  echo '  table.applicationMastheadLarge,'
  echo '  table.applicationMastheadLarge td {'
  echo '        background-color:#036;'
  echo '  }'
  echo '  td.mastheadTitle {'
  echo '           padding-top:8px;'
  echo '           padding-left:9px;'
  echo '  }'
  echo '  table.applicationMastheadLarge td.mastheadTitle {'
  echo '           padding-top:16px;'
  echo '           padding-left:16px;'
  echo '  }'
  echo '  td.mastheadTitle h1{'
  echo '           padding:0px;'
  echo '           margin:0px;'
  echo '           padding-right:16px;'
  echo '           color:#fff;'
  echo '           white-space:nowrap;'
  echo '  }'
  echo '  table.applicationMastheadLarge td.mastheadTitle h1 {'
  echo '           font-size:200%;'
  echo '  }'
  echo '</style>'
  echo '</HEAD>'
  echo
  echo '<BODY><center><font size=4 face=Arial>'
  echo 'System Management Homepage'
  echo '</font></center>'
  echo '<BR><BR><BR><BR>'
  echo "<table class=\"applicationMastheadSmall\" border=0 width=100%><tr>"
  echo '<td align=center colspan=2 width=100%>'
  echo '<h1>Error:'
  echo '<BR>'
  echo 'There is another process using the HTTPS port 2381.'
  echo '</h1></td></tr></table>'
  echo '<BR><BR><BR><BR><BR>'
  echo '<hr><table border=0 width=100%><tr><td align=left class=pageheader>'
  echo '</td><td align=right class=pageheader>&copy; 2004-2010 Hewlett-Packard Development Company, L.P.</td></tr></table>'
  echo '</body>'
  echo '</html>'
}

function replyStatus
{
  echo "HTTP/1.1 200 OK"
  echo "Date: $(/bin/date)"
  echo "Server: HPSMH"
  echo "Cache-Control: no-cache"
  echo "Connection: close"
  echo "Content-Type: text/plain; charset=iso-8859-1"
  echo
  echo "SSO=1"
  echo "STE=1"
  echo "CommonHomePage=1"
  echo "SSORestrictAccess=2.0"
  echo "ISAPIExtension=/Proxy"
  echo "Hidden=no"
  echo "Proxy=Yes"
  echo "ROOTUrl=/"
  echo "State=Running"
  echo "HMMOName=Marketingname: HP System Management Homepage"
  echo "HMMOType=0"
  echo "CIMOM=1"
  echo "HTTPServer=CompaqHTTPServer/99.99  (HP System Management Homepage v2.2.9)"
}

unset DISPLAY
unset LANG

# read requested url
# if none exit silently
read A URL B
if [ "$A" != "GET" -o -z "$URL" -o -z "$B" ]
then
    exit 1
fi
CGI="${URL%%[?]*}"

# see if URL is /proxy/xestatusreport

if [ "$URL" = "/Proxy/XEStatusReport" -o \
     "$URL" = "/proxy/xestatusreport" ]
then
    replyStatus
    exit 1
fi

# see if URL exists
# if not reply, but don't start the server
if [ ! -a "/opt/hp/hpsmh/data/htdocs/$CGI" -a "$URL" != "/" -a \
     ! -a "/opt/hp/hpsmh/data/$CGI" -a \
     ! -a "/opt/hp/hpsmh/tomcat/webapps/$CGI" ]
then
    replyFAIL
    exit 1
fi

# Start the HPSMH server...

AUTOSTART_SMH=`cat /opt/hp/hpsmh/conf/smhpd.xml | grep -i "autostart"| sed  's/<[^<>]*>//' | cut -d "<" -f 1`
if [ -n "$AUTOSTART_SMH" ] && [ $AUTOSTART_SMH == "false" ]
then
     replyFAILServerRestart
     exit 1
fi

/etc/init.d/hpsmhd stop  > /dev/null 2> /dev/null
/etc/init.d/hpsmhd autostart  > /dev/null 2> /dev/null


if [ $? -ne 0 ]
then
    #
    # HPSMH fails to start
    # Verify if the https port is in use by another process
    #
    INUSE=$(/bin/netstat -a | /bin/grep 2381 | /bin/grep LISTEN )
    if [ "$INUSE" = "" ]
    then
        replyFAILServer
    else
        replyHttpsINUSE
    fi
    exit 1
else
    read key value
    while [ "$key" != "Host:" ]
    do
        read key value
    done

    #
    # get the server's IP Address/hostname.
    # for hostname and IPV4 address, SERVER_FQDN contains port number also.
    # for IPV4 address, SERVER_FQDN will contain only IP address.
    #
    SERVER_FQDN=`echo $value | cut -f 2 -d [ | cut -f 1 -d ]`

    #
    #check if the host name/IP address contains ':'.
    #this is a check for the IPV6 address.
    #
    COLONCOUNT=`echo $SERVER_FQDN | awk -F\: '{print NF-1}'`
    if [ $COLONCOUNT -ne 1 ]
    then
        #
        #add open and closed Square brackets if it is an IPV6 address.
        #
        SERVER_FQDN1=`echo $SERVER_FQDN | sed 's/^/[/' | sed 's/$/]/'`
    else
        #
        #remove the port number from hostname/IPV4 address..
        #
        FQDN2=`echo $SERVER_FQDN | cut -f 1 -d :`
        SERVER_FQDN1=`echo $FQDN2`
    fi

    #
    # The url is the fully qualified domain name, the port, and the url
    #
    PORT=2381
    HTTP=https
    FULLURL="${HTTP}://${SERVER_FQDN1}:${PORT}${URL}"

    replyOK "$FULLURL"
fi

exit 0

