]> mj.ucw.cz Git - bex.git/blob - bex
Maint: Reorganization of my directory structure
[bex.git] / bex
1 #!/usr/bin/perl
2 # Batch EXecutor -- Master Program
3 # (c) 2012--2015 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 sub show_help() {
13         print <<AMEN ;
14 Usage: bex <general-options> <command> <command-options> <args>
15
16 General options:
17 --home=<dir>    Home directory where all queues and config files reside
18 --lib=<dir>     Directory where BEX modules are installed
19
20 Commands (and aliases):
21 add (a)         Add new jobs to a queue
22 help            Show this help
23 job (j)         Operations on jobs
24 mach            List known machines and groups
25 prun (pr)       Parallel version of `run'
26 qman            Management of queues
27 queue (q)       Show queues and jobs on them
28 run (r)         Run queued jobs
29 AMEN
30         exit 0;
31 }
32
33 Getopt::Long::Configure('require_order');
34 GetOptions(
35         "home=s" => \$bex_home,
36         "lib=s" => \$bex_lib,
37         "help" => \&show_help,
38         "version" => sub {
39                         print "BEX 3.3 (c) 2011-2015 Martin Mares <mj\@ucw.cz>\n";
40                 },
41 ) or die "Try `bex help' for more information.\n";
42 Getopt::Long::Configure('default');
43
44 if (!-d $bex_home) {
45         die "BEX home directory $bex_home does not exist.\n";
46 }
47 if (!-d "$bex_home/cf") {
48         die "BEX home directory $bex_home does not contain the cf subdirectory.\n";
49 }
50 if (!-f "$bex_lib/perl/BEX.pm") {
51         die "BEX library directory $bex_lib misconfigured.\n";
52 }
53
54 @ARGV or die "Missing subcommand.\n";
55 my $sub = shift @ARGV;
56 $sub =~ /^[0-9a-zA-Z]+$/ or die "Invalid subcommand $sub\n";
57
58 if ($sub eq 'help') { show_help(); }
59
60 my %aliases = (
61         'a' => 'add',
62         'j' => 'job',
63         'pr' => 'prun',
64         'q' => 'queue',
65         'r' => 'run',
66 );
67 if (defined $aliases{$sub}) { $sub = $aliases{$sub}; }
68
69 my $sub_path = "$bex_lib/bin/bex-$sub";
70 -x $sub_path or die "Unknown subcommand $sub\n";
71
72 $ENV{"BEX_HOME"} = $bex_home;
73 $ENV{"BEX_LIB"} = $bex_lib;
74 $ENV{"PERL5LIB"} = join(":", $bex_lib . "/perl", $ENV{"PERL5LIB"} // ());
75 exec $sub_path, @ARGV;
76 die "Cannot execute $sub_path: $!\n";