#!/bin/bash
# look for the following string in a bin dir
# $Id: fixroundcube.sh 21 2010-11-18 09:37:16Z deploy $

# older versions dont have the program/html2text.php
# look for the date 08/08/07 in the program

echo "checking for roundcube installations (this may take a while)"
#cd /tmp
# find instances of this file (which was the broken one) but not backups (best not to touch backups)

url="http://downloads.rimuhosting.com/roundcubemail-0.2-beta-patch.tar.gz"
for i in `find / -name html2text.php | grep bin|grep -v backup` ; do 
	# search for the string which indicates its the latest version
	num=`grep "html2text.php 2070 2008-11-20 10:29:34Z alec" $i | wc -c ` 
	fixdir=`echo $i | sed s@bin/html2text.php@@`
	echo -n "Found $fixdir ... "
	indexcheck=0
	if [ -s $fixdir/index.php ];then
		indexcheck=`grep "RoundCube Webmail" "$fixdir/index.php" | wc -c`
	fi
	if [ $num -gt 0 ];then 
		echo "looks secure, ill leave it alone"
	elif  [ $indexcheck -lt 1 ] ;then 
		echo "Doesnt look like roundcube ... skipping "
		
	else 
		echo " isnt secure. "
		echo -n "This looks like a roundcube install you want to fix? (y or n) "
	     	read yn
		if [ $yn = "y" ]; then
			echo "Fixing $fixdir"
			wget -c $url
			tar zvxf roundcubemail-0.2-beta-patch.tar.gz
			cp -a roundcubemail-0.2-beta-fix/* $fixdir
			echo "done"
		elif [ $yn = "n" ];then
			echo "Okay skipping $fixdir"
		else
			echo "That wasnt a y or n, ill skip for now"
		fi

       	fi
done

