All pastes #229739 Raw Edit

sorel's folio folly

public text v1 · immutable
#229739 ·published 2006-10-30 22:17 UTC
rendered paste body
#!/usr/bin/perl -w
#
# Straighten the numbering if the first verso page was chosen incorrectly
# This should be pretty unlikely, but eh, stuff happens.
#

if (@ARGV != 2) {
        printf "Usage: $0 folio|verso ###\n";
        exit;
}

if ($ARGV[0] eq "folio") { # then we had not enough verso pages
        opendir(DIR, ".") || die "can't get directory";
        @allfiles = grep { (/_folio.jpg/ || /_verso.jpg/) && !/thumb_/ } readdir(DIR);
        closedir DIR;
        @foliofiles = sort grep { /_folio/ } @allfiles;
        @versofiles = sort grep { /_verso/ } @allfiles;

        @fprts = split(/_/, $foliofiles[-1]);
        $lastfolio = $fptrs[0];

        @fprts = split(/_/, $versofiles[-1]);
        $lastverso = $fptrs[0];

        print "allfiles: @allfiles\nfoliofiles: @foliofiles\nversofiles: @versofiles\n";
        print "verso: $versofiles[-1],$lastverso\n folio: $foliofiles[-1],$lastfolio\n";

        # Number of pages to shift by, blank ones count too
        $offset = $lastfolio - $ARGV[1];

        # Now shift all verso by offset, then tack offset of folio onto the start
        # I can't believe I made this count from 1... ewww
        for ($curverso = $lastverso; $curverso > 0; $curverso--) {
                @mvargs = ("mv", sprintf("%03d", $curverso)."_verso.jpg",
                                 sprintf("%03d", $curverso+$offset)."_verso.jpg");
                print "@mvargs \n";
#               system(@mvargs);
                @mvargs = ("mv", "thumb_".sprintf("%03d", $curverso)."_verso.jpg",
                                 "thumb_".sprintf("%03d", $curverso+$offset)."_verso.jpg");
                print "@mvargs \n";
#               system(@mvargs);
        }
        # Now we shift
        $curverso = $offset;
        for ($curfolio = $lastfolio; $curfolio >= $ARGV[1]; $curfolio--) {
                $curverso--;
                @mvargs = ("mv", sprintf("%03d", $curfolio)."_folio.jpg",
                                 sprintf("%03d", $curverso)."_verso.jpg");
                print "@mvargs \n";
#               system(@mvargs);
                @mvargs = ("mv", "thumb_".sprintf("%03d", $curfolio)."_folio.jpg",
                                 "thumb_".sprintf("%03d", $curverso)."_verso.jpg");
                print "@mvargs \n";
#               system(@mvargs);
        }

} elsif ($ARGV[0] eq "verso") { # then we had too many verso pages
        opendir(DIR, ".") || die "can't get directory";
        @allfiles = grep { (/_folio.jpg/ || /_verso.jpg/) && !/thumb_/ } readdir(DIR);
        closedir DIR;
        @foliofiles = grep { /_folio/ } @allfiles;
        @versofiles = grep { /_verso/ } @allfiles;

        @fprts = split(/_/, $foliofiles[-1]);
        $lastfolio = $fptrs[0];

        @fprts = split(/_/, $versofiles[-1]);
        $lastverso = $fptrs[0];

        # Number of pages to shift by, blank ones count too
        $offset = $ARGV[1];

        $curfolio = $lastfolio;
        for ($curverso = 1; $curverso <= $offset; $curverso++) {
                $curfolio++;
                @mvargs = ("mv", sprintf("%03d", $curverso)."_verso.jpg",
                                 sprintf("%03d", $curfolio)."_folio.jpg");
                print "@mvargs \n";
#               system(@mvargs);
                @mvargs = ("mv", "thumb_".sprintf("%03d", $curverso)."_verso.jpg",
                                 "thumb_".sprintf("%03d", $curfolio)."_folio.jpg");
                print "@mvargs \n";
#               system(@mvargs);
        }
        for ($curverso = $offset+1; $curverso <= $lastverso; $curverso++){
                @mvargs = ("mv", sprintf("%03d", $curverso)."_verso.jpg",
                                 sprintf("%03d", $curverso-$offset)."_verso.jpg");
                print "@mvargs \n";
#               system(@mvargs);
                @mvargs = ("mv", "thumb_".sprintf("%03d", $curverso)."_verso.jpg",
                                 "thumb_".sprintf("%03d", $curverso-$offset)."_verso.jpg");
                print "@mvargs \n";
#               system(@mvargs);
        }
} else {
        print "Usage: $0 folio|verso ###\n";
        exit;
}

# Output:
#    tilia% ./firstverso.pl folio 4
#    allfiles: 001_folio.jpg 002_folio.jpg 003_folio.jpg 004_folio.jpg 005_folio.jpg 
#    005_verso.jpg 004_verso.jpg 003_verso.jpg 002_verso.jpg 001_verso.jpg
#    foliofiles: 001_folio.jpg 002_folio.jpg 003_folio.jpg 004_folio.jpg 005_folio.jpg
#    versofiles: 001_verso.jpg 002_verso.jpg 003_verso.jpg 004_verso.jpg 005_verso.jpg
#    Use of uninitialized value in concatenation (.) or string at ./firstverso.pl line 26.
#    Use of uninitialized value in concatenation (.) or string at ./firstverso.pl line 26.
#     verso: 005_verso.jpg,
#     folio: 005_folio.jpg,
#    Use of uninitialized value in subtraction (-) at ./firstverso.pl line 29.
#    Use of uninitialized value in numeric gt (>) at ./firstverso.pl line 33.
#    Use of uninitialized value in numeric ge (>=) at ./firstverso.pl line 45.