#! /usr/bin/perl -w
 
# Copyright (C) 2024 Arne Wichmann
#
# This little thing is distributed under the GNU General Public License,
# version 2. If you need the license, ask your distributor for it.

use strict;
use Getopt::Std;
sub usage();
sub finish($$);
sub canuseorfail($);

use vars qw($opt_f);
getopts('f:');

$opt_f || usage();

$#ARGV<0 and usage();

canuseorfail($opt_f);
my($mtime)=(stat(_))[9];

for (@ARGV) {
  canuseorfail($_);
  ($mtime>=(stat(_))[9]) or finish("CRITICAL - $opt_f older than $_!",2);
}

finish("OK - $opt_f is current",0);

sub canuseorfail($) {
  my($f)=shift;
  unless (-r $f) {
    my($msg)="unreadable";
    -f _ or $msg="is not a plain file";
    -e _ or $msg="nonexistant";
    finish("CRITICAL - File $f $msg!",2);
} }

# Show usage
sub usage() {
  print "usage:\n";
  print " $0 -f <file> <file> [...]\n\n";
  exit -1;
}

sub finish($$) {
    my ($msg,$state) = @_;

    print "$msg\n";
    exit $state;
}

