#!/bin/bash

#################################################################################
#        emailfilter.sh
#        
#        Copyright 2009 Dexter <dexter@fossist.net>
#        
#        This program is free software; you can redistribute it and/or modify
#        it under the terms of the GNU General Public License as published by
#        the Free Software Foundation; either version 2 of the License, or
#        (at your option) any later version.
#        
#        This program is distributed in the hope that it will be useful,
#        but WITHOUT ANY WARRANTY; without even the implied warranty of
#        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#        GNU General Public License for more details.
#################################################################################        


clear
echo -n “Enter the name of a DIRECTORY from where you want to pick up email id’s: “;
read dirname;

# check if the entered name is a directory

if [ -d $dirname ];then
	cd $dirname; #  if it exists change to the directory
else
	echo “+============================+”
	echo “| Check your directory name! |”
	echo “+============================+”
	exit 1;
fi

# Loop through all file  in the given directory
for files in *
do
	if [ ! -d $files ];then
	# process all files and store them in a temporary file in users home dir
	echo “Processing file $files”;
	cat $files | egrep -io '\<[^-.][0-9A-Za-z\.\-\_]+@[0-9A-Za-z.]+\>' >> ~/$$;
	echo “Processed”;
	fi
done

cd -  # get back to previous working dir, i am assuming it was home

# sort the emails ids in the file, remove duplicates and store in a final file.
sort ~/$$ | uniq >> emailids.$$

# remove the temporary file
rm ~/$$

# tell the user where the mail ids are stored
echo
echo “+=================================================+”
echo ” Your email ids are available in ~/emailids.$$ ”
echo “+=================================================+”

exit 0;

