#!/usr/bin/env perl
# IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html
use Fcntl qw(:DEFAULT :flock);
use strict;

sub get_lock {
    unless (flock(LOCKHANDLE, LOCK_EX | LOCK_NB)) {
        $| = 1;
        print "Acquiring startup lock...";
        flock(LOCKHANDLE, LOCK_EX) or die "Error trying to secure a startup lock";
        print "done\n";
    }
    truncate(LOCKHANDLE, 0);
    print LOCKHANDLE $$ . "\n";
}

sub release_lock {
    truncate(LOCKHANDLE, 0);
    flock(LOCKHANDLE, LOCK_UN);
}

BEGIN
{
    use Time::HiRes qw(sleep);
    use File::Path;
    use Fcntl qw(:DEFAULT :flock);
    $::XCATROOT = $ENV{'XCATROOT'} ? $ENV{'XCATROOT'} : '/opt/xcat';

}
my $sleepint = int(rand(10));
use lib "$::XCATROOT/lib/perl";
require xCAT::Client;
require xCAT::Utils;
use strict;

use xCAT::PPCcli qw(SUCCESS EXPECT_ERROR RC_ERROR NR_ERROR);
use Data::Dumper;
require File::Basename;

##############################################
# Globals
##############################################
my $verbose = 0;
my $node;
my $host;
my $lparid;
my $mtms;
my @cred;
my $credencial;
umask 0077;
mkpath("/tmp/xcat/");
unless (sysopen(LOCKHANDLE, "/tmp/xcat/consolelock", O_WRONLY | O_CREAT)) {
    xCAT::Utils::console_sleep(15, "Unable to open lock file");
    exit 0;
}
get_lock();
my $sleepint = int(rand(10)); #Stagger start to avoid overwhelming conserver/xCATd
xCAT::Utils::console_sleep($sleepint, "Opening console in " . (2 + (0.5 * $sleepint)) . " seconds...\n");

##########################################################################
# Open remote console
##########################################################################
sub invoke_cmd {
    my $type    = "lpar";
    my $hwtype  = "ivm";
    my %request = (
        ppcretry => 1,
        verbose  => $verbose
    );
    #################################
    # Get userid and password
    #################################
    @cred = split(/,/, $credencial);
    $request{$host}{cred} = \@cred;
    #################################
    # Connect to the remote server
    #################################
    my @exp = xCAT::PPCcli::connect(\%request, $hwtype, $host);
    if (ref($exp[0]) ne "Expect") {
        return ($exp[0]);
    }
    #################################
    # Open console connection
    #################################
    my $result = xCAT::PPCcli::mkvterm(\@exp, $type, $lparid, $mtms);
    my $Rc = shift(@$result);

    if ($Rc != SUCCESS) {
        return (@$result[0]);
    }
    return (0);
}
##############################################
# Start main body of code
##############################################
sub getans {
    my $rsp = shift;

    if ($rsp->{node}) {
        $host       = $rsp->{node}->[0]->{host}->[0];
        $lparid     = $rsp->{node}->[0]->{lparid}->[0];
        $mtms       = $rsp->{node}->[0]->{mtms}->[0];
        $credencial = $rsp->{node}->[0]->{cred}->[0];

    }
}

my $cmdref = {
    command   => ["gethmccon"],
    arg       => ["text"],
    noderange => [ $ARGV[0] ]
};


xCAT::Client::submit_request($cmdref, \&getans);
until ($lparid and $host and $mtms) {
    release_lock();    #Let other clients have a go
    $sleepint = 10 + int(rand(20)); #Stagger to minimize lock collisions, but no big deal when it does happen
    xCAT::Utils::console_sleep($sleepint, "Console not ready, retrying in $sleepint seconds (Hit Ctrl-E,c,o to skip delay)\n");
    get_lock();
    xCAT::Client::submit_request($cmdref, \&getans);
}
release_lock();                     #done with xcatd, can run with near impunity

$node = $ARGV[0];

my $result = invoke_cmd($host, $lparid, $mtms);
if ($result ne "0") {
    print STDERR "$node: $result\n";
    exit(1);
}
exit(0);
