#!/usr/bin/perl

# A "salami machine" for readying data for WWW/Mosaic
#
# (C) Rob Hartill 1994

# 1st read the configuration file...

$config_file = $0;
$config_file =~ s#.pl$#.ph#;

open ( CONFIG , "$config_file" ) || die "Error. I was unable to open $config_file";

@configuration = <CONFIG>;

close ( CONFIG );

# Process the configuration file
while ( $#configuration ) {
   $setting = shift(@configuration);
   next if $setting =~ /^#/ || $setting eq "\n";

   chop $setting;
   ($variable, $value) = split(/ is /,$setting);

   $set{$variable} = $value;
}

#-- -- -- -- -- -- -- -- -- -- -- -- -- -- --
# Process any configuration settings

$set{STRIPFROMURL} =~ s/(\W)/\\$1/g;

#-- -- -- -- -- -- -- -- -- -- -- -- -- -- --
# Ok lets try to open the input file

#print "sort $set{SORTFLAGS} -t\\$set{DELIMITER} $set{INFILE} \n";

if ( $set{SORTFLAGS} ne "none" ) {
   open ( INFILE , "sort $set{SORTFLAGS} -t\\$set{DELIMITER} $set{INFILE} |" ) || die "Error, I was unable to open $set{INFILE}";
} else {
   open ( INFILE , "$set{INFILE}" ) || die "Error, I was unable to open $set{INFILE}";
}

$records_output_this_page = 0;
$records_output_this_group = 0;

$this_index = 1;   # INDEXLENGTH links per index page. Start with index_1.html
$links_indexed = 0;

@fields = split(/,/, $set{ORDER});

if ( $set{THUMBNAIL} =~ /ON/i ) {
  # we want thumnails, so treat the first field as the thumbnail

  $thumbnail = shift(@fields);
}

$partnumber = 0;
$page = "";
&open_new_part();

while ( <INFILE> ) {
   chop;
   @line = split(/$set{DELIMITER}/, $_);
   unshift(@line,"");  # add an empy element so that the real array starts ind 1

   $lastindex = @line[$set{SORTFIELD}];
   $firstindex = $lastindex if !defined $firstindex;

   if ( (defined $last_divide && $set{DIVIDEFIELD} != 0 &&
          $last_divide ne @line[$set{DIVIDEFIELD}]) 
         || $records_output_this_page == $set{PAGESIZE} ) {

       if ( $nail ne "" || $group ne "" ) {
          $nails =~ s/\.JPG"/\.GIF"/g;
          $page .= "$nails<p>\n$group\n";
       }

       print PART "<H2>Entries $firstindex to $lastindex</H2>\n";  

       if ( $set{DIVIDEFIELD} != 0 ) {
           $extra = "$set{DIVTITLE} @line[$set{DIVIDEFIELD}]: ";
       } else {
           $extra = "";
       }

       if ( $firstindex ne $lastindex ) {
           $lastindex = "to $lastindex";
       } else { 
           $lastindex = "";
       }
       $big_index .= "<LI>$extra<A HREF=\"Part$partnumber".".html\">$firstindex $lastindex</A></LI>\n";

       if (++$links_indexed == $set{INDEXLENGTH} ) {
           &new_index();
       }

       print PART "$page\n";

       $group = "";
       $nails = "";
       $page = "";
       undef $firstindex;

       $records_output_this_group = 0;
       $records_output_this_page = 0;

       &open_new_part();

   }

   $last_divide = @line[$set{DIVIDEFIELD}];

   @line[$set{LINKFIELD}] =~ s/$set{STRIPFROMURL}//;

   if ( $set{NUMBEREDDIRECTORIES} eq "ON" ) {
      $numbdir = @line[$set{LINKFIELD}];
      $numbdir =~ s/[^0-9]//g;
      $numbdir = int($numbdir / 100);
      $numbdir = sprintf("%04d/",$numbdir);
   } else {
      $numbdir = "";
   }


   if ( $thumbnail ) {
       $nails .= "<IMG ALT=\"THUMBNAIL\" SRC=\"$set{IMAGEURL}$numbdir@line[$set{LINKFIELD}]\">";
   }

   #-- display one the current record.
   foreach $field ( @fields ) {
 
      if ( $set{HOTFIELD} == $field ) {
         $line[$field] = "<A HREF=\"$set{IMAGEURL}$numbdir@line[$set{LINKFIELD}]\">@line[$field]</A>"
      }
      $group .= " @line[$field]";
   }
   $group .= "<br>\n";
   $records_output_this_page++;
   $records_output_this_group++;

   if ($records_output_this_group >= $set{GROUPSIZE} ) {

       $nails =~ s/\.JPG"/\.GIF"/g;

       $page .= "$nails<p>\n$group\n";

       $group = "";
       $nails = "";

       $records_output_this_group = 0;
   }
}

if ( $page ne "" || $nails ne "" || $group ne "" ) {

   $nails =~ s/\.JPG"/\.GIF"/g;
   $page .= "$nails<p>\n$group\n";

   print PART "<H2>Entries $firstindex to $lastindex</H2>\n";  
   print PART "$page\n";
    
   if ( $set{DIVIDEFIELD} != 0 ) {
       $extra = "$set{DIVTITLE} @line[$set{DIVIDEFIELD}]: ";
   } else {
       $extra = "";
   }
   if ( $firstindex ne $lastindex ) {
       $lastindex = "to $lastindex";
   } else { 
       $lastindex = "";
   }

   $big_index .= "<LI>$extra<A HREF=\"Part$partnumber".".html\">$firstindex $lastindex</A></LI>\n";
   close(PART);
}  

&new_index();


#--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---

sub new_index {

open ( MAININDEX, ">$set{OUTDIR}/index_$this_index".".html") || die "Error, I was unable to open  $set{OUTDIR}/index_$this_index".".html";

print MAININDEX "<HTML>
<HEAD>
<TITLE>$set{INDEXHEADER}</TITLE>
</HEAD>

<BODY>
<H1>$set{INDEXHEADER}</H1>
<HR>
<OL>
$big_index
</OL>

Index page: ";

printf MAININDEX "<A HREF=\"index_1.html\">[First]" if $this_index >1;

printf MAININDEX "<A HREF=\"index_%s.html\">[Previous]",$this_index-1 if $this_index >2;

printf MAININDEX "<A HREF=\"index_%s.html\">[Next]",$this_index+1 if $links_indexed == $set{INDEXLENGTH};




print MAININDEX "
</BODY>
</HTML>
";

close(MAININDEX);

$this_index++;
$big_index = "";
$links_indexed = 0;
}



#--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---

sub open_new_part {

   print PART "<BR>\nPage: ";

   printf PART "<A HREF=\"Part1.html\">[First]" if $partnumber >2;

   printf PART "<A HREF=\"Part%s.html\">[Previous]",$partnumber-1 if $partnumber >1;
   printf PART "<A HREF=\"Part%s.html\">[Next]",$partnumber+1;
   printf PART "   <A HREF=\"index_1.html\">[Index]";


   close (PART);
   $partnumber++;
   
   open ( PART, ">$set{OUTDIR}/Part$partnumber".".html") || die "Error, I was numable to open  $set{OUTDIR}/Part$partnumber".".html";
   printf PART "<HTML>
<HEAD>
<TITLE>%s</TITLE>
</HEAD>

<BODY>
<H1>%s</H1>
<P>Page: <B>$partnumber</B></P>",$set{RUNNINGHEADER},$set{RUNNINGHEADER};

}

   
