#!/usr/bin/perl -w
# create-indexes2.pl - Creating thumbnail indices
# for picture galleries version 2.0 (August 2001)
# written by M. Verbitsky
# The first version was completed in October 1999
# Together with create-thumbnails.pl it produced
# a basic images gallery from an hierarchy of
# image directories
# The new version works to the same purpose
# Changes:
# 1. create-indexes.pl gave the total size of each directory, in Kb.
# Now it gives also the number of files in the hierarchy
# 2. The files .header, .footer are used as a header
# and a footer of each directory (the syntax is plain).
# If some subdirectory contains its own
# .header or .footer, it is added to the
# global .header and .footer for the whole
# hierarchy of subdirectories. In version 1
# header and footer were determined globally
# as variables - not as flexible
# 3. If a directory contains a file .dirpic, its entry in
# the index file is illustrated with the corresponding picture.
# Version 2.1: April 2003. Added support for .name files from
# indexes-nemirov.pl
# Set variables
$MAX_IND=15; # po skol'ku kartinok v odnom indekse?
# $ROWS=4;
$COLUMNS=3; #struktura indeksov
$DIRECTORIES_COLS=3; #skol'ko direktorij v strochke shapki
$PREFIX="000thu-"; # slovo, s kotorogo nachinayutsya thumbnail-y
# LANGUAGE
$SUBDIRS_ARE_CALLED = "Directories:";
$GALLERIES_ARE_CALLED = "Galleries: "; # galleries:
#nachalo i konec indeksov vyneseny v otdelnye fajly!
# MAIN () BEGINS HERE
my $num_args = 0;
$num_args = @ARGV;
if ($num_args==0)
{
print "\n usage: create-indexes2.pl [directory]\n";
exit;
}
# Determine .tmpheader, .tmpfooter, .tmpnumber
# in each directory - files with header,
# footer and the number of files
# (added in version 2.0)
determine_header("$ARGV[0]/");
determine_footer("$ARGV[0]/");
determine_tmpnumber("$ARGV[0]/");
@dir_list=glob("$ARGV[0]/ $ARGV[0]/*\/ $ARGV[0]/*/*\/ $ARGV[0]/*/*/*\/ $ARGV[0]/*/*/*/*\/ $ARGV[0]/*/*/*/*/*\/ $ARGV[0]/*/*/*/*/*/*\/ $ARGV[0]/*/*/*/*/*/*/*\/ $ARGV[0]/*/*/*/*/*/*/*/*\/");
foreach $dir (@dir_list)
{
open(HDR, "< $dir/.tmpheader") || die "couldn't open $dir/.tmpheader";
@BEGINNING_list = ;
close(HDR);
$BEGINNING = join(" ", @BEGINNING_list);
open(FTR, "< $dir/.tmpfooter") || die "couldn't open $dir/.tmpfooter"; ;
@ENDING_list = ;
close(FTR);
$ENDING = join(" ", @ENDING_list);
#chistim za soboj
foreach (<$dir/index*>)
{
unlink || die "having trouble deleting $_: $!";
print "deleting $_\n";
}
@all_listing = glob("$dir*");
# sozdaem shapku
@directory_listing=();
foreach $directory (@all_listing)
{ if (-d $directory)
{
push (@directory_listing, $directory);
}
}
# delaem prostuyu zagotovku shapki
@shapka=();
$numdirs=0;
foreach $directory (@directory_listing)
{
$numdirs++;
# opredelyaem razmer
system("du -s $directory > tode");
open (DU, "tode") || die ("couldn't open tode");
$size=;
$size=~ /(^\S*)/;
$size =$1;
close(DU);
unlink("tode") || die "having trouble deleting tode: $!";
# opredelili razmer
# A nuzhno li ehto? Poka ostavlyu...
# determining number of pics in all subdirectories
# (via .tmpnumber files)
open (TMPNUMBER, "< $directory/.tmpnumber")
|| die "couldn't open $directory/.tmpnumber!\n";
my @tmpn_contents = ;
close (TMPNUMBER);
my $numpics_dir = $tmpn_contents[0];
# finding .dirpic
my $dirpic = "";
if (-r "$directory/.dirpic")
{
open(DIRPIC, "< $directory/.dirpic");
my @dirpic_contents = ;
close(DIRPIC);
$dirpic = "$dirpic_contents[0]";
# adding 000thu- before the filename - takes 2 regexp replaces
# if there are "/" in dirpic
if ( $dirpic =~ /\//)
{
my $dirpic_file = $dirpic;
$dirpic_file =~ s/.*\///; # filename
my @path = split (/\//, $dirpic);
pop(@path);
my $dirpic_path = join("/",@path); # the path to the picture
$dirpic = "$dirpic_path/000thu-$dirpic_file";
}
else
{
$dirpic ="000thu-$dirpic";
}
}
# finding NAME of a subdirectory
my @NAME =();
if (-r "$directory/.name")
{
open(TITLEDIR, "< $directory/.name");
@NAME = ;
close(TITLEDIR);
} else {
$directory=~ s/[\.\/]*//;
@NAME = ("$directory");
}
# directory parameters are determined
$directory=~ s/.*\///; # ubiraem path,
# predshestvuyushchij nazvaniyu
if ($dirpic ne "")
{
$dirpic="\\ ";
}
@shapka = (@shapka, "\
");
# For a subdirectory with a number divisible by
# DIRECTORIES_COLS we add an extre linebreak
$numdirs_modulo = $numdirs % $DIRECTORIES_COLS;
if ($numdirs_modulo == 0)
{
push (@shapka, "\
\
");
}
}
# dobavlyaem v shapku tablicu subdirektorij, esli oni sushchestvuyut
if (defined($shapka[0]))
{
unshift(@shapka, "\ $SUBDIRS_ARE_CALLED \
\
\
");
push (@shapka, "\
\
");
}
# finding NAME of a directory
my @NAME;
if (-r "$dir/.name")
{
open(TITLEDIR, "< $dir/.name");
@NAME = ;
close(TITLEDIR);
} else {
my $dirtitle = $dir;
$dirtitle=~ s/^[\.\/]*//;
$dirtitle=~s/[\.\/]*$//;
# clean ../, ./ and so on
@NAME = ("$dirtitle");
}
@shapka = ( $BEGINNING, "\
", @NAME, "\
", @shapka);
# shapka gotova
#teper' udalyaem iz spiska imena direktorij
#i vse, chto nachinaetsya na prefiks
# s cel'yu poluchit' spisok fajlov dlya indeksa
@file_listing=();
foreach $file (@all_listing)
{ if ((-f $file) and not ($file =~ /$PREFIX/))
{
push (@file_listing,$file);
# $numfiles=@file_listing;
# print "new file listed: $file; file_listing is $numfiles long\n";
}
}
#creating indices
$numfiles=@file_listing;
$numindexes =$numfiles/$MAX_IND+1;
print " numfiles =$numfiles, numindexes= $numindexes, current dir = $dir\n";
for ($index=1 ; $index<= $numindexes; $index++)
{
@index_contents =();
for ($i= (($index-1)*$MAX_IND);
($i< $index*($MAX_IND) and ($i < $numfiles)); $i++)
{
$file= $file_listing[$i];
#print " i = $i"; print " index = $index "; print "file = $file \n";
$file=~ s/.*\///; # ubiraem path,
#predshestvuyushchij nazvaniyu fajla
push (@index_contents, $file);
}
@index_html=();
# making rows
$numfiles_index= @index_contents;
$Rows = $numfiles_index/$COLUMNS;
for ($Row =0; $Row < $Rows; $Row++)
{
push (@index_html, "\
\
\n");
for ($column=0;
(($column < $COLUMNS) and
defined($index_contents[$Row*$COLUMNS+$column]));
$column++)
{
$current=$index_contents[$Row*$COLUMNS+$column];
$Size=(stat("$dir/$current"))[7]/1000;
push (@index_html, "\
\$GALLERIES_ARE_CALLED \");
if ($numfiles>0)
{
push (@index_html, @indices_bar);
unshift (@index_html, @indices_bar);
} #esli net kartinok, linejka ne nuzhna
# dobavlyaem ENDING
push (@index_html, $ENDING);
unshift (@index_html, @shapka);
# pishem
$indexname= "$dir/index" . "$index" . ".html";
open (INDEX, ">$indexname") ||
die ("couldn't open $indexname");
print INDEX "@index_html "
|| die ("couldn't write to $indexname");
close (INDEX) || die ("couldn't close $indexname");
}
symlink("index1.html" ,"$dir/index.html") || warn "couldn't symlink $dir/index1.html to $dir/index.html";
}
# Determine .tmpheader, .tmpfooter, .tmpnumber
# in each directory - files with header,
# footer and the number of files
# (added in version 2.0)
sub determine_tmpnumber
{
my $dir = $_[0];
#the list of subdirectories
my @all_listing = glob("$dir/*");
my @directory_listing=();
foreach $directory (@all_listing)
{
if (-d $directory)
{
push (@directory_listing, $directory);
}
}
# How many files are there? Not counting .html and 000thu-
my $number_of_files=0;
foreach $file (@all_listing)
{
if (( -f $file) && !($file =~/html$/) && !($file =~/000thu-/))
{ $number_of_files++;}
}
# determine the number recursively
my $number_of_dirs=@directory_listing;
# if no subdirectories, no recursion needed
if ($number_of_dirs==0)
{
open(TMPNUMBER, "> $dir/.tmpnumber")
|| die ("couldn't open $dir/.tmpnumber: $!\n");
#print "opening $dir/.tmpnumber. 0=number_of_dirs = $number_of_dirs \n";
print TMPNUMBER $number_of_files;
close(TMPNUMBER);
}
else
{
my $tmpnumber = $number_of_files;
foreach $subdir (@directory_listing)
{
# print " obrabatyvaem $subdir\n";
determine_tmpnumber($subdir);
open (TMP_SUBDIR, "< $subdir/.tmpnumber")
|| die ("couldn't open $subdir/.tmpnumber: $!\n");
$tmp_subdir = ;
close(TMP_SUBDIR);
$tmpnumber = $tmpnumber + $tmp_subdir;
}
open(TMPNUMBER, "> $dir/.tmpnumber")
|| die ("couldn't open $dir/.tmpnumber: $!\n");;
#print "opening $dir/.tmpnumber. number_of_dirs = $number_of_dirs \n";
print TMPNUMBER $tmpnumber;
close(TMPNUMBER);
}
}
# Using recursion again to go through the directory tree
# This is needed for hierarchy of .footer and .header files
sub determine_header
{
my $dir = $_[0];
#the list of subdirectories
my @all_listing = glob("$dir/*");
my @directory_listing=();
foreach $directory (@all_listing)
{ if (-d $directory)
{
push (@directory_listing, $directory);
}
}
# The list of subdirectories is created. Now creating .tmpheader
my @headers = ();
if (-r "$dir/../.tmpheader")
{
open(ABOVEHEADER, "< $dir/../.tmpheader");
@headers = ;
close(ABOVEHEADER);
}
if (-r "$dir/.header")
{
open(HEADER, "< $dir/.header");
my @this_header =;
close(HEADER);
foreach $tmp (@this_header)
{
push(@headers, $tmp);
}
}
# Writing @headers
open(TMPHEADER, ">$dir/.tmpheader") ||
die ("couldn't open $dir/.tmpheader: $!\n");
print TMPHEADER @headers;
close(TMPHEADER);
# Recursion
foreach $directory (@directory_listing)
{
determine_header($directory);
}
}
#same with footer - obtained by query-replace
# Why? To simplify syntax, that's why.
sub determine_footer
{
my $dir = $_[0];
#the list of subdirectories
my @all_listing = glob("$dir/*");
my @directory_listing=();
foreach $directory (@all_listing)
{ if (-d $directory)
{
push (@directory_listing, $directory);
}
}
# The list of subdirectories is created. Now creating .tmpfooter
my @footers = ();
if (-r "$dir/../.tmpfooter")
{
open(ABOVEFOOTER, "< $dir/../.tmpfooter");
@footers = ;
close(ABOVEFOOTER);
}
if (-r "$dir/.footer")
{
open(FOOTER, "< $dir/.footer");
my @this_footer =