#!/usr/bin/perl
#  $Id:$
# -----------------------------------------------------------------------------
#  Proxy script for node support file management
#  Used by master_sf_collect via ssh
# -----------------------------------------------------------------------------
#  Author: Alexey Tsibulnik
#  Modified by:
#  QA by:  
#  Copyright: videoNEXT Network solutions, Inc.
# -----------------------------------------------------------------------------

use strict;

# CONS
my $APL = $ENV{APL} || '/opt/sarch';
my $APL_MOD = $ENV{APL_MOD};

# VARS
my $SupportDir = "$APL/var/sf/node";

# ARGS
my %args = map {(lc($1),$2) if /^(\w+)=(.*)$/} grep {/^\w+=/} @ARGV;
my $action = $args{action};

sub sf_list {
    my $i = 0;
    my @sflist;
    
    # List support files from disk
    opendir(DH, $SupportDir);
    while(my $f = readdir(DH)) {
	chomp;
	@{$sflist[$i++]}{'id','timestamp','year','month','day','hour','min','mode','status','file'} = 
	    ($1, $2, $3, $4, $5, $6, $7, $8 eq 'a'?'auto':'user', parse_status($9), $f) 
	    if $f =~ /^(.+((\d{4})(\d{2})(\d{2})_(\d{2})(\d{2}))([au]))\.(.+)$/;
    }
    close(DH);
    return @sflist;
}

#------------------------------------------------------------------------------
# Main
for($action) {
    /create/      && do { create();   next };
    /cancel/	  && do { cancel();   next };
    /list/        && do { list();     next };
    /status/      && do { status();   next };
    /remove/      && do { remove();   next };
    /cleanup/     && do { cleanup();   next };
    do { die "No action specified\n" };
}

#------------------------------------------------------------------------------
# Actions
sub create {
    # Look for running instance of node_sf_collect
    my $pidfile = "$APL/var/pids/node_sf_collect.pid";
    my @sf;
    my ($pid,$sf);
    if (-f $pidfile) {
	if (open(PID,$pidfile)) {
	    $pid = <PID>;
	    close PID;
	    chomp $pid;
	}
    }
    if ($pid && `ps -p $pid -o command=|grep node_sf_collect`) {
	opendir(SFD, $SupportDir);
	my @sfp = map {/^(.+)\.INPR$/} grep {/\.INPR$/} readdir SFD;
	closedir SFD;
	$sf = $sfp[0] if @sfp;
    }
    if (!$sf){
	opendir(SFD, $SupportDir);
	my @sfp = map {/^(.+)\.INPR$/} grep {/\.INPR$/} readdir SFD;
	closedir SFD;
	$sf = $sfp[0] if @sfp;
	my $mode = $args{mode} || 'user';
        $sf = `$APL/conf/bin/node_sf_collect -b -m $mode 2>/dev/null`;
        die('Failed to start collecting!') if $? != 0;
        chomp $sf;
    }
    print "$sf\n";
}

sub cancel {
    system("$APL/conf/bin/node_sf_collect -T 1>/dev/null 2>/dev/null &");
    die("Failed to cancel operation!") if $? != 0;
}

sub status {
    my $sfid = $args{sfid} or die "SFID should be specified!\n";
    my $status = 'none';
    my @sflist = grep {$_->{id} eq $sfid} sf_list;
    $status = $sflist[0]->{status} if @sflist;
    print "$status\n";
}

sub remove {
    my $sfid = $args{sfid};
    my @sflist = grep {$_->{id} eq $sfid} sf_list;
    die("No such file") unless @sflist;
    die("Collection in progress") if $sflist[0]->{status} eq 'inprogress';
    die("Cannot delete file $SupportDir/$sflist[0]->{file} : $!") unless unlink("$SupportDir/$sflist[0]->{file}");
}

sub cleanup {
    system("$APL/conf/bin/node_sf_collect -T 1>/dev/null 2>/dev/null");
    die("Failed to cancel operation!") if $?;
    unlink "$SupportDir/$_->{file}" foreach (sf_list);
}

sub parse_status {
    my $ext = shift;
    my $status;
    for($ext) {
	/nspt/        && do { return 'OK'         };
	/FAIL/        && do { return 'fail'       };
	/NOSPACE/     && do { return 'nospace'    };
	/INPR/        && do { return 'inprogress' };
	/TERM/	      && do { return 'cancelled'  };
    }
}

sub list {
    my $list = '';
    my @sflist = sf_list;
    $list .= "$_->{file}\n" foreach (@sflist);
    print $list;
}    

