#!/usr/bin/perl -w

# duck - the Debian Url Checker
# Copyright (C) 2014 Simon Kainz <simon@familiekainz.at>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# he Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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.
#
# On Debian GNU/Linux systems, the complete text of the GNU General
# Public License can be found in `/usr/share/common-licenses/GPL-2'.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, you can find it on the World Wide
# Web at https://www.gnu.org/copyleft/gpl.html, or write to the Free
# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
# MA 02110-1301, USA.

use strict;

use lib '/usr/share/duck/lib';
use lib './lib';

use DUCK;
use Getopt::Std;
use Getopt::Long qw(:config pass_through );
use Data::Dumper;
use File::Basename;
require lib;

sub HELP_MESSAGE();
sub display_result($;$;$);
sub missingHelpers();

my $checksdir='/usr/share/duck/lib/checks';

 if ( -d "./lib/checks" )
{
    $checksdir='./lib/checks';
}


my $try_https=0;
my $nocolor=0;
my $showMissingHelpers;

my @global_options=("n","v","q");
$Getopt::Std::STANDARD_HELP_VERSION=1;

my $exitcode=0;

our %opt;
my $dh;



if ($ENV{'DUCK_NOCOLOR'} ) {$nocolor=1;}


my $retval=GetOptions(
    "modules-dir=s" => \$checksdir ,
    "no-https" => \$try_https,
    "no-color" => \$nocolor,
    "missing-helpers" => \$showMissingHelpers
    );


my $DUCK= DUCK->new();
my $funcref= $DUCK->cb();

if ($showMissingHelpers)
{
    print "\n";
    my $mh=missingHelpers();
    if ($mh) {print $mh; exit(1)}
    else { print "All helpers installed.\n"; exit(0);}
    
}


our $color_r="\x1b[31m";
our $color_g="\x1b[32m";
our $color_y="\x1b[33m";
our $color_n="\x1b[0m"; 

if ($nocolor )
{
    $color_r="";
    $color_g="";
    $color_y="";
    $color_n="";
}



import lib $checksdir;

if (!opendir($dh,$checksdir))
{
 print STDERR "Modules directory $checksdir not found!, please use --modules-dir=<path> !\n";
 if ($opt{h})
 {
     HELP_MESSAGE();
   
 }
  exit(1);
}



my @modules;
my @module_options;

my $descriptions;

while (readdir $dh)
{
    my($filename, $directories, $suffix) = fileparse($_);
    if (/^\./) { next};
    if (/.pm$/)
    {
	require $_;
	my $modulename=fileparse($filename,qr/\.pm/);
	my $n="DUCK::".$modulename;
	if ($n->can("run"))
	{
	    push (@modules,$modulename);
	}
    }
}

if (!scalar @modules)
{
    print STDERR "No check modules found! Please check path: ".$checksdir."\n";
    exit 1;
}

#get all options modules are providing

foreach my $m (@modules)
{

    my $n="DUCK::".$m;
    if ( ($n->can("opts")) && ($n->can("run")) )
    {
	foreach ($n->opts())
	{
	    push(@module_options,$_);
	}
    }
    if ($n->can("desc"))
    {
       $descriptions.=$n->desc();
     }
}
push(@module_options,@global_options);


GetOptions(
      "help" => sub {HELP_MESSAGE() }
    );


getopts(join("",@module_options),\%opt);

if ( $opt{v} && $opt{q} ) 
{
    print STDERR " Please specify either -q or -v\n";
    exit(1);
}



my @entries;


#run all modules, create the list of checks to run.
foreach my $m (@modules)
{
  my $n="DUCK::".$m;
 
      $n->run(\%opt,\@entries);
 
}


# inject all options to check modules
$DUCK->setOptions("no-https",$try_https);

# iterate over all urls, run checks.


