#!/bin/bash

do_clean () {
 CLEAN="$1"
 echo "cleaning.."
 find $CLEAN -maxdepth 1 -type f -iname "*.php*" -exec rm "{}" \; > /dev/null 2>&1
}

do_index () {
 X=$1
 IMGS=`grep -i "general/images" logs/my-wget-log.$X | grep '\`' | grep "\\'" | cut -d '\`' -f2 | cut -d "'" -f1 | sort | uniq`
 mv gen.$X.html gen.$X.dat > /dev/null 2>&1
 for IMG in $IMGS; do
  echo "<img src='$IMG'><p>" >> gen.$X.dat
 done
 if [ -s gen.$X.dat ] ; then
  cat gen.$X.dat | sort | uniq > gen.$X.html
 else
  rm gen.$X.html > /dev/null 2>&1
  echo "No images for $X."
 fi
 rm gen.$X.dat > /dev/null 2>&1
}

do_wget () {
 DEPTH=$1
 X=$2
 URL="$3"
 REJECT="$4"
 EXCLUDE="$5"
 echo "starting $X."
 wget -o logs/my-wget-log.$X -R $REJECT -X $EXCLUDE -np -m -p -l$DEPTH $URL$X
 do_index $X
 echo "$X done."
 return
}

do_mget () {
 MIN=$1
 MAX=$2
 BITE=$3
 DEPTH=$4
 URL="$5"
 REJECT="$6"
 EXCLUDE="$7"
 CLEAN="$8"
 D=`date`
 echo "--- $D begin"
 for (( a=$MAX; a >= $MIN; a=a-$BITE )); do
  for (( x=0; x < $BITE; x++ )); do
   PAGE=$(( $a - $x ))
   if (( $PAGE >= $MIN )); then
    do_wget $DEPTH $PAGE "$URL" "$REJECT" "$EXCLUDE" &
    sleep 1
   else
    wait
    do_clean "$CLEAN"
    break 2
   fi
  done
  wait
  do_clean "$CLEAN"
  D=`date`
  echo "--- $D"
 done
# find logs/ -type f -exec rm "{}" \;
 find . -maxdepth 1 -type f -iname "gen.*.html" | cut -d "." -f3 | sort -n | xargs -n1 -i"{}" echo "<a href='gen.{}.html'>{}</a><br>" > gen.html
 D=`date`
 echo "--- $D end"
}

MIN=$1
MAX=$2
BITE=50
DEPTH=3
URL="http://www.imagereverb.com/general/showgallery.php?id="
REJECT="index.php*","news.php","faq.php""password.php","contact.php","register.php","privacy.php","termsofuse.php","dmca.php","2257.php"
EXCLUDE="/general/www.imagereverb.com/,/js/,/adult/,/mint/,/images/,/thumbs/,/general/thumbs"
CLEAN="www.imagereverb.com/general/"

renice 13 $$ > /dev/null 2>&1
do_mget $MIN $MAX $BITE $DEPTH "$URL" "$REJECT" "$EXCLUDE" "$CLEAN"
