#!/usr/local/bin/perl
#
# list_icons
#
#   This perl script was especially written to provide a means of
# generating a special index listing of icon sections, on my WWW
# server of "Anthony's Icon Library". In particualar provide a
# means where a gzip'ed tar file of a particular icon section can 
# be downloaded to the calling WWW client.
#
#   This script also was used to display all the icons in a section
# as a large document with inline images (instead of file type images).
# This however proved to be very slow, causes heavy load on the WWW
# server and heavy network traffic. It has thus been discontinued.
#
#   It is must better for clients to download a tar file then look at
# the de-archived contents with an appropiate icon viewer program 
# such as ``xbmbrowser''.
#
# CGI GET OPTIONS
#   dir=         # STRING  icon sub-directory to perfrom actions in
#   search=      # STRING  a filename sub-string to search for
#   image=       # BOOL    show all images (Obsolete due to load problems)
#   tar=         # BOOL    Download a tar file of the icon sub-directory
#
# AUTHOR
#         Anthony Thyssen           25 May 1994
#
# --- initialize ---
$LIST_URL  = "/antbin/list_icons";     # This scripts URL
$ICON_DIR  = "/home/anthony/icons/";   # Library Real Path
$ICON_URL  = "/~anthony/icons/";       # Library URL Path
$INDEX     = "index.html";             # name of a default index file
$FILE_SKIP = '^Images.*\.html$';       # RE of files which are NOT to be listed

$XBMBROWSER= "ftp://ftp.x.org/contrib/utilities/xbmbrowser4.2.tgz";

# Use the icons in the library directly -- solves problems
$LINK_UP   = "$ICON_URL/appl/www/back.xbm";    # up symbol
$LINK_NEXT = "$ICON_URL/appl/www/next.xbm";    # next symbol
$LINK_PREV = "$ICON_URL/appl/www/prev.xbm";    # prev symbol
$FILE_SYMS = "$ICON_URL/appl/desktop/file/bw/";   # file symbols
$LINK_WIDTH  = 16;
$LINK_HEIGHT = 16;
$FILE_WIDTH  = 20;
$FILE_HEIGHT = 20;


sub address {
print <<'EOF';

</BLOCKQUOTE>
<P><HR>
<ADDRESS>
Program: ``list_icons''<BR>
Created: 25 May 94 <BR>
Written:
  <A HREF="http://www.sct.gu.edu.au/~anthony/anthony.html">
  Anthony Thyssen</A>
  &lt;<A HREF="http://www.sct.gu.edu.au/~anthony/mail.shtml">
  anthony@cit.gu.edu.au&gt;
</ADDRESS>
EOF
exit 0;
}

# --- read the arguments ---
#print "Content-type: text/plain\n\n";

sub clean_arg {
  local($arg) = @_;
  $arg =~ tr/+/ /;      # CGI string decoding
  $arg =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  $arg =~ s/\r\n/\n/g;  # clean up end of lines
  $arg =~ s/\r/\n/g;
  # do not do any other special processing for this program
  return( $arg );
}

# Get the post variables
$buffer = "";
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
foreach $arg ( split(/&/, $buffer) ) {
  local($name, $value) = split(/=/, $arg.'=');
  $FORM{&clean_arg($name)} = &clean_arg($value);
  print "FORM Arg ``$name'' = ``$value''<BR>\n"  if $DEBUG;
}
foreach $arg ( split(/&/, $ENV{'QUERY_STRING'}) ) {
  local($name, $value) = split(/=/, $arg.'=');
  $FORM{&clean_arg($name)} = &clean_arg($value);
  print "FORM Arg ``$name'' = ``$value''<BR>\n"  if $DEBUG;
}

# --- check directory argument ---

$dir = $FORM{"dir"};
$dir =~ s#/?$#/#;     # insure / at end
1 while $dir =~ s#^\./|/.$##;   # null directories at ends
1 while $dir =~ s#/\./|//#/#;   # null directories in middle
$dir =~ s#^/##;       # and ignore / at front
$search = $FORM{"search"};

chdir($ICON_DIR);  # We are now in the main icon directory

undef $error;
$error = "You must have at least a `dir' or `search' argument!"
   if !defined $FORM{"dir"} && !defined $FORM{"search"};
$error = "You are not allowed to use \"..\" in `dir' argument!"
   if $dir =~ /\.\./;
