#!/bin/ksh
# convert_audiovideo_please.ksh
# Listen for email Downloads of audio or video and convert with ffmpeg as required

echo "" > ${HOME}/Downloads/convert_audiovideo_please.out
nthan="${HOME}/Downloads/convert_audiovideo_please.out"
cmdsuff=""
cmdmid=""
while [ 0 -lt 1 ]; do
  find ${HOME}/Downloads -name 'convert_audiovideo_please_*.*' -mtime -1m -newer ${nthan} -type f >> ${HOME}/Downloads/convert_audiovideo_please.lst #2> /dev/null
  if [ ! -f "${HOME}/Downloads/convert_audiovideo_please.lst" -o -z "${HOME}/Downloads/convert_audiovideo_please.lst" ]; then
   nthan="${HOME}/Downloads/convert_audiovideo_please.out"
  else 
   echo "" > ${HOME}/Downloads/convert_audiovideo_please.out
   echo "" > ${HOME}/Downloads/convert_audiovideo_please.alt
   nthan="${HOME}/Downloads/convert_audiovideo_please.alt"
   file="${HOME}/Downloads/convert_audiovideo_please.lst"
   # Thanks to https://www.cyberciti.biz/faq/ksh-read-file/
   while IFS= read -r line
   do
        # display line or do somthing on $line
    if [ `echo "${line}" | tr -d ' '` != `echo "${line}"` ]; then
      one=`echo "${line}"`
      two=`echo "${line}" | sed '/ /s///g'`
      mv "${one}" "${two}"
      line="${two}"
    fi
	lineone=`echo "${line}" | sed '/.pdf/s///g'`
	linetwo=`echo "${line}" | sed '/.jpeg/s///g' | sed '/.jpg/s///g' | sed '/.png/s///g' | sed '/.bmp/s///g' | sed '/.gif/s///g' | sed '/.tif/s///g' | sed '/.pdf/s///g'`
    linethree=`echo "${line}" | sed '/.movie/s///g' | sed '/.avi/s///g' | sed '/.mov/s///g' | sed '/.mp3/s///g' | sed '/.m4v/s///g' | sed '/.mpeg/s///g' | sed '/.lsx/s///g' | sed '/.ogv/s///g' | sed '/.webm/s///g'`
	toext=`echo "${line}" | cut -f 4 -d '_' | cut -f 1 -d '.'`
	if [ ! -z "${toext}" ]; then
	nonext=`echo "${line}" | cut -f 1 -d '.' | tr -d '_'`
	ext=`echo "${line}" | cut -f 2 -d '.' | cut -f 1 -d '-' | cut -f 1 -d '_' | cut -f 1 -d ' ' | cut -f 1 -d '(' | cut -f 1 -d '0' | cut -f 1 -d '1' | cut -f 1 -d '2' | cut -f 1 -d '3' | cut -f 1 -d '4' | cut -f 1 -d '5' | cut -f 1 -d '6' | cut -f 1 -d '7' | cut -f 1 -d '8' | cut -f 1 -d '9'`
	if [ "`echo ${toext} | wc -c`" == "3" -o  "`echo ${toext} | wc -c`" == "2" -o  "`echo ${toext} | wc -c`" == "1" ]; then 
	toext="${toext}"
	elif [ "${line}" != "${linethree}" ]; then
	ffmpeg -i "${line}" ${cmdmid} ${nonext}.${toext} ${cmdsuff} > ffmpeg.out 2> ffmpeg.err
	if [ -f "${nonext}.${toext}" ]; then
	  if [ "${toext}" == "gif" ]; then
	  open -a Safari ${nonext}.${toext}
	  else
	  open ${nonext}.${toext}
	  fi
	fi
	elif [ "${line}" != "${lineone}" ]; then
	convert -delay 200 "${line}" ${cmdmid} ${nonext}.${toext} ${cmdsuff} > ffmpeg.out 2> ffmpeg.err
	if [ -f "${nonext}.${toext}" ]; then
	  open ${nonext}.${toext}
	fi
	elif [ "${toext}" == "jpeg" -o "${toext}" == "jpg" -o "${toext}" == "gif" -o "${toext}" == "bmp" -o "${toext}" == "tif" -o "${toext}" == "png" -o "${toext}" == "pdf" ]; then
	convert "${line}" ${cmdmid} ${nonext}.${toext} ${cmdsuff} > ffmpeg.out 2> ffmpeg.err
	if [ -f "${nonext}.${toext}" ]; then
	  open ${nonext}.${toext}
	fi
	elif [ "${line}" != "${linetwo}" ]; then
	convert "${line}" ${cmdmid} ${nonext}.${toext} ${cmdsuff} > ffmpeg.out 2> ffmpeg.err
	if [ -f "${nonext}.${toext}" ]; then
	  open ${nonext}.${toext}
	fi
	else
	ffmpeg -i "${line}" ${cmdmid} ${nonext}.${toext} ${cmdsuff} > ffmpeg.out 2> ffmpeg.err
	if [ -f "${nonext}.${toext}" ]; then
	  open ${nonext}.${toext}
	fi
	fi
	fi
   done < $file
   echo "" > ~/Downloads/convert_audiovideo_please.lst
   find ${HOME}/Downloads -name 'convert_audiovideo_please_*.*' -mtime -1m -newer ${nthan} -type f > ${HOME}/Downloads/convert_audiovideo_please.lst #2> /dev/null
   echo "" > ${HOME}/Downloads/convert_audiovideo_please.alt
  fi
  sleep 10
done

exit
