#!/bin/bash
###################################################
#                                                 #
#  autoprint-server.sh                            #
#  Searches in print folder for files to print    #
#  Searches in tmp folder for files to delete     #
#                                                 #
#                                   version 1.1   #
###################################################

# Path where this script searches for files to print
printdir="/autoprint/print"

# Path where this script searches for uncompleted files to delete
tmpdir="/autoprint/tmp"

# Time before tmp file gets deleted if uncomplete (in seconds)
deltime="300"

# Delay time to scan print folder again (in seconds)
scandelay="5"

# Notify popup  (yes/no)
notify="yes"

# Keep a log /var/log/autoprint.log  (yes/no)
logfile="yes"



############################################################################################
########################  CHANGE AT OWN RISK BELOW THIS LINE  ##############################
############################################################################################

cleanup_tmp () {
while :
do

   # Search for files to delete from tmp dir
   while read -r tmpfile
   do
      # Check if file is closed
      tfid=$(lsof -t "$tmpfile"); if [ "$tfid" = "" ]; then tfid=0; fi

      # If greater then 0, file is open
      if [ "$tfid" -gt 0 ]; then
        echo "$tmpfile still open for wrting..."
      else
        filetime=$(stat -c %Y "$tmpfile")
        timediff=$(echo $EPOCHSECONDS-$filetime | bc)
        # If file is closed and still here after x seconds, then we can asure the
        # copying of the file has failed and can be removed.
        if [ "$timediff" -gt "$deltime" ]; then
           rm -f "$tmpfile"
           # Leave a message in log file
           if [ "$logfile" = "yes" ]; then
             echo "$(basename $tmpfile)  is deleted on  $(date +'%d %b %Y  %H:%M')" | sudo tee -a /var/log/autoprint.log
           fi
           # Show notify message
           if [ "$notify" = "yes" ]; then
             notify-send --urgency=normal "AutoPrint:  $(basename $tmpfile)  is deleted on  $(date +'%d %b %Y  %H:%M') "
           fi
        fi
      fi

   done < <(stat "$tmpdir"/* --format='%n') # list all filenames in tmp dir

sleep "$scandelay"  # Sleep 'scandelay' seconds and check folder again for new files
done
}

cleanup_tmp &



#######  Start print loop  ########
while :
do

   # Search for files to print
   while read -r file
   do

      # Check if file is closed
      fid=$(lsof -t "$file")
      if [ "$fid" = "" ]; then fid="0"; fi

         while [ "$fid" -gt "0" ]
         do
            fid=$(lsof -t "$file")
            echo "Check if $file is closed..."
            sleep 1
         done

      # Line below is for test purpose on terminal, line can be deleted or comment it out
      echo "Print  $(basename $file)  and remove it"

      # lp "$file"
      # wait
      rm -f "$file"

      # Leave a message in log file
      if [ "$logfile" = "yes" ]; then
        echo "$(basename $file)  is printed on  $(date +'%d %b %Y  %H:%M')" | sudo tee -a /var/log/autoprint.log
      fi
      # Show notify message
      if [ "$notify" = "yes" ]; then
        notify-send --urgency=normal "AutoPrint:  $(basename $file)  is printed on  $(date +'%d %b %Y  %H:%M') "
      fi

   done < <(stat "$printdir"/* --format='%n') # list all filenames in $printdir

sleep "$scandelay"  # Sleep 'scandelay' seconds and check folder again for new files
done

