
# Copyright © 1997 UC Regents. All rights reserved.
# Copyleft © 1999,2000 Michiel Dethmers. All rights reversed.
# Document maintained at http://www.quipu.co.uk/SWISH-E/AutoSwish/.
# Version 1.0 21/01/2000. 
#
# This program and library is free software; you can redistribute it and/or
# modify it under the terms of the GNU (Library) General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#* GNU (Library) General Public License for more details.
#
# You should have received a copy of the GNU (Library) General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#---------------------------------------------------------
#
# Purpose: These are some generic library functions used

@Months = qw/January February March April May June July August September October November December/;
@DoW = qw/Sun Mon Tue Wed Thu Fri Sat/;

# Subroutine to list available configuration files

sub ListConfigFiles {
  local($cmd) = @_;
  my $html = "<ul>";
  my @configfiles = &get_config_files("$CONFIGDIR");
	foreach (@configfiles) {
	  $html .= "<LI><A HREF=\"$AUTOSWISH?todo=$cmd&ConfigFile=$_->{filename}\">$_->{name}: $_->{description}</A>\n";
	}
	$html .=  "</UL>\n";
	$html .=  "<b>Error: No Config files available</b>" if !@configfiles;
	$html .= "\n";
}

# Subroutine for printing a generic HTML header.

sub html_header {

$document_title = $_[0];

return <<HTML_Header;
Content-type: text/html
pragma: no-cache

<HTML>
<HEAD>
<TITLE>$document_title</TITLE>
</HEAD>
$BODYTAG
<P ALIGN=CENTER>
<a href="/SWISH-E/autoswish/"><IMG 
ALT="AutoSwish" BORDER=0 WIDTH=298 HEIGHT=50 
SRC="/SWISH-E/images/autoswish.gif"></A> </P>

<H2 ALIGN=CENTER>AutoSwish: Automatic Index Creation</H2>

HTML_Header
}

sub webify {
	# translate some special characters in their html equivalent
	$val = $_[0];
	$val =~ s/"/&quot;/g;
	$val =~ s/>/&gt;/g;
	$val =~ s/</&lt;/g;
	$val;
}

# Subroutine for printing a generic HTML footer.

sub html_footer {

return <<HTML_Footer;
<hr>
<center><a href="http://sunsite.berkeley.edu/SWISH-E/"><P ALIGN=CENTER><IMG SRC="$BANNER" border=0></a></p>
<a href="/SWISH-E/autoswish/">Main index</a> | <a href="/SWISH-E/autoswish/complete.shtml">Create</a> | 
<a href="$AUTOSWISH?todo=listreindex&ConfigFile=.conf">Reindex</a> | 
<a href="$AUTOSWISH?todo=listremove&ConfigFile=.conf">Remove</a> |
<a href="$AUTOSWISH?todo=listedit&ConfigFile=.conf">Edit</a> |


</center>
<HR>
<P ALIGN=CENTER>
Problems, Questions, Comments?<BR>
Contact your <A HREF="mailto: $WEBMAN">Webmaster</A>.
</BODY>
</HTML>
HTML_Footer

}

# Subroutine to return html - clean errors

sub return_error {

local ($status, $keyword, $message) = @_;

print &html_header("CGI Program - Unexpected Error");

print <<End_of_Error;

<center><H2>$keyword</H2>
<HR>
$message<BR>
Please contact the <A HREF="mailto:$WEBMAN">Webmaster</A> for more information.

End_of_Error
print &html_footer;
exit(1);
}

sub web_die {
	&return_error(500,"Config Error", @_);
}

sub finish {
  print "Error: $_[0]\n";
  exit;
}

# Subroutine to retrieve available configuration files

sub get_config_files {
  local($dir) = @_[0];
  opendir DIR, $dir;
  my $file, @res;
  while ($file = readdir(DIR)) {
    next if $file !~ /\.conf$/;
    my $configfile = {};
    $configfile->{filename} = $file;
    $configfile->{name} = &getconfigitem("IndexName","$dir/$file");
    $configfile->{description} = &getconfigitem("IndexDescription","$dir/$file"),
    push @res,$configfile;
  }
  @res;
}

# Subroutine to read some variable from a configuration file

sub getconfigitem {
  local($toget,$file) = @_;
  open F, $file;
  while (<F>) {
    if (/$toget/i) {
      s/$toget\s+//i;
      # remove comments
      s/#.*//;
      close(F);
      chomp;
      return $_;
    }
  }
  close(F);
  return "0";
}

1;
