💾 Archived View for gmn.clttr.info › sources › art-helpers.git › tree › afm.txt captured on 2024-02-05 at 10:02:33.

View Raw

More Information

⬅️ Previous capture (2023-06-14)

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

#!/usr/bin/perl
# Copyright(c) René Wagner 2019-2023
# https://git.sr.ht/~rwa/art-helpers
# published under BSD 3-Clause License 

use Modern::Perl '2019';
use Cwd;
use File::Basename;
use File::Copy;
use File::Find::Rule;
use File::Spec::Functions;
use File::Path qw(make_path);
use File::HomeDir;
use Getopt::Std;

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

my $VERSION = '1.1';
my $PROGRAM = 'ART File Mover';

my $configfile = catfile(File::HomeDir->my_home, '.afm.conf');

# read commandline switches
our $opt_l = 0;
our $opt_R = 'DNG';
our $opt_F = 'JPEG';
our $opt_E = 'jpe{0,1}g';
our $opt_C = '';
our $opt_f = '';

getconfig();

getopts('lR:F:E:C:f:') or	die "Invalid parameters provided! See 'afm --help' for more details.";

# read remaining commandline args for destination dir
# last dir will win
my $destination_dir = getcwd;
foreach my $arg ( @ARGV ) 
{
	$destination_dir = $arg;
}

# fallback to list-only mode when no dir is given
if ($destination_dir eq getcwd) { $opt_l = 1; }

writeconfig();

move_files($destination_dir);

exit 0;

sub move_files
{
	my ( $destination_dir ) = @_;
	my $move_counter = 0;

	say "destination dir  : $destination_dir";
	($opt_C eq '') or say "copy dir         : $opt_C";
	say "file filter      : $opt_f";
	say "raw file ext     : $opt_R";
	say "output file dir  : $opt_F";
	say "output file ext  : $opt_E";
	say 'action           : '. ($opt_l ? 'list only' : 'move');
	say '';
	my $converted_destination_dir = catfile($destination_dir, $opt_F);

	if ( !$opt_l && (! -d $destination_dir || ! -d $converted_destination_dir))
	{
		if (! -d $destination_dir ) { make_path $destination_dir or die "Could not create destination dir $_ : $!"; }
		if (! -d $converted_destination_dir ) { make_path $converted_destination_dir or die "Could not create destination dir $_ : $!"; }
	}

	# find all jpegs in current dir
	my @files = File::Find::Rule->file()->name( qr/\.$opt_E$/i )->name( qr/$opt_f/ )->maxdepth(1)->in( catfile(getcwd, $opt_F) );

	foreach my $converted_file ( @files )
	{
		my ( $filename, $path, $ext ) = fileparse($converted_file, qr/\.[^.]*/);
		my $outarp_file = $converted_file. '.out.arp';
		my $raw_file = catfile(dirname($path), $filename .'.'. $opt_R);
		my $rawarp_file = $raw_file. '.arp';
		my $rawarp2_file = $filename. '.arp';
		my $xmp_file = $raw_file. '.xmp';
		my $xmp2_file = $filename. '.xmp';

		print $raw_file .'...';

		$move_counter++;
		# do not move files that have no raw-file anymore
		if ( ! -e $raw_file )
		{
			print 'raw-file not found!';
		}
		elsif ( !$opt_l )
		{
			# only warn if JPEG or RAW file could not be moved
			# the processing files *.arp are optional and are fine to be absent
			move($outarp_file, catfile($converted_destination_dir, basename($outarp_file)));
			move($rawarp_file, catfile($destination_dir, basename($rawarp_file)));
			move($rawarp2_file, catfile($destination_dir, basename($rawarp2_file)));
			move($xmp_file, catfile($destination_dir, basename($xmp_file)));
			move($xmp2_file, catfile($destination_dir, basename($xmp2_file)));

			if ($opt_C ne '' && -d $opt_C)
			{
				copy($converted_file, catfile($opt_C, basename($converted_file)));
			}

			if ( move($converted_file, catfile($converted_destination_dir, basename($converted_file))) && 
				 move($raw_file, catfile($destination_dir, basename($raw_file))	)) 
			{ 
				print 'moved.';
			}
			else
			{
				print "move failed: $!";
			}
		}
		say '';
	}
	
	say "\r\nfound $move_counter files.";
}

sub getconfig
{
	if ( -f $configfile ) { do $configfile; }
}

sub writeconfig
{
	open(my $filehandle, '>', $configfile) or die "Could not open file '$configfile': $!";
	say $filehandle '$opt_F="'. $opt_F .'";';
	say $filehandle '$opt_E="'. $opt_E .'";';
	say $filehandle '$opt_R="'. $opt_R .'";';
	say $filehandle '$opt_C="'. $opt_C .'";';
	close $filehandle;
}

sub main::VERSION_MESSAGE()
{
	say $PROGRAM . ' - version ' . $VERSION;
	say 'published under BSD 3 - Clause License';
	say 'Copyright(c) 2019-2023 René Wagner';
	say 'https://git.sr.ht/~rwa/art-helpers';
}

sub main::HELP_MESSAGE
{
	say '';
	say 'Find all by ART converted files in the current folder and their corresponding RAWs and move them to specified directory.';
	say '';
	say 'usage: afm [options] <target folder>';
	say '';
	say 'options:';
	say '   -l          : list-only mode - does not move files but only lists which files would be moved';
	say '   -f <exp>    : filter files by regex - only raw files that match the PCRE are processed';
	say '                 needs to be quoted';
	say '   -R <raw>    : override the raw extensions, defaults to "DNG"';
	say '                 This option is automatically saved in the user config and can be omitted on the next run';
	say '   -F <folder> : override the folder for converted files, defaults to "JPEG"';
	say '                 This option is automatically saved in the user config and can be omitted on the next run';
	say '   -E <ext>    : override the converted file extenion (case-insensitive), defaults to "jp(e)g"';
	say '                 Perl-compatible regular expressions allowed';
	say '                 This option is automatically saved in the user config and can be omitted on the next run';
	say '   -C <folder> : copy the converted (output) files (ie jpegs) to an additional directory';
	say '                 This option is automatically saved in the user config and can be omitted on the next run';
	say '   --help      : show this help';
}