#!/usr/bin/perl -w

# $Id:

#****************************************************************************
#
#                             BLDLVLCK
#
#  This perl script checks the user's system for the required levels of
#  software needed to properly build Hercules from the source repository.
#
#****************************************************************************
#
#                       ---- CHANGE LOG ----
#
# DD/MM/YY Description...
# 26/02/03 Created by Jim Morrison.
# 21/09/03 Removed libintl & libtool. Although "gettextize" is no longer
#          run, pkg "gettext" is still needed to run the msgfmt/msgmerge
#          utilities. libintl is now self-contained. libtool is now self-
#          contained. (ISW)
# 24/10/05 Added history section in preparation for possible changes. (Fish)
# 24/10/05 According to jj, automake 1.6 is insufficient. He says 1.9 works
#          though, but doesn't know about 1.7 or 1.8 yet. (Fish)
# 30/11/07 Changed URL to point to hercules-390.org. (JRM)
# 30/11/10 Remove obsolete CVS test, add special gsed test for Apple/Darwin
#          (Enrico Sorichetti by Fish)
# 06/12/10 M4 fix (Enrico Sorichetti by Fish)
#****************************************************************************

use strict;

#    Facility, required level, special flag, download URL

my @req = qw(
     autoconf    2.5    0  http://www.gnu.org/directory/autoconf.html
     automake    1.9    0  http://www.gnu.org/directory/automake.html
     gawk        3.0    0  http://www.gnu.org/directory/gawk.html
     gcc         3      0  http://www.gnu.org/directory/gcc.html
     grep        1      0  http://www.gnu.org/directory/grep.html
     libiconv    1.8    1  http://www.gnu.org/directory/libiconv.html
     m4          1.4.6  0  http://www.gnu.org/directory/GNU/gnum4.html
     make        3.79   0  http://www.gnu.org/directory/make.html
     perl        5.6    1  http://www.gnu.org/directory/perl/html
     sed         3.02   0  http://www.gnu.org/directory/sed.html
);

my $msg;
my $instversion;

sayintro();

for (my $i = 0; $i < @req; $i += 4) {
  my $facility = $req[$i];
  if ($facility eq 'sed') {
    if ($^O eq 'darwin') {
      print "Apple/Darwin ==> sed changed to gsed!\n" ;
      $facility = 'gsed';
    }
  }

  my $level = $req[$i+1];
  my $special = $req[$i+2];
  if ($facility eq 'm4') {
    if ($^O eq 'darwin') {
      print "Apple/Darwin ==> custom version parsing!\n" ;
      $special = 1;
    }
  }

  my $url = $req[$i+3];
  if ($special) {
    weird($facility, $level, $url);
  } else {
    if (present($facility, $url)) {
      my @resp = `$facility --version`;
      chomp $resp[0];
      $instversion = getvers($resp[0]);
      $msg = ckvers($level, $instversion);
      print "$msg\t$facility requires $level, found $instversion\n";
      print "\tURL: $url\n" if $msg eq 'UPGRADE';
    }
  }
  print "\n";
}

exit 0;

sub weird {
  my ($facility, $level, $url) = @_;

  if ($facility eq 'libiconv') {
    if (present('iconv', $url)) {
      my @resp = `iconv --version`;
      chomp $resp[0];
      my $instversion = getvers($resp[0]);
      my $msg = ckvers($level, $instversion);
      print "$msg\t$facility requires $level, found $instversion\n";
    }
    print "\tURL: $url\n" if $msg eq 'UPGRADE';
    return;
  }
  if ($facility eq 'perl') {
    if (present($facility, $url)) {
      my $instversion = getvers($^V);
      my $msg = ckvers($level, $instversion);
      print "$msg\t$facility requires $level, found $instversion\n";
      print "\tURL: $url\n" if $msg eq 'UPGRADE';
    }
    return;
  }
  if ($facility eq 'libtool') {
    if (present($facility, $url)) {
      my @resp = `libtoolize --version`;
      print "\t$resp[0]\n";
      chomp $resp[0];
      my $instversion = getvers($resp[0]);
      my $msg = ckvers($level, $instversion);
      print "$msg\t$facility requires $level, found $instversion\n";
      print "\tURL: $url\n" if $msg eq 'UPGRADE';
    }
    return;
  }

  if ($facility eq 'm4') {      # m4 --version: GNU m4 1.4o
    if (present($facility, $url)) {
      my $resp = `m4 --version`;
      chomp $resp;
      my $msg = 'HUH?   ';
      my $instversion = "DUNNO";
      if ($resp =~ /GNU [mM]4 (\d+.\d+.\d+)/) {
        $instversion = '';
        $instversion = $1 if defined $1;
        $instversion = "$1.$2" if defined $1 && defined $2;
        $instversion = "$1.$2.$3" if defined $1 && defined $2 && defined $3;
        $msg = ckvers($level, $instversion);
      }
      print "$msg\t$facility requires $level, found $instversion\n";
      print "\tURL: $url\n" if $msg eq 'UPGRADE';
    }
    return;
  }
  print "ERROR $facility flagged as special, not found\n";
}

sub getvers {
  my $resp = $_[0];
  my $vers;
  if ($resp =~ /(\d+).(\d+).(\d+)/) {
    $vers = "$1.$2.$3";
    return $vers;
  }
  if ($resp =~ /(\d+).(\d+)/) {
    $vers = "$1.$2";
    return $vers;
  }
  print "HUH?\n";
}

sub ckvers {
  my ($reqvers, $instvers) = @_;
  my @rv = split /\./, $reqvers;
  my @iv = split /\./, $instvers;
  for (my $i = 0; $i < @rv; $i++) {
    if ( (exists $rv[$i])
      && (exists $iv[$i])
      && ($iv[$i] > $rv[$i]) ) {
      return 'OK';
    }
    if ( (exists $rv[$i])
      && (exists $iv[$i])
      && ($iv[$i] < $rv[$i]) ) {
      return 'UPGRADE';
    }
  }
  return 'OK';
}

sub sayintro {
    print "This utility will check the level of various utilities needed to build\n";
    print "hercules. Checking is done against versions that are KNOWN to work.\n";
    print "This doesn't mean a build will NOT succeed with older versions\n";
    print "of the utilities, but will give a hint as to what package may need\n";
    print "an upgrade if the build ever fails with some odd reason.\n\n\n";
}

sub present {
  my ($facility, $url) = @_;
  my @present = `which $facility 2>/dev/null`;
  if (! @present) {
    print "INSTALL\t$facility not found\n";
    print "\tURL: $url\n";
  }
  return scalar @present;
}
