#!/usr/bin/perl
# ---------------------------------------------------------------------------
#
# Script to determine whether a particular POPFile version is up to
# date.
#
# Copyright (c) 2001-2008 John Graham-Cumming
#
#   This file is part of POPFile
#
#   POPFile is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   POPFile is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with POPFile; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# ---------------------------------------------------------------------------

use strict;
use CGI;
my $cgi = new CGI;

# Where POPFile lives and the current shipping version to compare
# against

my $site = 'http://getpopfile.org';
my $current_release_file = '../downloads/current_release.txt';
my ( $current_ma, $current_mi, $current_bu ) = ( 1, 1, 0 );

if ( open my $current_release, $current_release_file ) {
    my $current_version = <$current_release>;
    $current_version =~ s/[\r\n]//g;
    close $current_release;

    ( $current_ma, $current_mi, $current_bu ) = split( /[.]/, $current_version );
}

# Grab the passed in values and check that they are all numeric.  If
# they are not then just give up with silence.

my $result = 'new';
my ( $ma, $mi, $bu ) = ( $cgi->param('ma'), $cgi->param('mi'), $cgi->param('bu') );

if ( ( $ma !~ /^\d+$/ ) ||
     ( $mi !~ /^\d+$/ ) ||
     ( $bu !~ /^\d+$/ ) ) {
    $result = 'ok';
} else {

    # Decide whether the mentioned version is greater, less or equal
    # to the current version
    #
    # current == running           return ok
    # current > running            return new
    # running > current            return ok (for beta testers)

    my ( $ma_cmp, $mi_cmp, $bu_cmp ) = ( $current_ma <=> $ma,
                                         $current_mi <=> $mi,
                                         $current_bu <=> $bu );
    my $cmp = $ma_cmp;
    if ( $cmp == 0 ) {
        $cmp = $mi_cmp;
        if ( $cmp == 0 ) {
            $cmp = $bu_cmp;
        }
    }

    if ( $cmp <= 0 ) {
        $result = 'ok';
    }
}

print $cgi->redirect( "$site/images/${result}_popfile.gif" );
