#!/usr/bin/perl
#version 3

use strict;

use vars qw($opt_P $opt_r $port_string $opt_q $opt_h $client $opt_b $opt_u $opt_R $opt_s);

$client='/usr/bin/cleo-client';

GetOptsTillCan('P=i'=>\$opt_P,
               'r=' =>\$opt_r,
               'h=' =>\$opt_h,
               'R=s'=>\$opt_R,
               'q=s'=>\$opt_q,
               's=' =>\$opt_s,
               'b=' =>\$opt_b,
               'u=' =>\$opt_u
              );
# -r -> recursive     -b -> block    -u -> unblock


die "Usage: $0 [-q queue][-P port][-R reason][-r][-s][-b|-u] cpus_list\n".
    "  Cpus list can be given as list of nodes and/or cpus and/or".
    "  diapasons of them via comma. Diapasons are two cpu or node".
    "  names with '..' between. E.g. node1..node3\n"
  if($opt_h || ($ARGV[0] eq ''));

$port_string  =" -p $opt_P" if $opt_P =~ /^\d+$/;
$port_string .=" -R" if $opt_r;
$port_string .=" -O 1" if $opt_s;
$port_string .=" -M '$opt_R'" if $opt_R ne '';
$port_string .=" -q $opt_q" if $opt_q ne '';

if($opt_u){
  $b = '-U';
}
else{
  $b = '-B';
}

exec("$client $port_string $b ".join(',',@ARGV)) or die("Internal error. Unable to run client application\n");


#
#  Gets opts like this: ('X=i', \$Xoption,...) (this means "option '-X 10' to variable $Xoption=10)
#  The scans command line for options till founds argument '--' or non-specified
#  option, or not '-' prefixed argument.
#  Specifications of options (what goes after 'X='):
#  i - integer
#  s - string
#  + - cumulative value (variable MUST be a list)
#  nothing - flag
#
sub GetOptsTillCan{

  my %args=@_;
  my ($k,$nk,$nv,$a,$next,%types);

  foreach $k (keys(%args)){
    $k =~ /^(\S+)(\=)(.*)/ or next;
    $nk=$1;
    $nv=$args{$k};
    $types{$nk} = $3;

    delete $args{$k};
    $args{$nk} = $nv;
  }

  while($next=shift @ARGV){
    last if(substr($next,0,1) ne '-');
    last if($next eq '--');
    $a=substr($next,1);
    last unless(exists $args{$a});
    undef $next;
    if(($types{$a} eq 'i') || ($types{$a} eq 's')){
      $a=$args{$a};
      $$a=shift @ARGV;
    }
    elsif($types{$a} eq ''){
      $a=$args{$a};
      $$a=1;
    }elsif($types{$a} eq '+'){
      $a=$args{$a};
      push @$a, shift @ARGV;
    }
  }
  unshift @ARGV, $next if(defined $next);
}

