#!/bin/bash
#date --date='1 day ago'
txtred=$(tput setaf 1)    # Red
txtgrn=$(tput setaf 2)    # Green
txtrst=$(tput sgr0)       # Text reset

DIRYR=`date --date='today' +%Y`
DIRMONTH=`date --date='today' +%b`
DIRDAY=`date --date='today' +%d`

FILEYR=`date --date='today' +%Y`
FILEMONTH=`date --date='today' +%m`
FILEDAY=`date --date='today' +%d`

#Create date folders
mkdir /mnt/$DIRYR
mkdir /mnt/$DIRYR/$DIRMONTH
mkdir /mnt/$DIRYR/$DIRMONTH/$DIRDAY

#Move each file with file name in $MONTH and $DAY
for file in $(ls -l /SSD/callrecs/ | grep "$FILEYR$FILEMONTH$FILEDAY" | awk '{print $9}')
do

VAL=$(/usr/sbin/lsof /SSD/callrecs/$file)
if [ -z $VAL ] ; then
   echo "$file [${txtgrn}NOT open${txtrst}]. Moving."
   mv /SSD/callrecs/$file /mnt/$DIRYR/$DIRMONTH/$DIRDAY/$file
else
   echo "$file [${txtred}Open${txtrst}]. Not moving."
fi

done

chmod 774 -R /mnt/$DIRYR/$DIRMONTH/$DIRDAY
