Source

overdiff / overdiff.pl

#!/usr/bin/perl -w
#
# This program is a quick wrapper around the Perl Text::Diff CPAN module.
# It's purpose at the moment is just to explore how easily we can use
# it for changes to configuration files during package installation.
#
# Debian dependencies:
# libtext-diff-perl
#

use strict;
use diagnostics;
use warnings;

#use autodie qw(:all);
use Text::Diff;
use Getopt::Std;
use Data::Dumper;
#----------------------------------------------------------------------------
# Standard system errors
use constant EXIT_SUCCESS => (0);
use constant EXIT_FAILURE => (1);
#----------------------------------------------------------------------------
sub Main();
sub Diff(@);
#----------------------------------------------------------------------------
sub Main()
{
	my %Opts = ( );
	my @Params = ( );
	my $endArgs = 0;
	foreach my $arg ( @ARGV ) {
		if ( !$endArgs && $arg eq '--' ) {
			$endArgs = 1;
			next;
		}
		next if ( !$endArgs && index($arg, '-') == 0 ); # Let getopts() handle options
		push(@Params, $arg);
	}
	getopts('u', \%Opts);
	eval {
		Diff(
			FileA => $Params[0],
			FileB => $Params[1],
			Unified => $Opts{'u'}
		);
	};
	if ( $@ ) {
		printf(STDERR "ERROR: %s\n", $@);
		return EXIT_FAILURE();
	}
	return EXIT_SUCCESS();
}
#----------------------------------------------------------------------------
sub Diff(@)
{
	my ( $failName );
	my %Params = @_;

	# Do all error handling first.
	die "Two files must be specified for comparison\n"
		unless ( $Params{FileA} && $Params{FileB} );

	foreach my $f ( ($Params{FileA}, $Params{FileB})) {
		if ( open(my $testH, '< ' . $f) ) {
			close($testH);
		} else {
			$failName = $f;
			last;
		}
	}
	die(sprintf("Error opening \'%s\' - %s\n", $failName, $!))
		if ( $failName );

	# Now let's do tje comparison.
	#my $diff = diff $Params{FileA}, $Params{FileB}, { STYLE => "Context" };
}
#----------------------------------------------------------------------------
exit(Main());
#----------------------------------------------------------------------------
1;