]> mj.ucw.cz Git - bex.git/blob - bex
The big move -- introduced subcommands
[bex.git] / bex
1 #!/usr/bin/perl
2 # Batch EXecutor 3.0 -- Master Program
3 # (c) 2012 Martin Mares <mj@ucw.cz>
4
5 use strict;
6 use warnings;
7 use Getopt::Long;
8
9 my $bex_home = $ENV{"BEX_HOME"} // ".";
10 my $bex_lib = $ENV{"BEX_LIB"} // "lib";
11
12 Getopt::Long::Configure('require_order');
13 GetOptions(
14         "home=s" => \$bex_home,
15         "lib=s" => \$bex_lib,
16         "help" => sub {
17                         print "Usage: brum\n";
18                         exit 0;
19                 },
20         "version" => sub {
21                         print "BEX 3.0 (c) 2011-2012 Martin Mares <mj\@ucw.cz>\n";
22                 },
23 ) or die "Try `bex --help' for more information.\n";
24 Getopt::Long::Configure('default');
25
26 if (!-d $bex_home) {
27         die "BEX home directory $bex_home does not exist.\n";
28 }
29 if (!-d "$bex_home/BEX") {
30         die "BEX home directory $bex_home does not contain the BEX subdirectory.\n";
31 }
32
33 @ARGV or die "Missing subcommand.\n";
34 my $sub = shift @ARGV;
35 $sub =~ /^[0-9a-zA-Z]+$/ or die "Invalid subcommand $sub\n";
36
37 my %aliases = (
38         'a' => 'add',
39         'q' => 'queue',
40         'r' => 'run',
41 );
42 if (defined $aliases{$sub}) { $sub = $aliases{$sub}; }
43
44 my $sub_path = "$bex_lib/bin/$sub";
45 -x $sub_path or die "Unknown subcommand $sub\n";
46
47 $ENV{"BEX_HOME"} = $bex_home;
48 $ENV{"BEX_LIB"} = $bex_lib;
49 $ENV{"PERL5LIB"} = join(":", $bex_lib . "/perl", $ENV{"PERL5LIB"} // ());
50 exec $sub_path, @ARGV;
51 die "Cannot execute $sub_path: $!\n";