#!/usr/bin/perl
#
# check.pl Verify URL list specified on the command line or read from stdin
#
# (C) Copyright 2000-2002 Diomidis Spinellis
# 
# Permission to use, copy, and distribute this software and its
# documentation for any purpose and without fee is hereby granted,
# provided that the above copyright notice appear in all copies and that
# both that copyright notice and this permission notice appear in
# supporting documentation.
# 
# THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
# MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#
# $Id: check.pl 1.1 2002/08/31 17:06:22 dds Exp $
#

open(LOG, ">>log") || die;
select(LOG);
$| = 1;
select(STDOUT);
while (<>) {
	chop;
	if (/^(http|ftp):/) {
		$url = $_;

		# Fix null references
		$tmpurl = $url;
		$tmpurl =~ s,://,,;
		$url .= "/" unless ($tmpurl =~ m,/,);	
		$url =~ s/\xad/-/g;		# Dash

		print LOG "REF $ref\nURL $url\n";
		$url =~ s/'/'\\''/g;		# Quotes for the shell
		if ($url =~ m/^http:/) {
			open(TMP, '>W.log'); close(TMP);
			open(ERR, "webcopy -o '" . $url . "' >/dev/null 2>&1 |");
			while (<ERR>) {
				print LOG;
			}
			close(ERR);
			open(IN, 'W.log') || die;
			while (<IN>) {
				print LOG;
			}
			close(IN);
		} elsif ($url =~ m,^ftp://([^/]+)/(.*),) {
			open(ERR, $cmd = "ftpcopy $1 $2 /dev/null 2>&1 |");
			print LOG "$cmd\n";
			while (<ERR>) {
				print LOG;
			}
			close(ERR);
		}
	} else {
		$ref = $_;
	}
}
