RSS Git Download  Clone
Raw Blame History
#!/usr/bin/env perl

# creates YAML .cfg file for use with web_page_from_db.pl
# list of images in format:
# eg: IMG_0100, IMG_0101,IMG_0110..0113, IMG_0119 or a folder name containing images

use v5.26; # say
use autodie;
use strict;
use warnings;

# using system perl & local-lib - see web_page_from_db.pl
use lib '/home/raj/perl5/lib/perl5'; # no longer in PERL5LIB due to plenv

use List::Util qw(first);
use Data::Printer use_prototypes => 0;
use IO::YAML;
use IO::All;

use vars qw($path $imgs $dir $tags $h2 $h4 $skip_imgs $skip_carousel); # STDIN vars:
{ # get vars from stdin:
	print "enter path to index.html under ~/scripts/exifdata/folio/: ";
	chomp ( $path = <STDIN> );

	print "\nenter level 2 heading: ";
	chomp ( $h2 = <STDIN> );

	print "\nenter level 4 heading: ";
	chomp ( $h4 = <STDIN> );

	print "\nenter list of jpgs (eg IMG_0100, IMG_0110..0120) or leave blank for dir:\n";
	chomp ( $imgs = <STDIN> );

	if (! $imgs ) {
		print "\npath to images folder under /mnt/nas_images:\n";
		chomp ( $dir = <STDIN> ); # warn $dir;
		die "$dir doesn't exist" unless -e "/mnt/nas_images/$dir";

        print "\nenter list of jpgs to skip (eg IMG_0100, IMG_0110..0120)\n";
        chomp ( $skip_imgs = <STDIN> );
	}

	print "\nadditional tag groups [focus]:\n";
	chomp ( $tags = <STDIN> );

	print "\nskip carousel [0|1]?:\n";
	chomp ( $skip_carousel = <STDIN> );
	# p [$path, $imgs, $dir, $tags, $h2, $h4, $skip_carousel];
}

# full path to new dir:
my $full_path = "/home/raj/scripts/exifdata/folio/${path}";

my $re = '(.*)\s?\.\.\s?(.*)';

# convert eg IMG_099, IMG_0100 .. IMG_0110 -> IMG_099, IMG_100, IMG_101, etc
my @images = expand_image_list($imgs);

my @skip_imgs = expand_image_list($skip_imgs); # p \@skip_images;

# create dir:
unless (-e $full_path) {
    say "creating $full_path";
	io($full_path)->mkpath;
}

{ # create YAML file:
	my $cfg = "$full_path/.cfg"; # warn $cfg;

	my $h = {
		h2  => $h2,
		h3  => undef,
		h4  => $h4,
	};
	# add list of images or folder name:
	if ( @images ) {
		$h->{img} = [ map $_.'.JPG', @images ];
	}
	elsif ($dir) {
		$h->{dir} = $dir;
        if ( @skip_imgs ) {
            $h->{skip_img} = [ map $_.'.JPG', @skip_imgs ];
        }
	} # p $h;

	if ( $tags ) {
		my %t = map { $_ => 1 } split /[\,\s]+/, $tags;
		$h->{tag_group} = \%t;
	}

	$h->{skip_carousel} = ($skip_carousel); # 1 or 0

	my $io = IO::YAML->new;
	$io->open('>'.$cfg) or die "Couldn't open $cfg - $!";

	print $io $h;
    say "created new .cfg in $full_path";
}

sub expand_image_list {
    my $imgs = shift || return; # p $imgs;

    my @items = split /[\,\s]+/, $imgs; # p \@items;

    for my $i(@items) {
        if ( $i =~ /$re/ ) { # eg IMG_0100 .. IMG_0110
            my ($a,$b) = ($1,$2); # p [$a, $b];
            my ($begin_str) = $a =~ /(\D+)/;
            my ($begin_num) = $a =~ /(\d+)/;
            my ($end_num)   = $b =~ /(\d+)/; # p [$begin_str, $begin_num, $end_num];

            # replace element containing '..' with new list:
            my $index = first { $items[$_] eq $i } 0..$#items; # warn $index;

            my @new = ( $begin_num .. $end_num ); # p \@new;
            splice(@items, $index, 1, map $begin_str . $_, @new);
        }
    } # p \@items;
    return @items;
}