#!/bin/bash

# googleflood.sh - last revised as $version below
# Script to flood google with random searches, using my regular browser
# cookies file, in order to mask their record of my real searches with
# thousands of false search records
# Requires: wget, mktemp, wc, ping, awk, grep, cut, /proc filesystem

version=2009.04.11

# user settings - first at least will need changing:
COOKIESFILE=/home/ajpg/.mozilla/default/gvp4jt3u.slt/cookies.txt
WORDSFILE=/usr/dict/words
GOOGLESITE=www.google.co.uk
NUMBERWORDS=3	# number of words to include in each search phrase
MAXLOAD=0.30	# only search when last minute's CPU load under this value
VERBOSITY=2	# verbosity level, viz:
# 0 - silent
# 1 - output stream of marks to indicate progress (+ = search, . = pause for
# CPU load to decrease, - = longer pause when network/google down)
# 2 - verbose version of above (includes words being googled)
# 3 - make wget noisier (reports command and errors, eg if google is blocking
# your searches!)
# 4 - make wget noisier still

if test $VERBOSITY -gt 0; then
  echo "$0 version $version, searching $GOOGLESITE"
  echo "giving google cookies from $COOKIESFILE"
  echo "for $NUMBERWORDS word phrases taken from $WORDSFILE"
  echo 
fi

# set/adjust internal variables:
test $VERBOSITY -lt 3 && wgetnoise='--quiet'
test $VERBOSITY -eq 3 && wgetnoise='--no-verbose'
test $VERBOSITY -gt 3 && wgetnoise='--verbose'
maxload=`echo $MAXLOAD | cut --delimiter='.' \
--output-delimiter='' --fields='1-2'`
wordsfile=`mktemp -t googleflood.XXXXXXXXXX`
grep -v '[^a-zA-Z]' $WORDSFILE > $wordsfile
wordcount=`cat $wordsfile | wc --lines`

while true; do
  if ping -c 1 $GOOGLESITE > /dev/null 2>&1; then
    cpuload=`cut --delimiter=' ' --fields=1 /proc/loadavg | cut \
    --delimiter='.' --output-delimiter='' --fields='1-2'`
    if test $cpuload -lt $maxload; then
      searchterm=''
      for (( n=1; $n <= $NUMBERWORDS; n++ )); do
	sleep 1 # to ensure new date/time seed for each srand() in awk
	searchword=`awk -v WDS="$wordcount" \
	'BEGIN {srand(); TRM = int(rand() * WDS) + 1}; NR == TRM' $wordsfile`
	searchterm="$searchword $searchterm"
      done
      # note google blocks wget unless you reset the user agent string
      wget --user-agent='' --output-document=/dev/null \
      --load-cookies=$COOKIESFILE $wgetnoise \
      "http://$GOOGLESITE/search?q=$searchterm"
      test $VERBOSITY -eq 1 && echo -n '+'
      test $VERBOSITY -gt 1 && echo "googled for '$searchterm' ... "
      sleep 2 #let CPU recover after all that awking...
    else
      test $VERBOSITY -eq 1 && echo -n '.'
      test $VERBOSITY -gt 1 && echo "CPU load too high, waiting for it to fall ..."
      sleep 15
    fi
  else
    test $VERBOSITY -eq 1 && echo -n '-'
    test $VERBOSITY -gt 1 && echo "Cannot reach $GOOGLESITE, sleeping a while ..."
    sleep 120
  fi
done

