#!perl -w

use strict;
use warnings;

use Rex;
use Rex::Config;
use Rex::Logger;
use Rex::Master;

use Getopt::Std;

$|++;

my %opts;

getopts('rvbhdP:l:p:i:', \%opts);

$Rex::Logger::debug = $opts{'d'};

if($opts{'l'}) {
   Rex::Config->set_log_filename($opts{'l'});
}

Rex::Logger::debug("Command Line Parameters");
for my $param (keys %opts) {
   Rex::Logger::debug("\t$param = " . $opts{$param});
}


if($opts{'h'}) {
   print "(R)?ex - (Remote)? Execution (Master)\n";
   printf "  %-15s %s\n", "-r", "Run it. This is alpha code. Don't use it unless you want to test it. But be warned, it may crash everything.";
   printf "  %-15s %s\n", "-l", "Set Logfilename";
   printf "  %-15s %s\n", "-i ip", "IP to listen on";
   printf "  %-15s %s\n", "-p port", "Set listen port";
   printf "  %-15s %s\n", "-b", "Fork to background";
   printf "  %-15s %s\n", "-P pid-file", "Pid File (default: master.pid)";
   printf "  %-15s %s\n", "-d", "Debug";
   printf "  %-15s %s\n", "-v", "Display version";
   print "\n";
   CORE::exit 0;
} elsif($opts{'v'}) {
   print "(R)?ex (Master) " . $Rex::VERSION . "\n";
   CORE::exit 0;
}

if(!$opts{'r'}) {
   print "This is alpha code. Don't use it unless you want to test it. But be warned, it may crash everything.\n";
   CORE::exit 1;
}

my %config = (
   port => $opts{'p'} || 7345,
   host => $opts{'i'} || "",
   background => $opts{'b'} || 0,
   pid_file => $opts{'P'}   || 'master.pid',
);

Rex::Master->new(%config)->run;


__END__

=pod

=head1 (R)?ex - (Remote)? Execution - Master

rex-master is a simple server to serve the rex tasks to all the rex-agents in your network.

=cut

