#This program grabs the directory listing from the
#clients fromclient and the archive directory. Determines
#if it is a file, if so it logs in in the csv file. Then
#it emails the file to the users listed in the mailatt.php file.
#
#Requested by Armando

cd /home/list2 #For crontab reasons. It’s lost with out it.

logfile=”fromclient`date +%Y%m%d%H%M`.csv” #Initially sets the log file name
echo “Client, Root Directory, Filename, Date” >> $logfile

dir_list_array=( $(ls /home/ftpusers) ) #Loads ls results into an array
dl_count=${#dir_list_array[@]} #Determines the array length and sets a var
dl_index=0 #Inital set of the index var

while [ “$dl_index” -lt “$dl_count” ] #Loops for each dir name in the array
do
client=${dir_list_array[$dl_index]} #makes code easier to look at

if [[ $client == ftp* ]] #Determines if the dir starts with FTP. If so processes
then
frompath=”/home/ftpusers/$client/fromclient” #Once again, to make code easier to read
fromclient_array=( $(ls $frompath) ) #Loads ls results into an array
fc_count=${#fromclient_array[@]} #Determines the array length and sets a var
fc_index=0 #Inital set of the index var

while [ “$fc_index” -lt “$fc_count” ] #Loops for each dir name in the array
do
listfile=${fromclient_array[$fc_index]} #Easier to read
filepath=”$frompath/$listfile” #Sets path to actual file
if [[ -f $filepath ]] #Check to see if file or dir
then
date=`find “$filepath” -prune -printf “%Tm%Td%TY%TT”`
# date=`find “$filepath” -prune -printf “%Tc”` #Grabs file date and formats it
echo “$client , fromclient, $listfile, $date” >> $logfile #Create and append csv file
else
if [[ $listfile == archive ]]
then
archpath=”$frompath/archive”
archive_array=( $(ls $archpath) ) #Loads ls results into an array
ar_count=${#archive_array[@]} #Determines the array length and sets a var
ar_index=0 #Inital set of the index var

while [ “$ar_index” -lt “$ar_count” ] #Loops for each dir name in the array
do
ar_listfile=${archive_array[$ar_index]} #Easier to read
ar_filepath=”$archpath/$ar_listfile” #Sets path to actual file
if [[ -f $ar_filepath ]] #Check to see if file or dir
then
ar_date=`find “$ar_filepath” -prune -printf “%Tm%Td%TY%TT”`
# ar_date=`find “$ar_filepath” -prune -printf “%Tm”/”%Td”/”%TY %Tr”`
# ar_date=`find “$ar_filepath” -prune -printf “%Tc”` #Grabs file date and formats
echo “$client , archive, $ar_listfile, $ar_date” >> $logfile
fi
let “ar_index = $ar_index +1” #Increment Counter
done
fi
fi
let “fc_index = $fc_index +1” #Increment counter
done

fi
let “dl_index = $dl_index +1″ #Increment counter
done
cp $logfile /home/ftpusers/fromclient_list.csv
php mailatt.php subject=”Daily fromclient and archive data” log=$logfile #Email log file.

Advertisements