#!/usr/bin/perl -w
####################### check_cmsms_version.pl #######################
# Version : 0.1
# Date : 30 Mar 2010
# Author  : Jan Falkenstern (falkenstern at digital-designs.de)
# Licence : GPL - http://www.fsf.org/licenses/gpl.txt
######################################################################
#
# help : ./check_cmsms_version.pl -h

use Getopt::Long;
use LWP::Simple;

use lib "/usr/local/nagios/libexec";
use utils qw(%ERRORS);
#my %ERRORS=('OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3,'DEPENDENT'=>4);


###############
### Globals ###
###############

my $script_name            = $0;
my $script_version         = '0.1';
my $latest_version         = undef;
my $latest_version_url     = 'http://dev.cmsmadesimple.org/latest_version.php';
my $o_cms_root             = undef;
my $installed_version      = undef;
my $installed_version_name = undef;


#################
### Functions ###
#################

sub show_versioninfo { print "$script_name version: $script_version\n"; }

sub print_usage {
  print "Usage: $script_name -r <cmsroot>\n";
}

sub help {
  print "\nCMSms (CMS made simple) version check for Nagios. Version ",$script_version,"\n";
  print " by Jan Falkenstern | falkenstern(at)digital-designs.de\n";
  print " CMS made simple: http://www.cmsmadesimple.org/\n\n";
  print_usage();
  print <<EOT;
-r, --rootfolder
   Root folder of CMSms installation. (This folder contains version.php file)
-h, --help
   Print this help message.
-V, --version
   Prints version number.

EOT
}

sub check_options {
  Getopt::Long::Configure ("bundling");
  GetOptions(
      'h'     => \$o_help,        'help'          => \$o_help,
      'V'     => \$o_version,     'version'       => \$o_version,
      'r:s'   => \$o_cms_root,    'rootfolder:s'  => \$o_cms_root
  );

  if (defined ($o_help)) { help(); exit $ERRORS{"UNKNOWN"}};
  if (defined($o_version)) { show_versioninfo(); exit $ERRORS{"UNKNOWN"}};

   # Check mandatory attributes
  if (!defined($o_cms_root)) { print_usage(); exit $ERRORS{"UNKNOWN"}};
}


############
### MAIN ###
############

check_options();

##########################################
### Get latest version number from url ###
##########################################

my $content = get $latest_version_url;
die "Couldn't get $latest_version_url" unless defined $content;

@array = split(/:/, $content);
if ($array[0] ne "cmsmadesimple") {
  die "Couldn't get current version from $latest_version_url";
}
$latest_version = $array[1];


#############################################
### Get version number of installed CMSms ###
#############################################

open(VERSIONFILE, "<$o_cms_root/version.php") || die "Unable to read version info file (version.php) from folder \"$o_cms_root\".\n";

while(<VERSIONFILE>) {
  if ($_ =~ "CMS_VERSION ") {
    @parts = split(/"/, $_);
    $installed_version = $parts[1];
  }  
  if ($_ =~ "CMS_VERSION_NAME ") {
    @parts = split(/"/, $_);
    $installed_version_name = $parts[1];
  }
}

die "Could not get value CMS_VERSION from file version.php" unless defined $installed_version;
die "Could not get value CMS_VERSION_NAME from file version.php" unless defined $installed_version_name;


########################################
### Compare version numbers / Output ###
########################################

if ($latest_version eq $installed_version) {
  print "OK - CMSms installation in $o_cms_root is up to date. Version $installed_version ($installed_version_name)\n";
#  exit($ERRORS{"OK"});
  exit(0);
} else {
  print "WARNING - CMSms instalation in $o_cms_root is outdated. Your Version: $installed_version. Latest Version: $latest_version\n";
#  exit($ERRORS{"WARNING"});
  exit(1);
}
