#!/bin/ksh
# download_copier.ksh
echo 'Listener for downloads where the name downloaded matches criteria for a PATH defined that facilitates a followup copy'
echo ''
dpath=""
defp="`pwd`"
lendefp="`echo ${defp} | wc -c`"
xdefp=""
if [ ! -z "${1}" ]; then
 if [ -d "${defp}" ]; then
  defp="${1}"
  lendefp="`echo ${defp} | wc -c`"
if [ "`echo $defp | sed '/\\\\/s///g'`" != "${defp}" ]; then
 defp="${defp}\\"
else 
 defp="${defp}/"
fi
 dpath="${defp}"
 xdefp="${defp}"
 fi
fi
if [ -z "${xdefp}" ]; then
if [ "`echo $defp | sed '/\\\\/s///g'`" != "${defp}" ]; then
 defp="${defp}\\"
else 
 defp="${defp}/"
fi
echo "Path for matching downloads to copy to [${defp}] "
read dpath
if [ -z `echo "${dpath}" | tr -d '\r' | tr -d '\n` ]; then
 dpath="${defp}"
 lendefp="`echo ${defp} | wc -c`"
else
lendefp="`echo ${dpath} | wc -c`"
if [ "`echo $dpath | sed '/\\\\/s///g'`" != "${dpath}" ]; then
 dpath="${dpath}\\"
else 
 dpath="${dpath}/"
fi
fi 
fi
if [ -d "$dpath" ]; then
pdp="`echo $dpath | sed '/\\\\/s//?/g' | sed '/\//s//?/g' | sed '/\:/s//?/g'`"
echo "" > ${HOME}/Downloads/download_to_place.out
nthan="${HOME}/Downloads/download_to_place.out"
cmdsuff=""
cmdmid=""
while [ 0 -lt 1 ]; do
  find ${HOME}/Downloads -name "${pdp}*.*" -mtime -1m -newer ${nthan} -type f >> ${HOME}/Downloads/download_to_place.lst #2> /dev/null
  if [ ! -f "${HOME}/Downloads/download_to_place.lst" -o -z "${HOME}/Downloads/download_to_place.lst" ]; then
   nthan="${HOME}/Downloads/download_to_place.out"
  else 
   echo "" > ${HOME}/Downloads/download_to_place.out
   echo "" > ${HOME}/Downloads/download_to_place.alt
   nthan="${HOME}/Downloads/download_to_place.alt"
   file="${HOME}/Downloads/download_to_place.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
	toext=""
	nonext=`echo "${line}"`
    ifl=${nonext}
    bname=${ifl##*/}
    brest="`echo ${bname} | cut -b ${lendefp}-`"
    suf=""
    isuf=-1
    while [ -f "${dpath}/${brest}${suf}" ]; do
      ((isuf=isuf+1))
      suf="_${isuf}"
    done
    if [ ! -z "$suf" ]; then
      echo "mv ${dpath}/${brest} ${dpath}/${brest}${suf} # `date`" >> download_to_place.out
      mv ${dpath}/${brest} ${dpath}/${brest}${suf} >> download_to_place.out 2>> download_to_place.err
    fi
	echo "cp -f ${ifl} ${dpath}/${brest} # `date`" >> download_to_place.out 
	cp -f ${ifl} ${dpath}/${brest} >> download_to_place.out 2>> download_to_place.err
   done < $file
   echo "" > ~/Downloads/download_to_place.lst
   find ${HOME}/Downloads -name "${pdp}*.*" -mtime -1m -newer ${nthan} -type f > ${HOME}/Downloads/download_to_place.lst #2> /dev/null
   echo "" > ${HOME}/Downloads/download_to_place.alt
  fi
  sleep 10
done

exit
else
 echo "Needs to be an existant directory PATH on the disk"
fi
exit
