#!/usr/bin/perl
#
# status_buttons.pl - Steven Lerner 3/2005
#                     Woods Hole Oceanographic Institution
#
#       Description: Displays mars status_buttons
#
#       Usage: status_buttons.pl 
#
#	Output:  html form to stdout
#
# History:
#       Date       Who      Description
#       ----       ---      ---------------------------------------------------
#       04/2005     SL      Create
#       02/2007     SL      Added marsdcslib.pl and use marsdcs.cfg
#	02/2007     ARM     Added wiki link
###############################################################################
require "flush.pl";
require "marsdcslib.pl";
%Cfg = &load_cfgfile("marsdcs.cfg");

print "Content-Type: text/html\n\n";
print "<HTML>\n<HEAD>\n";
print "<TITLE>Mars DCS Status Buttons</TITLE>\n";
print "<!--\n";
print "  (c)2005 Deep Submergence Laboratory\n";
print "   Woods Hole Oceanographic Institution\n";
print "//-->\n";

$sdir = "$Cfg{'ScriptDir'}/Status";

print <<"EndHTML";
<style type="text/css">

#dhtmltooltip{
position: absolute;
width: 150px;
border: 2px solid black;
padding: 2px;
background-color: lightyellow;
visibility: hidden;
z-index: 100;
/*Remove below line to remove shadow. Below line should always appear last within this CSS*/
filter: progid:DXImageTransform.Microsoft.Shadow(color=gray,direction=135);
}

</style>
EndHTML

$timestamp = gmtimestamp();

$refresh_rate = 30; #seconds
$fnt = "<font face=sans-serif color=white size=2><b>";

#===================================================================
#Get status to set colors and display latest status
#
# EthChan Traffic button
$ethchan_status = `$sdir/ethchan_status.pl`; chomp($ethchan_status);
if ($ethchan_status =~ /^1/) {$ethchan_color = "#00aa00";}
                        else {$ethchan_color = "red";
                              $ethchan_status_msg = "onMouseOver=\"ddrivetip(\'$ethchan_status\','yellow',500)\"; onMouseout=\"hideddrivetip()\"";
                             }
# Science Traffic button
$scitraf_status = `$sdir/scitraf_status.pl`; chomp($scitraf_status);
if ($scitraf_status =~ /^1/) {$scitraf_color = "#00aa00";}
                        else {$scitraf_color = "red";
                              $scitraf_status_msg = "onMouseOver=\"ddrivetip(\'$scitraf_status\','yellow',500)\"; onMouseout=\"hideddrivetip()\"";
                             }
# Gyda button
$gyda_status = `$sdir/gyda_status.pl`; chomp($gyda_status);
if ($gyda_status =~ /^1/)  {$gyda_color = "#00aa00";}
                      else {$gyda_color = "red";
                            $gyda_status_msg = "onMouseOver=\"ddrivetip(\'$gyda_status\','yellow',500)\"; onMouseout=\"hideddrivetip()\"";
                             }
# Node EDFA button
$node_edfa_status = `$sdir/node_edfa_status.pl`; chomp($node_edfa_status);
if ($node_edfa_status =~ /^1/)  {$node_edfa_color = "#00aa00";}
                      else {$node_edfa_color = "red";
                            $node_edfa_status_msg = "onMouseOver=\"ddrivetip(\'$node_edfa_status\','yellow',500)\"; onMouseout=\"hideddrivetip()\"";
                             }
# Shore EDFA button
$shore_edfa_status = `$sdir/shore_edfa_status.pl`; chomp($shore_edfa_status);
if ($shore_edfa_status =~ /^1/)  {$shore_edfa_color = "#00aa00";}
                      else {$shore_edfa_color = "red";
                            $shore_edfa_status_msg = "onMouseOver=\"ddrivetip(\'$shore_edfa_status\','yellow',500)\"; onMouseout=\"hideddrivetip()\"";
                             }
#
# Commenting out power status and temperature status until they can be modified to use data from pmacs console
#
# Power Status button
#$power_status = `$sdir/power_status.pl`; chomp($power_status);
#if ($power_status =~ /^1/)  {$power_color = "#00aa00";}
#                       else {$power_color = "red";
#                             $power_status_msg = "onMouseOver=\"ddrivetip(\'$power_status\','yellow',500)\"; onMouseout=\"hideddrivetip()\"";
#                             }
# Temperature button
#$temperature_status = `$sdir/temperature_status.pl`; chomp($temperature_status);
#if ($temperature_status =~ /^1/)  {$temperature_color = "#00aa00";}
#                       else {$temperature_color = "red";
#                             $temperature_status_msg = "onMouseOver=\"ddrivetip(\'$temperature_status\','yellow',500)\"; onMouseout=\"hideddrivetip()\"";
#                             }

