#!/bin/bash


rm -rf outp.txt
rm -rf p1.txt
rm -rf outfnames.txt
rm -rf outpp.txt

if [ $# -lt 6 ]
then 
	echo "Usage replace1 -z1 <size> -s1 <str1> -s2 <str2>"
	exit
fi

if [ $1 = -help ]
then
	 echo "Usage replace1 -z1 <size> -s1 <str1> -s2 <str2>"
         exit
fi
		 

if [ $1 = -z1 ]
then
	export size=$2
	echo $size
else if [ $3 = -z1 ]
	then
		export size=$4
	else if [ $5 = -z1 ]
	then 
		export size=$6
	fi
fi
fi

if [ $1 = -s1 ]
then
	export str1=$2
else if [ $3 = -s1 ]
	then
		export str1=$4
	else if [ $5 = -s1 ]
	then 
		export str1=$6
	fi
fi
fi

if [ $1 = -s2 ]
then
 	export str2=$2
else if [ $3 = -s2 ]
	then	
	export str2=$4
	else if [ $5 = -s2 ]
	then 
		export str2=$6
	fi
fi
fi

export cmd="find -type f -size +"$size"w -print"
for m in `$cmd`;do

#skip if file is a directory
if [ -d $m ]
then
continue
#skip if file has no read permissions
elif [ ! -r $m ]
then
continue
#skip if file has no write permissions
elif [ ! -w $m ]
then
continue
fi

#################################################################################
if [ "$m" != "./replace1" -a "$m" != "./replace2" ]
then

grep $str1 $m > outpp.txt

x=$(wc -l outpp.txt | cut -b7)
if [ $x != 0 ]
then
cat outpp.txt >> outp.txt
fi
#sed 's/~'"$str1"'/'"$str2"'/g' $m >temp
replace -s '\b'$str1'' $str2 <$m >temp
diff $m temp > tempdiff
rm -rf $m
cp temp $m
y=$(wc -w tempdiff | cut -b7)
if [ $y != 0 ]
then
 echo $m >> outfnames.txt
fi
fi 
done; 

echo "List of modified files :"
if [ -f "outfnames.txt" ]
then
cat outfnames.txt
fi
