#!/bin/ksh

#-----------------------------------------------------------------------------------------
#  INIT DATE VARIABLES
#-----------------------------------------------------------------------------------------
year=`date "+%Y"`                                       ;export year
mm=`date "+%m"`                                         ;export mm
dd=`date "+%d"`                                         ;export dd
yearmm=`date "+%Y%m"`                                   ;export yearmm
archive_datetime=`date "+%Y%m%d_%H%M%S"`                ;export archive_datetime
#-----------------------------------------------------------------------------------------
#today=`date "+%Y%m%d"`                                  ;export today
#TODAY=`date "+%m/%d/%Y"`                                ;export TODAY
#ETERNITY="01/01/3000"                                   ;export ETERNITY
#ARCHIVE_DATETIME=`date "+%Y%m%d_%H:%M:%S"`              ;export ARCHIVE_DATETIME
#-----------------------------------------------------------------------------------------

##mm=1;
##year=2009;

#---------------------------------------------------------------------
# Get today's month days.  Assign to 15 if days is less than 15.
# This will be used to delete files older than x days.
#---------------------------------------------------------------------
xDaysOld=$dd;
if test $dd -lt 15; then xDaysOld=15; fi
echo "xDaysOld=$xDaysOld";

#---------------------------------------------------------------------
# Determine previous month and its corresponding year.
# Set month to 2 digits especially when month is Jan-Sep.
#---------------------------------------------------------------------
prevMonthYear=$year;
((prevMonth=mm-1));
if test $prevMonth -eq 0;  then prevMonth=12; ((prevMonthYear=$year-1)); fi
if test $prevMonth -lt 10; then prevMonth="0"$prevMonth; fi
echo "prevMonth=$prevMonth";
echo "prevMonthYear=$prevMonthYear";

logFile="/www/ftp/pub/span/data/.archive_scripts/log/archive_SPAN_files.$yearmm.log";

cd /www/ftp/pub/span/data


#dirList="";
#for dir in `find . -maxdepth 7 -type d -exec ls -d {} \; `
#do
#       echo "=====>dir=$dir"
#       # do not want any prior archive year
#       cntPriorArchYear=`echo $dir | egrep -c '(199|20)'`;
#       echo "   cntPriorArchYear=$cntPriorArchYear"
#
#       if test $cntPriorArchYear -eq 0; then
#               # --- Use the following when files from prior months were missed
#               #cnt0=0;
#               #cnt1=0;
#               ## ------ TEMP to get month 01 to 09  and month 10 to 11
#               ## --- use this to ALSO search for filename with "-mm-dd" format --- not used since these files are owned by "admin"
#               ##cnt0=`ls $dir/*0[1-9][0-3][0-9]*.* $dir/*-0[1-9]-[0-3][0-9]* 2>/dev/null | egrep -c "(0[1-9][0-3][0-9]|-0[1-9]-[0-3][0-9])" `;
#               ##cnt1=`ls $dir/*1[01][0-3][0-9]*.*  $dir/*-1[01]-[0-3][0-9]* 2>/dev/null  | egrep -c "(1[01][0-3][0-9]|-1[01]-[0-3][0-9])" `;
#               # --- use this to search only for filename with "mmdd" format
#               ##cnt0=`ls $dir/*0[1-9][0-3][0-9]*.* 2>/dev/null | grep -c "0[1-9][0-3][0-9]" `;
#               ##cnt1=`ls $dir/*1[01][0-3][0-9]*.*  2>/dev/null | grep -c "1[01][0-3][0-9]" `;
#               #
#               #((cntPriorMonth=cnt0+cnt1));
#
#               # --- use this to ALSO search for filename with "-mm-dd" format --- not used since these files are owned by "admin"
#               #cntPriorMonth=`ls $dir/*$prevMonth[0-3][0-9]*.* $dir/*-$prevMonth-[0-3][0-9]* 2>/dev/null | egrep -c "($prevMonth[0-3][0-9]|-$prevMonth-[0-3][0-9])" `;
#               # --- use this to search only for filename with "mmdd" format
#               cntPriorMonth=`ls $dir/*$prevMonth[0-3][0-9]*.* 2>/dev/null | grep -c "$prevMonth[0-3][0-9]" `;
#               echo "   cntPriorMonth=$cntPriorMonth"
#               if test $cntPriorMonth -gt 0; then
#                       dirList="$dirList $dir"
#                       echo "======> cnt=$cnt          $dir" | tee -a $logFile
#               fi;
#       fi;
#done

dirList="./cme/test/swapstream/span"

echo "--------------------------------------------------------------------------------" | tee -a $logFile
echo "$archive_datetime  The following is the dirList:" | tee -a $logFile
echo "$dirList"                 | tee -a $logFile
echo "M O V I N G   F I L E S " | tee -a $logFile

# Note:  some files in swapstream are not owned by "spanftp"
#dirList="./difx ./cbt/settle ./cdc ./cfe ./cme/test ./dgc ./kel ./mge ./nos ./nse ./nyb/test ./nzf ./sfe"
#dirList="./cme/test/fxm ./cme/test/swapstream ./cme/test/swapstream/span"


for dir in `echo $dirList `
do

        #  Count number of files with year ie. 2008
        cntFileWithYear=`ls $dir/*$prevMonthYear*.* 2>/dev/null | grep -c $prevMonthYear `
        echo "======> cntFileWithYear=$cntFileWithYear    $dir" | tee -a $logFile

        cntFileWithMonth=0
        loopCnt=1
        while test $loopCnt -le $prevMonth
        do
                mm2digits=$loopCnt;
                if test $loopCnt -lt 10; then mm2digits="0"$loopCnt; fi;

                #  Count number of files with 2 digit month and day mmdd ie. 0801
                cntFile=`ls $dir/*$mm2digits[0-3][0-9]*.* 2>/dev/null | grep -c $mm2digits[0-3][0-9] `
                ((cntFileWithMonth=cntFileWithMonth+cntFile));
                ((loopCnt=loopCnt+1));
        done

        echo "======> cntFileWithMonth=$cntFileWithMonth        $dir" | tee -a $logFile
        ((cntTot=cntFileWithYear+cntFileWithMonth));
        echo "======> cntTot=$cntTot            $dir" | tee -a $logFile

        if test $cntTot -gt 0; then
                echo "======> cntTot=$cntTot            $dir" | tee -a $logFile

                mkdir -p $dir/$prevMonthYear;

                echo "Moving \"$dir\" files that are older than $xDaysOld from today." | tee -a $logFile

                find $dir -name "*.*" -maxdepth 1 -user spanftp -daystart -mtime +$xDaysOld | xargs -i mv {} $dir/$prevMonthYear
                #------------------
                # the following is for testing via ls and not mv
                #------------------
                #find $dir -name "*.*" -maxdepth 1 -user spanftp -daystart -mtime +$xDaysOld | xargs -i ls -l {}

        fi;

done

cd /www/ftp/pub/span/data/.archive_scripts

