#!/usr/bin/perl -w
########################################################################
#
# parsepage - parses gopher dir taken on STDIN
# Copyright (C) 2002, 2003, 2004 Timothy Jon Fraser tfraser@alum.wpi.edu
#
# $Id: parsepage,v 1.2 2004/06/03 15:01:46 tim Exp $
#
# This file is part of gspider.
#
# gspider is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.  
# 
# gspider 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 General Public
# License for more details.  
#
# You should have received a copy of the GNU 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.
#
#
# USAGE:  parsepage -h <host> [-p <port>] -f <pagefilename>
#
########################################################################

use strict;
use vars qw($opt_h $opt_p $opt_f);
use Getopt::Std;
use gspider;

#
# handle command line arguments
#
my $usage = "USAGE: parsepage -h <host> [-p <port>] -f <pagefile>";
$opt_h = "";      # gopher server host, no default
$opt_p = "70";    # gopher server port, defaults to 70
$opt_f = "";      # filename of file containing gopher page
getopts('h:p:f:') || die $usage;
if($opt_h eq "") {  # user must supply host on commandline
    die $usage;
}
if($opt_f eq "") {  # user must supply filename on commandline
    die $usage;
}
open(PAGEFILE, $opt_f) || die "Can't open $opt_f: $!";
my @thepage = <PAGEFILE>;
close PAGEFILE;

my @remoteservers;
my @localdirs;
my @failures;
my $hostnameport = $opt_h . ":" . $opt_p;
my $selectorcount = parsegopherpage($hostnameport, \@thepage,
				    \@remoteservers, \@localdirs,
				    \@failures);

print "Parsed $selectorcount selectors from $hostnameport.\n";

print "Remote servers:\n";
my $line;
foreach $line (@remoteservers) {
  print "\t$line";
}

print "Local directory selectors:\n";
foreach $line (@localdirs) {
  print "\t$line";
}

print "Failures:\n";
foreach $line (@failures) {
  print "$line";
}

exit 0;