# Timing button
$timing_status = `$sdir/timing_status.pl`; chomp($timing_status);
if ($timing_status =~ /^1/) {$timing_color = "#00aa00";}
                         else {$timing_color = "red";
                              $timing_status_msg = "onMouseOver=\"ddrivetip(\'$timing_status\','yellow',500)\"; onMouseout=\"hideddrivetip()\"";
                              }

# NTP button
$ntp_status = `$sdir/ntp_status.pl`; chomp($ntp_status);
if ($ntp_status =~ /^1/) {$ntp_color = "#00aa00";}
                         else {$ntp_color = "red";
                              $ntp_status_msg = "onMouseOver=\"ddrivetip(\'$ntp_status\','yellow',500)\"; onMouseout=\"hideddrivetip()\"";
                              }

#===================================================================

$fnt1 = "<font face=sans-serif size=2 color=white><b>";
print <<"EndHTML";

<meta content=15;url="/mars-bin/mars_status_buttons.pl" http-equiv="refresh">
</head>
<body bgcolor=4488aa>
<center>
   <table>
<table>
   <th align=left><font color=white size=+1>Real Time Status as of</font><br> 
                 <font color=white size=+1><b>$timestamp</b> </font></th> 
   <td>
     <table>
      <td bgcolor=$ethchan_color><a href=/mars-bin/display_ethchanplot.pl target=DispEthWin $ethchan_status_msg>$fnt1 EthChan Traffic</a> &nbsp
      <td bgcolor=$scitraf_color><a href=/mars-bin/display_sciencetraf.pl target=DispSciWin $scitraf_status_msg>$fnt1 Science Traffic</a> &nbsp
      <td bgcolor=$ntp_color><a href=/mars-bin/display_ntp_status.pl target=NTPStaWin $ntp_status_msg>$fnt1 NTP</a> &nbsp
</tr><tr>
      <td bgcolor=$power_color><a href=/mars-bin/display_powerdat.pl target="DispPowWin" $power_status_msg>$fnt Power Status</a> &nbsp
      <td bgcolor=$temperature_color><a href=/mars-bin/display_temperature.pl target="DispTempWin" $temperature_status_msg>$fnt Temperature</a> &nbsp
      <td bgcolor=$gyda_color><a href=/mars-bin/display_gyda.pl target=GYDAWin $gyda_status_msg>$fnt1 Gyda</a> &nbsp
      <td bgcolor=$shore_edfa_color><a href=/mars-bin/display_shore_edfa.pl target=ShoreEDFAWin $shore_edfa_status_msg>$fnt1 Shore EDFA</a> &nbsp
      <td bgcolor=$node_edfa_color><a href=/mars-bin/display_node_edfa.pl target=NodeEDFAWin $node_edfa_status_msg>$fnt1 Node EDFA</a> &nbsp
      <td bgcolor=$timing_color><a href=/mars-bin/display_timing.pl target=DispTimWin $timing_status_msg>$fnt1 Timing</a> &nbsp
     </table>
  </td>
  </tr>

<tr><th align=left><font color=#222222>MarsDCS Control:</font> </th>
  <td>
      <table>
        <td bgcolor=#444444><a href=control_ethports.pl target=StatusWin>$fnt1 Ethernet Control</a> &nbsp
	<td bgcolor=#444444><a href=display_ethports.pl target=StatusWin>$fnt1 Display Ethernet</a> &nbsp
        <td bgcolor=#444444><a href=display_mra01.pl target=StatusWin>$fnt1 Display Mra01</a> &nbsp
	<td bgcolor=#444444><a href=display_ra24e.pl target=StatusWin>$fnt1 Display RA24e</a> &nbsp
        <td bgcolor=#444444><a href=display_array.pl target=StatusWin>$fnt1 Display Test</a> &nbsp
        <td bgcolor=#444444><a href=control_obc_timing.pl target=StatusWin>$fnt1 OBC Timing</a> &nbsp
      </tr>

      </table>

<tr><th align=left><font color=#442266>Network Management:</font> </th>
      <td><table>
        <td bgcolor=#664488><a href=/nagios/cgi-bin/status.cgi?hostgroup=all&style=servicedetail target=StatusWin>$fnt1 StatusDetail</a> &nbsp
        <td bgcolor=#664488><a href=/nagios/cgi-bin/statusmap.cgi?host=all target=StatusWin>$fnt1 StatusMap</a> &nbsp
        <td bgcolor=#664488><a href=/nagios target=StatusWin>$fnt1 Nagios</a> &nbsp
        <td bgcolor=#664488><a href=router_interface.pl target=StatusWin>$fnt1 Cisco Cmds</a> &nbsp
        <td bgcolor=#664488><a href=/mars-bin/viewlog.pl?file=/var/log/cisco.log target=StatusWin>$fnt1 CiscoLog
        <td bgcolor=#664488><a href=/mars-bin/display_hist_power.pl target="StatusWin">$fnt Historical Plots</a>
      </td>
      </table>