$error = "Illegal character in `dir' argument!"
   if $dir =~ /[][|^;&<>*?$\\`'"\n]/;
$error = "Unknown Directory : $dir!"
   if ! -d "$ICON_DIR$dir";
$error = "Empty `dir' argument without `tar' or `search' arguments!"
   if $dir eq "" && !( defined $FORM{"tar"} || defined $FORM{"search"} );

if( defined $error ) {
  print "Content-type: text/html\n\n";
  print "Sorry I am not allowed to use these arguments\n",
	"in this combination : <PRE>\n";
  foreach $arg ( keys %FORM ) {
    print '   ', $arg, ' = "', $FORM{$arg}, "\"\n";
  }
  print "</PRE>\n\n";
  print "The reason for this is : $error\n\n";
  &address;
}

if( $search =~ /[^\w.-]/ ) { # non-plain character in search string
  print "Content-type: text/html\n\n";
  print "Sorry the search string must be just a simple filename sub-string\n";
  print '   search = "', $search, "\"\n";
  &address;
}

# directories to search 
$search_dir = "appl desc prog large";

# tar whole library -- if $dir is now empty
$dir = "Announce README index.html docs appl desc prog large support"
   if $dir eq "" && defined $FORM{"tar"};

# --- icon searching form (no search string given) ---
if ( defined $FORM{"search"} && $search eq "" ) {
  # Output a search form as appropriate
  print "Content-type: text/html\n\n",
        "<HTML><HEAD>\n",
        "<TITLE>Anthony's Icon Library - Search Form</TITLE>\n",
        "</HEAD><BODY>\n",
        "<H1>Anthony's Icon Library - Search Form</H1>\n",
        "<FORM METHOD=GET ACTION=\"$LIST_URL\">\n",
        "Filename sub-string to search for :-\n",
        "<INPUT NAME=\"search\"><BR>\n",
        "Download a gzip'ed tar file of files found?\n",
        "<INPUT TYPE=\"checkbox\" NAME=\"tar\"",
        defined $FORM{"tar"} ? "CHECKED" : "", "><BR>\n",
        "<INPUT TYPE=\"submit\"> <INPUT TYPE=\"reset\">",
        "</FORM><P>\n";
  print "WARNING: this may take a minite or so to search. <P>\n\n";
  print "EXAMPLE: try looking for \"dragon\" or \"shuttle\". <P>\n\n";
  print "NOTE: if you don't find what you are looking for first time\n",
        "try a releated subject.  EG: \"letter\", \"mail\", \"post\"\n",
        "would find related but different sets of icons in the library. <P>";
  &address;
}
# --- do tar download if selected ---
if ( defined $FORM{"tar"} ) {
  # Download Gzip'ed Tar file of appropriate directory
  $| = 1;  # flush the output
  print "Content-type: application/x-tar\n",
        "Content-encoding: x-gzip\n\n";
  $dir =~ s|/$||;
  if( defined $FORM{"search"} ) {
    system("tar cf - `find $search_dir -name '*$search*' -print` | ".
           "/usr/local/bin/gzip");
  } else {
    system("tar cf - $dir | ".
           "/usr/local/bin/gzip");
  }
  exit 0;
}

# --- if index file present use it ---
if ( -r "$ICON_DIR$dir$INDEX" && !defined($FORM{"search"}) ) {
  # Redirect to the index file
  print "Location: $ICON_URL$dir\nContent-type: text/html\n\n";
  print "The index for this directory is <A HREF=\"$ICON_URL$dir\">here</A>.\n";
  &address;
}

# --- Output the Appropriate Icon List ---
print "Content-type: text/html\n\n";

if ( $FORM{"search"} ) {
  # search icons list
  print "<TITLE>Icon search for ``$search''</TITLE>\n",
	"<H1>Icon search for ``$search''</H1>\n\n",
	"You can also download a ",
	"<A HREF=\"/antbin/list_icons?search=$search&tar\"><B>\n",
	"gzip'ed tar file</B></A> of these icons as it currently stands\n",
	"<I>at this moment</I>. Please do this sparingly however.<P>";

  @dir = split(/\s+/, `find $search_dir -name '*$search*' -print` );
  $dir = "";

} else {
  # Index List
  print "<TITLE>Index for Icon Section ``$dir''</TITLE>\n",
	"<H1>Index for Icon Section ``$dir''</H1>\n\n",
	"You can also download a ",
	"<A HREF=\"/antbin/list_icons?dir=$dir&tar\"><B>\n",
	"gzip'ed tar file</B></A> of these icons as it currently stands\n",
	"<I>at this moment</I>. Please do this sparingly however.<P>";

  if ( ! opendir(DIR, "$dir" ) ) {
    print "<H2>Unable to read icon directory ``$dir''</H2>\n";
    &address;
  }
  @dir = readdir(DIR);
  closedir(DIR);
}

print <<"EOF";
Once downloaded you can un-tar and view the whole icon directory with an
icon browser like <A HREF=\"$XBMBROWSER\"><B>XbmBrowser</B></A> or you can
used the XV program.<P>

<A HREF=\"$ICON_URL\">
   <IMG SRC=\"$LINK_UP\" WIDTH=$LINK_WIDTH HEIGHT=$LINK_HEIGHT ALIGN=top>
   Up to the Anthony's Icon Libray Home</A>.<P>

<HR><P>\n\n<BLOCKQUOTE>
EOF

foreach $file ( sort @dir ) {

  next  if $file eq "." || $file eq "..";

  next  if $file =~ /$FILE_SKIP/o;

  if ( -d "$dir$file" ) {
    print '<IMG SRC="', $FILE_SYMS, "dir.xbm", '"',
                 " WIDTH=", $FILE_WIDTH, " HEIGHT=", $FILE_HEIGHT, ">\n",
          ' <A HREF="', $LIST_URL, '?dir=',$dir,$file,'/">',$file,"</A><BR>\n";
    next;
  }

  $sym = "text.xbm";   # the default
  $sym = "script.xbm"  if -x "$dir$file";
  $sym = "xbm.xbm"     if $file =~ /\.xbm$/;
  $sym = "xpm.xbm"     if $file =~ /\.xpm$/;
  $sym = "image.xbm"   if $file =~ /\.gif$/;

  if ( $FORM{"image"} && $sym ne "text.xbm" && $sym ne "script.xbm" ) {
    # Display Icons
    print '<IMG SRC="', $ICON_URL, $dir, $file,  "\">\n",
          ' <A HREF="', $ICON_URL, $dir, $file,'">', $file, "</A><BR>\n";
  } else {
    # Index List
    print '<IMG SRC="', $FILE_SYMS, $sym, '"',
                 " WIDTH=", $FILE_WIDTH, " HEIGHT=", $FILE_HEIGHT, ">\n",
          ' <A HREF="', $ICON_URL, $dir, $file,'">', $file, "</A><BR>\n";
  }
}
closedir(DIR);

&address;

