#!/bin/bash

cat << EOF

hostlist.sh
Script per recuperare una hostlist di client/server gnutella
Copyright (C) 2002  Gianluca 'gurutech' Mascolo
<mailto:gurutech@gurutech.it>

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) 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 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.


EOF


#Azzeramento di eventuali file temporanei rimasti
rm -f report.html
rm -f hostlist
rm -f lista
rm -f urlfile

#URL e' l'URL da dove recuperare una serie di siti che fanno webcache
URL="http://zero-g.net/gwebcache/report.html"
#QUANTO e' un numero indicativo del numero di URL di webcache da
#recuperare e indica il numero di righe totali su cui effettuare
#la ricerca nella pagina HTML
QUANTO=200
#MARKER server ad identificare dove comincia la lista degli URL di webcache
#nella pagina. E' sufficiente aprire la pagina a mano una volta e
#individuare una riga precedente agli URL che sia identificabile
# IN MODO UNIVOCO
MARKER="host url"

#QUIET puo' essere uguale a -q per non avere nessun output, oppure puo' essere
#nullo per avere delle informazioni aggiuntive sullo stato dei download
QUIET="-q"

echo "Recupero i server di WebCache"
wget -t 1 $QUIET -O report.html $URL
#Trovo la riga a cui comincia l'elenco degli host
INIZIO=`grep -i -n "$MARKER" report.html | tail -n1 | cut -d \: -f 1`
TOT=`cat report.html | wc -l`
#Estraggo gli URL con pagine in PHP
tail -n$((TOT-INIZIO)) report.html | head -n $QUANTO | grep -i "a href" | grep -i -v \.php\? | grep -i -v details | grep \.php | tr [:upper:] [:lower:] | sed -e s/.*href=\"// | sed -e s/\".*// > urlfile

#Recupero gli host in cache sugli URL di cui sopra
TOT=`cat urlfile | wc -l`
x=0
while ( echo "$((x<TOT))" | grep 1 &> /dev/null); do
        {
        BASE="`head -n$((x+1)) urlfile | tail -n1`"
	echo "Recupero lista host $((x+1)) / $TOT"
	wget -t 1 $QUIET -O lista $BASE?hostfile=1
	cat lista >> hostlist
	rm -f lista
	x=$((x+1))
        }
done

rm -f report.html
rm -f lista
rm -f urlfile
