aboutsummaryrefslogtreecommitdiff
path: root/listen
blob: 23893db6caa74ca8b8668eaa25a1e1bdda5d3f84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#!/usr/bin/env perl
# TODO:
# implement -s and -r flags
# implement --timeout flag in main loop
# possibly create a man page
#
# listen: a simple configurable build system
# created by Bryson Steck, @brysonsteck on GitHub
# free and open source under the GPL Version 3
# 
# Usage
# listen [flags] [file(s) to watch] [command(s) to run on file changes]
# 
# Flags
# -a, --all if all of the files have changed, required if multiple files
# -o, --any if any one of the files have changed, required if multiple files
# -s to use checksum on file change instead of when the file was modified
# -r, --run immediately run exec command, then start listening
# --timeout=[time] change timeout time in whole seconds (DEFAULT 1 SECOND)
# --help prints help

# GLOBALS
local $| = 1;
my $VERSION = "0.1.0";
my $ARGC = scalar @ARGV;
my $TIMEOUT = 1;
my $START_LISTEN = 0;
my $EXEC_POSITION = $ARGC - 1;
my $EXEC_LISTEN = undef;
my $ALL_FLAG = undef;
my $ANY_FLAG = undef;
my $MULTI_FILES = undef;
my $CHECKSUM_FLAG = undef;
my $CHECKSUM_COMMAND = "/usr/bin/env cksum";
my $RUN_NOW = undef;
my $black = "\033[0;90m";
my $nocolor = "\033[0m";

sub flags {
  if ($ARGC == 0) {
    print "listen: To what?\n";
    exit 2;
  }

  my $current_arg = 0;

  foreach $arg (@ARGV) {
    if ($arg =~ m/-[aosrhv]/) {
      if ($arg =~ m/a/) {
        $ALL_FLAG = "def";
      } 
      if ($arg =~ m/o/) {
        $ANY_FLAG = "def";
      }
      if ($arg =~ m/s/) {
        $CHECKSUM_FLAG = "def";
      }
      if ($arg =~ m/r/) {
        $RUN_NOW = "def";
      }
      if ($arg =~ m/h/) {
        print "\nlisten v$VERSION - a simple automation system\n";
        print "Copyright 2022 Bryson Steck\n";
        print "Free and open source under the GNU General Public License v3.\n";
        print "Run 'listen --license' to view the license and warranty disclaimer.\n\n";
        print "usage: listen [-v | --version] [-h | --help] [-a | --all]\n";
        print "              [-o | --any] [-s] [-r | --run] FILE1 [FILE2 ...]\n";
        print "              COMMAND\n\n";
        print "informational flags:\n";
        print "  -h | --help    -> Print this message\n";
        print "  -v | --version -> Print the version of listen\n";
        print "  --license      -> Print the license/warranty disclaimer\n";
        print "                    (GNU General Public License v3)\n\n";
        print "multiple file flags:\n";
        print "  -a | --all     -> Run COMMAND if ALL of the files have been modified\n";
        print "  -o | --any     -> Run COMMAND if ANY of the files have been modified\n\n";
        print "other flags:\n";
        print "  -r | --run     -> Run COMMAND before starting listen\n";
        print "  -s             -> Check for file modification based on cksum\n";
        print "                    (as opposed to the files' modified timestamp)\n\n";
        exit 1;
      }
      if ($arg =~ m/v/) {
        print "listen v$VERSION\n";
      }
    } elsif ($arg =~ m/--timeout=/) {
      my @timeout_split = split /\=/, $arg;
      if (scalar @timeout_split == 1) {
        print "timeout flag invalid\n";
        exit 2;
      } elsif (int($timeout_split[1]) > 0) {
        $TIMEOUT = $timeout_split[1];
        print "timeout is now $TIMEOUT seconds\n";
      } else {
        print "timeout flag invalid\n";
        exit 2;
      }
    } elsif ($arg =~ m/--help/) {
      print "\nlisten v$VERSION - a simple automation system\n";
      print "Copyright 2022 Bryson Steck\n";
      print "Free and open source under the GNU General Public License v3.\n";
      print "Run 'listen --license' to view the license and warranty disclaimer.\n\n";
      print "usage: listen [-v | --version] [-h | --help] [-a | --all]\n";
      print "              [-o | --any] [-s] [-r | --run] FILE1 [FILE2 ...]\n";
      print "              COMMAND\n\n";
      print "informational flags:\n";
      print "  -h | --help    -> Print this message\n";
      print "  -v | --version -> Print the version of listen\n";
      print "  --license      -> Print the license/warranty disclaimer\n";
      print "                    (GNU General Public License v3)\n\n";
      print "multiple file flags:\n";
      print "  -a | --all     -> Run COMMAND if ALL of the files have been modified\n";
      print "  -o | --any     -> Run COMMAND if ANY of the files have been modified\n\n";
      print "other flags:\n";
      print "  -r | --run     -> Run COMMAND before starting listen\n";
      print "  -s             -> Check for file modification based on cksum\n";
      print "                    (as opposed to the files' modified timestamp)\n\n";
      exit 1;
    } elsif ($arg =~ m/--version/) {
      print "listen v$VERSION\n";
      exit 1;
    } elsif ($arg =~ m/--license/) {
      print "listen is free and open source under the GNU GPL Version 3.0.\n\n";
      print "This program is free software: you can redistribute it and/or modify\n";
      print "it under the terms of the GNU General Public License as published by\n";
      print "the Free Software Foundation, either version 3 of the License, or\n";
      print "(at your option) any later version.\n\n";
      print "This program is distributed in the hope that it will be useful,\n";
      print "but WITHOUT ANY WARRANTY; without even the implied warranty of\n";
      print "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n";
      print "GNU General Public License for more details.\n\n";
      print "You should have received a copy of the GNU General Public License\n";
      print "along with this program.  If not, see <https://www.gnu.org/licenses/>.\n";
      exit 1;
    } elsif ($arg =~ m/--all/) {
      $ALL_FLAG = "def";
    } elsif ($arg =~ m/--any/) {
      $ANY_FLAG = "def";
    } elsif ($arg =~ m/--run/) {
      $RUN_NOW = "def";
    } else {
      last;
    }
    $current_arg++;
  }

  $START_LISTEN = $current_arg;
}

