#!/bin/bash
## Iri 2018-11-06 Change to quiet
## Iri 2019-01-20 Add listing for restore
## Iri 2019-06-08 Don't check BBC files - no ads
## Iri 2019-06-15 Add colours and start end time
## Iri 2019-06-17 Add to to log file name - add 7 days to Qmaint
## Iri 2019-06-18 Change Log file loctopn
## Iri 2019-07-13 Don't run on HD as takes too long
#Define colours used
NC='\033[0m'     # No Color
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
MAGENTA='\033[0;35m'
CYAN='\033[0;36m'
WHITE='\033[0;37m'
DATE=$(date +"%a%H%M")
LOGFILE="/Storage/Logs/QComskip-${DATE}.log"
#date > $LOGFILE
DATE=$(date)
printf "${CYAN}$DATE${NC}\n" >$LOGFILE
cd /Storage/TVShows

##Keep a list of files in case of crash
ls -alrt > /home/pi/Qtvlist.txt
for f in *.ts; do
     # Strip out the filename and extension"
     filename=$(basename -- "$f")
     extension="${filename##*.}"
     filename="${filename%.*}"
     #If BBC then don't bother
# 0713     if [[ $f == *"BBC"* ]]; then
     if [[ "$f" = *"BBC"* ]] || [[ "$f" = *"HD."* ]] ; then
       TEXT=" BBC or HD file: Skipping"
       printf "$f${WHITE}$TEXT${NC}\n" >>$LOGFILE
     else
       # If *.txt exists, then don't do anything
       if [ ! -f "$filename".txt ]; then
         TEXT=".txt Does not exist: Processing"
         DATE=$(date +%R)    # Time only
         printf "$f${RED}$TEXT${NC}\n" >> $LOGFILE
         printf "Start time ${CYAN}${DATE}${NC}  " >> $LOGFILE
         /home/pi/bin/Comskip/comskip --quiet --ini=/home/pi/bin/Comskip/comskip.ini -t "$f"
         DATE=$(date +%R)
         printf "End time ${MAGENTA}${DATE}${NC}\n" >> $LOGFILE
       else
         TEXT=".txt Already exists: Do nothing"
         printf "$f${GREEN}$TEXT${NC}\n" >> $LOGFILE
       fi
     fi
done
DATE=$(date)
printf "${CYAN}$DATE${NC}\n" >>$LOGFILE