foreach my $entry (@entries)
{
    my $type=@$entry[0];
    my $k=@$entry[1];
    my $url=@$entry[2];
    my $origline=@$entry[3];
    my $extra=@$entry[4];

    chomp $origline unless !$origline;
  
    if ($funcref->{$k})
    {

  if ($opt{n})
    {
         print STDOUT $type.": ".$k.": ".$url.": ";
	 print STDOUT " DRY RUN\n";
	 next;
    }

	my $res=&{$funcref->{$k}}($url);
	
	if (!defined $res)
	{
	    if (!$opt{q})
	    {
		if (-t STDERR) {print STDERR $color_y;}
		print STDERR "I: Skipping field ".$k." (Reason: Missing helper!)\n";
		if (-t STDERR) {print STDERR $color_n;}
	    }
	}
	else
	{
	    if ($res->{retval}>0)
	    {
		
		if ($res->{'certainty'}) { $extra->{'certainty'}=$res->{'certainty'};}
		
		if ($res->{retval}==2)
		{
		    
		    if (!$opt{q})
		    {
			if (-t STDERR) {print STDERR $color_y;}
			print STDERR display_result($res,$extra);
			if (-t STDERR) {print STDERR $color_n;}
		    }
		    
		} else
		{
		    if (!$opt{q})
		    {
			if (-t STDERR) {print STDERR $color_r;}
			print STDERR display_result($res,$extra);
			if (-t STDERR) {print STDERR $color_n;}
		    }
		    $exitcode=1;
		    
		}
		
	    }
	    else
	{
	    if ($opt{v})
	    {
		if (-t STDOUT) {print STDOUT $color_g;}
		print STDOUT display_result($res,$extra);
		if (-t STDOUT) {print STDOUT $color_n;}
	    }
	    
	}
	}
    }
    
    
    
}

exit($exitcode);

##############################################################################
# Helper functions


sub display_result($;$;$)
{
    
    my $out="";
    my ($res,$data)=@_;
    
    my $prefixes={ 0 => "O: ",
		   1 => "E: ",
		   2 => "I: "
    };

    my $states={ 0 => "OK",
		 1 => "ERROR",
		 2 => "INFORMATION"
    };
    my $P;
    if ($prefixes->{$res->{retval}}) 
    {
	$P=$prefixes->{$res->{retval}};
    }
    else
    {
	$P=$prefixes->{1}; # default to Error if return value >0 and out out bounds
    }
    $out.=$P;
    my $indent=$P;
    $indent =~ s/./ /g;
    
    
# try to print data supplied by check
    if ($data->{verbose})
    {
	$out.=$data->{verbose};
	if ($states->{$res->{retval}})
	{
	    $out.=': '.$states->{$res->{retval}};
        } else
    	{
	    $out.=': '.$states->{1};
    	}
	$out.=' (Certainty:'.$data->{certainty}.')';
	$out.="\n";
    } else

    {


	if ($data->{filename})
	{
	    $out.=$data->{filename}.":";
	}
	if ($data->{linenumber})
	{
	    $out.= $data->{linenumber}.": ";
	}
	
	if ($data->{checkmethod})
	{
	    $out.=$data->{checkmethod}.": ";
	}
	if ($data->{url})
	{
	    $out.=$data->{url}.": ";
	}
	$out.=$states->{$res->{retval}};
	$out.=' (Certainty:'.$data->{certainty}.')';
	$out.="\n";
    }
    
    if ( $res->{response} && ($res->{retval}>0) )
    {
	my $ts=$res->{response};
	$ts =~ s/\n*$//g;
	$ts =~ s/^/$indent/g;
	$ts =~ s/\n/\n$indent/g;

	$out.=$ts;
	
    }


    $out =~ s/\n*$//g;
    $out.="\n\n";
    

    return $out ;
    
}


sub HELP_MESSAGE()
{

if (!$descriptions)
{
    $descriptions="  No modules, found, no further options available.\n";
}
print STDOUT <<EOF;

Usage: duck [options] 

  -h\t--help\t\tdisplay this usage information and exit
  -q\t\t\tquiet mode, suppress all output
  -v\t\t\tverbose mode
  -n\t\t\tdry run, don't run any checks, just show what would be checked
  --modules-dir=dir\tpath to check modules
  --no-https\t\tdo not try to find matching https URLs to http URLs
  --no-color\t\tdo not colorize output
  --missing-helpers\tdisplay list of missing external helper tools
  --version\t\tdisplay copyright and version information
  
  Available module options:

EOF
print $descriptions;
print "\n";
    exit(0);
}

sub VERSION_MESSAGE()
{
    my $DUCK=DUCK->new();
    print "duck ".$DUCK->version()."\n";
    my $copyright_year=$DUCK->copyright_year();

print <<EOF;
This code is copyright $copyright_year by Simon Kainz <simon\@familiekainz.at>
all rights reserved.
This program comes with ABSOLUTELY NO WARRANTY.
You are free to redistribute this code under the terms of the
GNU General Public License, version 2 or later.

EOF
}

sub missingHelpers()
{
    my $DUCK=DUCK->new();
    my $out;
    my $h=$DUCK->getHelpers();
    
    if (!$h->{git}) { $out.= "git missing. Please install package git.\n"; }
    if (!$h->{bzr}) { $out.= "bzr missing. Please install package bzr.\n"; }
    if (!$h->{svn}) { $out.= "svn missing. Please install package subversion.\n"; }
    if (!$h->{hg}) { $out.= "hg missing. Please install package mercurial.\n"; }
    
    return $out;
    

}