sub get_exec {
  if ($START_LISTEN + 1 == $ARGC) {
    print "listen: Not enough arguments\n";
    exit 3;
  }

  $EXEC_LISTEN = $ARGV[-1];
}

sub require_flags {
  if ($EXEC_POSITION - $START_LISTEN != 1) {
    if (!$ALL_FLAG and !$ANY_FLAG) {
      print "listen: Either -a or -o must be specified with multiple files to watch\n";
      exit 4;
    } elsif ($ALL_FLAG and $ANY_FLAG) {
      print "listen: You must specify either -a or -o for multiple files, not both\n";
      exit 4;
    }

    $MULTI_FILES = "def";
  }
}

sub check_files {
  for (my $i = $START_LISTEN; $i < $EXEC_POSITION; $i++) {
    if (not -e $ARGV[$i]) {
      print "listen: $ARGV[$i]: No such file or directory\n";
      exit 5;
    } elsif (not -r $ARGV[$i]) {
      print "listen: $ARGV[$i]: Permission denied (warning)\n";
    }
  }
}

sub diff {
  if ($CHECKSUM_FLAG) {
    #my $checksum_files;
    #for (my $i = $START_LISTEN; $i < $EXEC_POSITION; $i++) {
    #  $checksum_files = $checksum_files + " $ARGV[$i]";
    #}
    #print "$CHECKSUM_COMMAND $checksum_files";
    #return `$CHECKSUM_COMMAND $checksum_files`
  } else {
    my @return;

    for (my $i = $START_LISTEN; $i < $EXEC_POSITION; $i++) {
      push @return, (stat($ARGV[$i]))[9];
    }

    return @return;
  }
}

