#!/usr/bin/perl
#
# $Id: conf_kowtow 32844 2015-08-05 14:02:13Z atsybulnik $
#
# Purpose:
#     handshake with master
#
# Call:
#     conf_kowtow
#     conf_kowtow register 
# Note:
#   1. Node-side process
#   2. Starts by cam_patrol
#   3. Runs in background
#   4. register the node with master if master refuse handshake
#   5. completes the mission when master confirm handshake
#   6. Master provides RTSP port when handshake, store port in conf

use strict;
use XML::Simple;
use Data::Dumper;
use NextCAM::Conf;
use Node::Conf qw(Set_Conf);
use Log::Log4perl "get_logger";
require "$ENV{APL}/common/bin/logger.patrol";

# CONS ================================================================

my $log = get_logger('NEXTCAM::CONF::KOWTOW');
my $APL=$ENV{APL};
my $REGISTER=($ARGV[0] eq 'register')?1:0;

# PROC ===============================================================
sub api {
   my ($uni,$act)=@_;
   my $call=(-f "$ENV{APL_VAR}/conf/master/s_master")
      ? "$APL/conf/bin/master4node action=$act uni=$uni"
      : "ssh s_master '. $APL/base/etc/env.conf && $APL/conf/bin/master4node action=$act uni=$uni'";
   my $ret=`$call`;
   return "ERROR: call ($call) fails ($ret)" if $?;
   my $xref = XMLin($ret);
   $log->debug(Dumper($xref));
   my $new_conf = {};
   $new_conf->{RTSP_PORT} = $xref->{RTSP_PORT} if $xref->{RTSP_PORT};
   $new_conf->{OBJID} = $xref->{OBJID} if $xref->{OBJID};
   $new_conf->{UNI} = $xref->{SET_UNI} if $xref->{SET_UNI};
   $new_conf->{FQDN} = $xref->{FQDN} if $xref->{FQDN};
   $new_conf->{VERID} = $xref->{VERID} if $xref->{VERID};
   $new_conf->{IP} = $xref->{IP} if $xref->{IP};
   $new_conf->{INSTALL_RESULT} = $xref->{INSTALL_RESULT} if $xref->{INSTALL_RESULT};
   $new_conf->{HISTORY} = $xref->{HISTORY} if $xref->{HISTORY};
   Set_Conf($new_conf);
#  print STDERR "$xref->{STATUS}:$xref->{MESSAGE}:$xref->{'RTSP_PORT'}\n";
   return "$xref->{STATUS}:$xref->{MESSAGE}:$xref->{'RTSP_PORT'}" if not $xref->{SET_UNI};
   return "$xref->{STATUS}:$xref->{MESSAGE}:$xref->{'RTSP_PORT'}:$xref->{OBJID}:$xref->{SET_UNI}";
}

# MAIN ===============================================================

$log->info("handshake starts");

open(NODE,"$APL/var/conf/node/conf") || $log->logdie("Cannot read $APL/var/conf/node/conf");
my %node=map {/(^\w+)=(.+)/} grep {/^\w+=.+/} <NODE>;
close NODE;
$log->logdie("Cannot locate correct UNI in $APL/var/conf/node/conf") if not $node{UNI}=~/^\w{22}$/;

my $ret;

for(;;) {
  for(;;sleep 30) {      # -------------------- handshake attempt
     $ret=api($node{UNI},'handshake');
     last if not $ret=~/^ERROR/;
     $log->info("handshake: $ret");
  }
  if($ret=~/^FAIL/) {    #--------------------- registration
    $log->info("handshake fails, need register with master");
    if($REGISTER) {      # only if register in command line
       $ret=api($node{UNI},'register');
       if(not $ret=~/^OK/) {
         $log->warn("registration fails: $ret");
         sleep 30;
       }
    }
  }
  $log->info("handshake completed: $ret");
  $log->info("System UNI was changed!") if $ret=~/UNI CHANGED/i;
  last if  $ret=~/^OK/;
}
