#!/bin/bash
FINALDIR=/mnt/omg/tv/episodes
### for debugging - uncommont to enable
#echo "${1}" "${2}" "${3}" "${4}" "${5}" "${6}" "${7}" > /tmp/tvep.log
if test -d "${1}"
then
echo "Processing ${3}"
cd "${1}"
### get the filename and file extension of the release
FILE=`find . -size +103400k \( -name "*.[M|m][K|k][V|v]" -o -name "*.[A|a][V|v][I|i]" -o -name "*.[T|t][S|s]" -o -name "*.[M|m][P|p][4]" \) -print | grep -v [Ss][Aa][Mm][Pp][Ll][Ee] | head -n1`
FILEEXT=${FILE##*.}
# for debugging of filename and extension - uncomment to enable
#echo "${FILE}"
#echo "${FILEEXT}"
### update file creation time to current system time ###
touch "${FILE}"
### cleanup the filename to replace underscores and .-. combos with just a -
CLEANNAME=`echo ${3} | sed -e 's/_/\./g' -e 's/\.-\./-/g'`
echo ${CLEANNAME} > /tmp/tvep.log
### file is moved into place but remains hidden until I/O is completed
{ mv "${FILE}" "../${CLEANNAME}.${FILEEXT}" && mv "../${CLEANNAME}.${FILEEXT}" "${FINALDIR}/.${CLEANNAME}.${FILEEXT}" && mv "${FINALDIR}/.${CLEANNAME}.${FILEEXT}" "${FINALDIR}/${CLEANNAME}.${FILEEXT}" && cd .. && rm -R -f "${1}" ; } &
echo "Moving to final resting directory..."
echo "Executing cleanup script..."
### import file limit function
. /path/to/scripts/tv.filelimit.func
### run it
fileCount
echo "Done!"
exit 0
else
echo "${1}" doesnt exist yet!
### folder or release does not exist yet exit 0
exit 0
fi
### import file limit function
#. /path/to/scripts/limit.func
### run it
#fileCount