💾 Archived View for gmn.clttr.info › sources › photo-helpers.git › tree › spmvsc.txt captured on 2023-04-26 at 13:54:10.

View Raw

More Information

⬅️ Previous capture (2023-01-29)

-=-=-=-=-=-=-

#!/usr/bin/perl
# Copyright(c) René Wagner 2019-2020
# https://src.clttr.info/rwa/photo-helpers
# published under BSD 3-Clause License

use Modern::Perl '2019';
use Cwd;
use File::Basename;
use File::Find::Rule;
use Getopt::Std;

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

my $VERSION = '0.6';
my $PROGRAM = 'SilkyPix Multiversion Sidecar Cleaner';

# read commandline switches
our $opt_l = 0;

getopts('l') or die "Invalid parameters provided! See 'spmvsc --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);

exit 0;

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

	say "working directory: $dir";
	say 'action           : '. (!$opt_l ? '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 ( !$opt_l )
			{
				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://git.sr.ht/~rwa/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 'usage: spmvsc [options] <target folder>';
	say '';
	say 'options:';
	say '    -l     : list-only mode - does not delete files but only lists which files would be moved';
	say '    --help : show this help';
}