#!/usr/bin/perl
# Copyright(c) René Wagner 2019-2020
# published under BSD 3-Clause License
# https://www.codewolke.net/photo-helpers/

use warnings;
use strict;
use feature qw(say);
use Cwd;
use File::Basename;
use File::Find::Rule;
use File::Find;
use Getopt::Std;

$Getopt::Std::STANDARD_HELP_VERSION = 'true';

my $VERSION = '0.5';
my $PROGRAM = 'Multiversion Sidecar Cleaner';

# read commandline switches
our $opt_d = 0;

if ( !getopts('d') )
{
	die("Invalid parameters provided! See 'silkypix_multiversion_sidecar_cleaner.pl --help' for more details.");
}

# read remaining commandline args
# last dir will win
my $work_dir = getcwd;
foreach my $arg ( @ARGV ) 
{
	if ( -d $arg ) { $work_dir = $arg }
}

delete_files($work_dir, $opt_d);

exit 0;

sub delete_files
{
	my ( $dir, $delete_flag ) = @_;
	my ( $file_counter ) = 0;

	say "working directory: $dir";
	say 'action           : '. ($delete_flag ? 'delete' : 'list only') . "\r\n";
	say 'files with newer versions:';

	# alle Dateien in allen Unterordnern *.spd *.spf suchen

	my @files = File::Find::Rule->file->name( qr/\.sp(d|f)$/i )->in( $dir );
	
	foreach my $file ( @files )
	{
		if ( exist_newer_file($file, @files) )
		{
			print $file .'...';
			$file_counter++;
			if ( $delete_flag )
			{
				if ( unlink $file )
				{
					print 'deleted.'
				}
				else
				{
					print " deletion failed: $!";
				} 
			}
			say '';
		}
	}
	
	say "\r\n$file_counter duplicate file versions found.";
}

sub exist_newer_file
{
	my ( $original_file, @files_hash ) = @_;
	
	if ( -f $original_file )
	{
		# build the regex to find all versions of this file
		my @original_file_parts = split /\./, basename($original_file);

		# don't handle files that doesn't have a version
		if ( scalar @original_file_parts < 4 )
		{
			return 0;
		}

		my $filefinder_regex = "$original_file_parts[0]\\.$original_file_parts[1]\\.[0-9]+\\.$original_file_parts[3]";
		my @version_files = File::Find::Rule->file->name( qr/$filefinder_regex/i )->in( dirname($original_file) );
		
		foreach my $version_file ( @version_files )
		{
			my @version_file_parts = split /\./, $version_file;
			if ( $version_file_parts[2] > $original_file_parts[2] ) { return 1; }
		}
	}
	
	return 0;
}

sub main::VERSION_MESSAGE()
{
	say $PROGRAM . ' - version ' . $VERSION;
	say 'published under BSD 3 - Clause License';
	say 'Copyright(c) 2019-2020 René Wagner';
	say 'https://www.codewolke.net/photo-helpers/';
}

sub main::HELP_MESSAGE
{
	say '';
	say 'Deletes Silkypix Sidecar files when newer versions of the files are available.';
	say 'This usually happens when a file is opened in a newer version of Silkypix, as the sidecar files are version specific.';
	say '';
	say 'commandline parameters:';
	say 'duplicate_sidecar_cleaner.pl - list duplicate sidecar file versions in current working directory and subfolders';
	say 'duplicate_sidecar_cleaner.pl -d - delete duplicate sidecar file versions in given directory and subfolders';	
	say 'duplicate_sidecar_cleaner.pl <folder> - list duplicate sidecar file versions in given directory and subfolders';
	say 'duplicate_sidecar_cleaner.pl -d <folder> - delete duplicate sidecar file versions in given directory and subfolders';
	say 'duplicate_sidecar_cleaner.pl --help - show this help';
}