#!/bin/bash

export DATADIR='/raid/lehd/'
export LOGDIR='/root/crons/lehd/log/'
#export MAILTO='frank.a.brunotts@census.gov'
export MAILTO='ces.lehd.ftp.list@census.gov'

export find='/usr/bin/find'
export date='/bin/date'
export mail='/bin/mail'
export grep='/bin/grep'
export cat='/bin/cat'
export ls='/bin/ls'
export tr='/usr/bin/tr'
export cut='/bin/cut'

# Subroutines

clean()	{

export LOG="${LOGDIR}cleaned.log"
echo -n 'START clean	' >> ${LOG}
${date} >> ${LOG}

UNENCRYPTED=`${find} ${DATADIR} -type f -and -not -iname '.*' -and -not -iname '*.pgp' -and -not -iname '*.pgp.?'`

if [ -n "${UNENCRYPTED}" ]
then
${find} ${DATADIR} -type f -and -not -iname '.*' -and -not -iname '*.pgp' -and -not -iname '*.pgp.?' -exec echo "Removed file:   {}" \; >> ${LOG}
${find} ${DATADIR} -type f -and -not -iname '.*' -and -not -iname '*.pgp' -and -not -iname '*.pgp.?' | ${mail} -s "LEHD files DELETED on ftp.census.gov" ${MAILTO}
${find} ${DATADIR} -type f -and -not -iname '.*' -and -not -iname '*.pgp' -and -not -iname '*.pgp.?' -exec rm {} \;
fi

echo -n 'END   clean	' >> ${LOG}
${date} >> ${LOG}
}

alert()	{

export LOG="${LOGDIR}alerted.log"
echo -n 'START alert     ' >> ${LOG}
${date} >> ${LOG}

export NEWDB="${LOGDIR}alerted.db"
touch ${NEWDB}

export TEMPNEWDB="${LOGDIR}alerted.db.tmp"
touch ${TEMPNEWDB}

ENCRYPTED=`${find} ${DATADIR} -type f -and -not -iname '.*' -and \( -iname '*.asc' -or -iname '*.pgp' -or -iname '*.gpg' \) `

if [ -n "${ENCRYPTED}" ]
then
	for FILE in ${ENCRYPTED}
	do
	export LIST=`${ls} -l --time-style=long-iso ${FILE} | ${tr} -s ' ' '|' | ${cut} -d'|'  -f6-8 | ${tr} '|' '_'`
	export ALERTED=`${grep} ${LIST} ${NEWDB}`
	if [ -z "${ALERTED}" ]
	then
		echo ${LIST} >> ${TEMPNEWDB} 
		echo "New   file:     ${FILE}" >> ${LOG}
	fi
	done
fi

NEWFILES=`${cat} ${TEMPNEWDB}`
if [ -n "${NEWFILES}" ]
then
	${cat} ${TEMPNEWDB} | ${mail} -s "LEHD files RECEIVED on ftp.census.gov" ${MAILTO}
	${cat} ${TEMPNEWDB} >> ${NEWDB}
fi

rm ${TEMPNEWDB}

echo -n 'END   alert	' >> ${LOG}
${date} >> ${LOG}

}

remind()	{

export LOG="${LOGDIR}reminded.log"
echo -n 'START remind    ' >> ${LOG}
${date} >> ${LOG}

export OLDDB="${LOGDIR}reminded.db"
touch ${OLDDB}

export TEMPOLDDB="${LOGDIR}reminded.db.tmp"
touch ${TEMPOLDDB}

OLDENCRYPTED=`${find} ${DATADIR} -type f -and -not -iname '.*' -and -mtime +2 -and \( -iname '*.asc' -or -iname '*.pgp' -or -iname '*.gpg' \) `

if [ -n "${OLDENCRYPTED}" ]
then
	for FILE in ${OLDENCRYPTED}
	do
	export LIST=`${ls} -l --time-style=long-iso ${FILE} | ${tr} -s ' ' '|' | ${cut} -d'|'  -f6-8 | ${tr} '|' '_'`
	REMINDED=`${grep} ${LIST} ${OLDDB}`
	if [ -z "${REMINDED}" ]
	then
		echo ${LIST} >> ${TEMPOLDDB}
		echo "Old   file:     ${FILE}" >> ${LOG}
	fi
	done
fi

OLDFILES=`${cat} ${TEMPOLDDB}`
if [ -n "${OLDFILES}" ]
then
	${cat} ${TEMPOLDDB} | ${mail} -s "LEHD files 2 DAYS OLD on ftp.census.gov" ${MAILTO}
        ${cat} ${TEMPOLDDB} >> ${OLDDB}
fi

rm ${TEMPOLDDB}

echo -n 'END   remind    ' >> ${LOG}
${date} >> ${LOG}

}

# Main

clean
alert
remind