sub start {
  if (!$MULTI_FILES) {
    if ($EXEC_LISTEN =~ m/ _ /) {
      $EXEC_LISTEN =~ s/ _ / $ARGV[$START_LISTEN] /g;
    } elsif ($EXEC_LISTEN =~ m/ _/) {
      $EXEC_LISTEN =~ s/ _/ $ARGV[$START_LISTEN] /g;
    } elsif ($EXEC_LISTEN =~ m/_ /) {
      $EXEC_LISTEN =~ s/_ / $ARGV[$START_LISTEN] /g;
    }
  }

  print "$black& listen $VERSION\n";
  print "& This program is free software, and comes with ABSOLUTELY NO WARRANTY.\n";
  print "& Run 'listen --license' for details.\n&\n";
  print "& This shell command will run:\n";
  print "&   $EXEC_LISTEN\n";

  if ($MULTI_FILES) {
    if ($ALL_FLAG) {
      print "& When all the files below have been modified:\n";
      for (my $i = $START_LISTEN; $i < $EXEC_POSITION; $i++) {
        print "&   $ARGV[$i]\n";
      }
    } elsif ($ANY_FLAG) {
      print "& When any of the files below have been modified:\n";
      for (my $i = $START_LISTEN; $i < $EXEC_POSITION; $i++) {
        print "&   $ARGV[$i]\n";
      }
    }
  } else {
    print "& When this file has been modified:\n";
    print "&   $ARGV[$START_LISTEN]\n";
  }

  if ($RUN_NOW) {
    print "${black}& Running now, then starting listen...${nocolor}\n";
    system $EXEC_LISTEN;
    my $status = $? >> 8;
    if (int($status) != 0) {
      print "$black& WARNING: Exit code is $status. Returned to listen...$nocolor\n";
    } else {
      print "$black& Returned to listen...$nocolor\n";
    }
  } else {
    print "${black}& Starting now...${nocolor}\n";
  }

  my @previous_epoch = diff();
  my @current_epoch = diff();
  my @modified_files;

  for (my $i = $START_LISTEN; $i < $EXEC_POSITION; $i++) {
    push @modified_files, "no";
  }

  while (1) {
    my $run = undef;
    my $file_changed = undef;
    @current_epoch = diff();

    if ($MULTI_FILES) {
      if ($ALL_FLAG) {
        my $counter = 0;

        foreach $modified (@modified_files) {
          if (@current_epoch[$counter] != @previous_epoch[$counter]) {
            $modified = "yes";
          }
          $counter++;
        }
        foreach $modified (@modified_files) {
          my $run = 'def';
          if ($modified !~ "yes") {
            $run = undef;
          }
        }
      } else {
        my $counter = 0;

        foreach (@current_epoch) {
          if ($current_epoch[$counter] != $previous_epoch[$counter]) {
            $run = "def";
            $file_changed = $counter;
          }
          $counter++;
        }
      }
    } else {
      if ($current_epoch[0] != $previous_epoch[0]) {
        $run = "def";
        $file_changed = 0;
      }
    }

    if ($run) {
      if ($MULTI_FILES) {
        print "$black& File \"$ARGV[$file_changed + 1]\" modified. Running command...$nocolor\n";
      } else {
        print "$black& File \"$ARGV[$START_LISTEN]\" modified. Running command...$nocolor\n";
      }
      system $EXEC_LISTEN;
      my $status = $? >> 8;
      if (int($status) != 0) {
        print "$black& WARNING: Exit code is $status. Returned to listen...$nocolor\n";
      } else {
        print "$black& Returned to listen...$nocolor\n";
      }

      @previous_epoch = @current_epoch;
    }

    sleep(1);
  }
}

# ---- START OF SCRIPT ----
# start by checking flags
flags();

# get executable to run on trigger
get_exec();

# check if additional flags are needed 
# because multiple files are being listened to
require_flags();

# check if file(s) exist
check_files();

# start listen
start();