#!/usr/bin/perl
#
# npc_login - Steven Lerner 12/2005
#             Woods Hole Oceanographic Institution
#
#       Description: Quick script that logs into the DCS Node
#                    Power Controller (NPC) via obc xbar7/sail 
#                    serial port
#
#       Usage: npc_login
#
#       Output: returns 1 if logged-in, 0 if not logged-in
#
# History:
#       Date       Who    Description
#       ----       ---    ---------------------------------------------------
#      12/16/2005  SL     Create
#################################################################################
require "getopts.pl";
require "flush.pl";
require "ctime.pl";
require "timelocal.pl";

$VERSION = "v1.0";

&Getopts('db');
if (! defined ($opt_d)) {$DEBUG = 0;}
                   else {$DEBUG = 1;}
if (! defined ($opt_b)) {$bypass_check = 0;}
                   else {$bypass_check = 1;}


#make sure get_power_data_sail is not running
#unless bypass_check is set
if (!$bypass_check)
   {$ret = `/bin/ps auwx | grep get_power_data_sail | grep -v grep`;
    if ($ret ne "") {print "==>get_power_data_sail.pl is already running<==\n";
                     print "To force login, run with -b option (should only be done via get_power_data_sail.pl)\n";
                     exit;
                     }
    }


$sail = "/home/stevel/sail_driver/sail";
$ddir = "/home/stevel/power_obc/Data";

use FileHandle;
use IPC::Open3;

my $in  = new FileHandle;
my $out = new FileHandle;
my $err = new FileHandle;

open3 ($out,$in,$err,"$sail -s#S01A -x7") || die "Unable to open sail driver $!\n";


#Added non-blocking option
use Fcntl;
$flags = '';
fcntl($in, F_GETFL, $flags) or die "Couldn't get flags for HANDLE : $!\n";
$flags |= O_NONBLOCK;
fcntl($in, F_SETFL, $flags) or die "Couldn't set flags for HANDLE: $!\n";


#make sure logged-in
if (! &npc_login())
     {$logged_in = 0;
      for ($i=0;$i<3;$i++)
        {if (&npc_login()) {$logged_in = 1; last;}   
         }
      if (!$logged_in) {print "0,Unable to log in to NPC\n";
                       exit;
                       }
      }
print "1,Logged in! [$logged_in]\n";
close($in); close($out); close($err);

#just in case, kill any sail turds
$pid = "";
$ret = `/bin/ps auwx|grep "sail -s#S01A -x7"|grep -v grep`;
($user,$pid) = split(' ',$ret);
#print "ret=[$ret], pid=[$pid]\n";
if ($pid ne "") {`kill -9 $pid`;}

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";}
}




sub npc_login {
	my($ret,$uname,$pword);
      $uname = "dcs"; $pword="apl1013";
      print "Sending \\n to sail port\n" if $DEBUG;
      print $out "\n"; #flush(COM);
      select(undef, undef, undef, 1); #wait a smidge
      while ($ret=<$in>)
        {print "response=[$ret]\n" if $DEBUG;
         if ($ret =~ /login/i)
          {print $out "$uname\n";
           select(undef, undef, undef, .25); #wait a smidge
           print $out "$pword\n";
           select(undef, undef, undef, .25); #wait a smidge
           next; 
           }
         if ($ret =~ /password/i)
           {print $out "$pword\n";
            select(undef, undef, undef, .25); #wait a smidge
            next; 
            }
         if ($ret =~ />/) {return 1;} #prompt, logged-in
         select(undef,undef,undef, .5);
         }
      return 0;
}


