#!/usr/bin/perl -w
#-------------------------------------------------------------------------------
# listbooks.pl - this creates a list of ebook titles in a file
#  written by Eric Pence
#-------------------------------------------------------------------------------
use strict;
use CGI qw(-oldstyle_urls :standard);
use CGI::Carp qw(fatalsToBrowser);
use FileHandle;

BEGIN {
  # Set up error log file
  my $log_file = "error_log.txt";
  use CGI::Carp qw(carpout);
  open(LOG, ">>$log_file") or die "Unable to append to error log: $!";
  carpout(*LOG);
}

# Get directory contents
my $workdir = 'C:/Documents and Settings/Eric/My Documents/My Books/All books';
my @dir_contents = ();
opendir(DIR,$workdir) || die("Cannot open directory !\n");
@dir_contents = sort readdir(DIR);
closedir(DIR);

my $list_file = "c:\\web\\penceland\\cgi-bin\\ebooks\\ebooks.txt";
# Create output file
open( LIST_FILE, ">$list_file" ) || die "Can't open '$list_file'";

# Update file with MP3 names
foreach my $filename (@dir_contents) {
  if (lc($filename) =~ /\.epub$/) {
    # Remove .epub extension from name
    $filename =~ s/\.epub//;
    $filename =~ s/\.EPUB//;
    print LIST_FILE $filename."\n";
  }
}
close LIST_FILE;

print "\n\n ebooks.txt updated!\n\n";

exit;