#!/bin/bash

#-------------------------------
#   GOOWORDS 	(c) narfu 2008  |
#   Version:	     0.0.1beta  |
#   contact:	  narfu@gmx.de  |
# ------------------------------

# first install the script with 'sudo ln -s /pfad/to/script /usr/bin/goowords' otherwise it will not work! (because of screen)
# after installation, you can start it independent from current workingdirectory by typing 'goowords <parameter>' 

if [ ! -n "$1" ]
then
	echo "err: 	missing parameter"
	echo "usage:	goowords <any word>"
	exit
fi

if [ "$1" = "help" -o "$1" = "--help" -o "$1" = "-h" ]
then
	echo "usage:	goowords <any word>"
	exit
fi

if [ "$2" = "screen_start" ]
then
	word=$1
	url0="http://www.google.de/search?hl=de&q=test&btnG=Suche&meta="
	url1=`echo $url0 | sed -e "s/test/${word}/g"`

	count_lines=1
	while [ ! -f stop ]
	do
		wget --save-cookies google.de -U 'mowl' -O trash.html "http://www.google.de"
		rm trash.html
		wget --load-cookies google.de --referer "http://www.google.de" -U 'mowl' -O content.html $url1
		rm google.de

		cat content.html |						\
		sed -e :a -e 's/<[^>]*>//g;/</N;//ba' |				\
		sed -e 's/ /\n/g' | tail --lines=+137 |				\
		sed -e 's/[ -,:;()+&]/ /g' -e 's/&nbsp//g'| cut -d. -f2 |	\
		sed -e 's/ /\n/g' |						\
		tr [:upper:] [:lower:] | sort | uniq |tail --lines=+17 		\
		>> wordlist

		word=$(cat wordlist | tail -n $count_lines | head -n 1)
		url1=`echo $url0 | sed -e "s/test/${word}/g"`

		echo $url1 
		sleep 3

		count_lines=`expr $count_lines + 1`
	done
	exit
else
		screen -dmS getwords goowords $1 "screen_start"
		echo 	"	 -------------------------------"
		echo 	"	|   GOOWORDS 	(c) narfu 2008  |"
		echo 	"	|   Version:	     0.0.1beta  |"
		echo	"   	|   contact:	  narfu@gmx.de  |"
		echo 	"	 -------------------------------"
		echo 	""
		echo 	"[+]	the script is working for now ..."	
		echo 	"[i]	type 'screen -rx getwords' in a new terminal, will show you the activity"

	while [ ! -f stop ]
	do
		echo 	"[!]	to stop the script, type 'stop' in this prompt"
		echo 	"[!]	to check the size of the wordfile, type 'size' in this prompt"
		read -p "[>]	: " user
		
		if [ "$user" = "size" -o "$user" = "Size" -o "$user" = "SIZE" ]
		then
			ls -hl | grep wordlist | cut -d' ' -f5
		fi
		if [ "$user" = "stop" -o "$user" = "Stop" -o "$user" = "STOP" ]
		then
			touch stop
			echo "[!]	if the wordfile is big, to exit may take several seconds/minutes"
			while [ ! "$(screen -ls | grep getwords)" = "" ]
			do
				echo " ..."
				sleep 2
			done
		fi
	done
		
	count_words=0
	while read line
	do
	count_words=`expr $count_words + 1`
	done < 'wordlist'
	echo ""
	echo "[+]	$count_words word's written!"
	rm stop
	rm content.html
	echo "[+]	done."
	exit
fi