</tr><tr>
<td><b><font color=#444444>DCS Documentation:</b> </font></td>
   <td><table>
      <td bgcolor=#666666><a href=/mars-doc/UserGuide target="StatusWin">$fnt User Guide</a>
      <!--<td bgcolor=#666666><a href=/mars-doc/DiagnosticGuide target="StatusWin">$fnt Diagnostic Guides</a>-->
      <td bgcolor=#666666><a href=/mars-doc/Manuals target="StatusWin">$fnt Manuals</a>
      <td bgcolor=#666666><a href=/mars-doc/Notes target="StatusWin">$fnt Notes</a>
      <!--<td bgcolor=#666666><a href=operator_log.pl target="StatusWin">$fnt Operator Log</a>-->
      <td bgcolor=#666666><a href=http://10.91.128.15/marswiki target="StatusWin">$fnt Wiki</a>
   </td>
</table>


</tr><tr>
<td colspan=2><font size=1>View: 
      <a href=http://10.91.128.96 target=StatusWin><font size=1>Camera1</font></a> &nbsp
      <a href=http://10.91.128.94 target=StatusWin><font size=1>Camera2</font></a> &nbsp
  </td>
</table>

  </table>
</center>

<!----Following for dynamic TIP
--->
<div id="dhtmltooltip"></div>

<script type="text/javascript">

/***********************************************
* Cool DHTML tooltip script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/

var offsetxpoint=-60 //Customize x offset of tooltip
var offsetypoint=20 //Customize y offset of tooltip
var ie=document.all
var ns6=document.getElementById && !document.all
var enabletip=false
if (ie||ns6)
var tipobj=document.all? document.all["dhtmltooltip"] : document.getElementById? document.getElementById("dhtmltooltip") : ""

function ietruebody(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function ddrivetip(thetext, thecolor, thewidth){
if (ns6||ie){
if (typeof thewidth!="undefined") tipobj.style.width=thewidth+"px"
if (typeof thecolor!="undefined" && thecolor!="") tipobj.style.backgroundColor=thecolor
tipobj.innerHTML=thetext
enabletip=true
return false
}
}

function positiontip(e){
if (enabletip){
var curX=(ns6)?e.pageX : event.clientX+ietruebody().scrollLeft;
var curY=(ns6)?e.pageY : event.clientY+ietruebody().scrollTop;
//Find out how close the mouse is to the corner of the window
var rightedge=ie&&!window.opera? ietruebody().clientWidth-event.clientX-offsetxpoint : window.innerWidth-e.clientX-offsetxpoint-20
var bottomedge=ie&&!window.opera? ietruebody().clientHeight-event.clientY-offsetypoint : window.innerHeight-e.clientY-offsetypoint-20

var leftedge=(offsetxpoint<0)? offsetxpoint*(-1) : -1000

//if the horizontal distance isn't enough to accomodate the width of the context menu
if (rightedge<tipobj.offsetWidth)
//move the horizontal position of the menu to the left by it's width
tipobj.style.left=ie? ietruebody().scrollLeft+event.clientX-tipobj.offsetWidth+"px" : window.pageXOffset+e.clientX-tipobj.offsetWidth+"px"
else if (curX<leftedge)
tipobj.style.left="5px"
else
//position the horizontal position of the menu where the mouse is positioned
tipobj.style.left=curX+offsetxpoint+"px"

//same concept with the vertical position
if (bottomedge<tipobj.offsetHeight)
tipobj.style.top=ie? ietruebody().scrollTop+event.clientY-tipobj.offsetHeight-offsetypoint+"px" : window.pageYOffset+e.clientY-tipobj.offsetHeight-offsetypoint+"px"
else
tipobj.style.top=curY+offsetypoint+"px"
tipobj.style.visibility="visible"
}
}

function hideddrivetip(){
if (ns6||ie){
enabletip=false
tipobj.style.visibility="hidden"
tipobj.style.left="-1000px"
tipobj.style.backgroundColor=''
tipobj.style.width=''
}
}

document.onmousemove=positiontip

</script>

</html>
EndHTML


exit;

#
# Subroutines Follow
#
sub gmtimestamp {local($no_gmtlabel) = $_[0];
        local($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime;
        # zero fill hour,min,sec and month
        if ($sec < 10) {$sec = "0$sec";}
        if ($min < 10) {$min = "0$min";}
        if ($hour < 10) {$hour = "0$hour";}
        $mon += 1; #from gmtime defined 0-11, make it 1-12
        if ($mon < 10) {$mon = "0$mon";}
        if ($mday < 10) {$mday = "0$mday";}
   	#make year 4-digits - Note: Year is years since 1900
   	$year += 1900; 
	if ($no_gmtlabel)
             {return "$year/$mon/$mday $hour:$min:$sec";}
        else {return "$year/$mon/$mday $hour:$min:$sec GMT";}
}


