#!/bin/bash
# Watch this folder for incoming files
# Rename each file to a hash of some kind
# Put each file into a folder of the old name

# Check for .known-files which tracks hash history to prevent duplicates
# Will create if -f flag is used, otherwise exits to prevent accidental runs
if [[ ! -f .known-files ]]; then
  if [[ "$1" != "-f" ]]; then
    echo ".known-files not found. Use -f to override."
    exit 1
  fi
  touch .known-files
fi

command -v file >/dev/null 2>&1 || { echo "file command not found"; exit 1; }
command -v magick >/dev/null 2>&1 || { echo "imagemagick not found"; exit 1; }
command -v dctfilename >/dev/null 2>&1 || { echo "dctfilename not found"; exit 1; }
command -v webpmux >/dev/null 2>&1 || { echo "webpmux not found"; exit 1; }
command -v gifski >/dev/null 2>&1 || { echo "gifski not found"; exit 1; }

while true; do
  for f in *.*; do
    # Check if we need to skip
    [ -e "$f" ] || continue
    [ -e "$f.part" ] && continue
    [ ${f##*.} == "part" ] && continue
    [ ${f##*.} == "sh" ] && continue

    # Print some file info, and check file validity
    FILEINFO=`file $f`
    echo $FILEINFO
    if echo "$FILEINFO" | grep -q empty; then
      continue
    fi
    if ! echo "$FILEINFO" | grep -iq "image\|video"; then
      continue
      echo "skip"
    fi

    # Handle webp as a special case, and defer to next iteration
    if [ ${f##*.} == "webp" ]
    then
      if [ `magick identify $f | wc -l` -gt 1 ]
      then
        echo "Animated webp. Converting to gif..."
        TMPDIR=$(mktemp -d)
        magick "$f" "$TMPDIR/frame-%d.png"
        DELAYS=$(webpmux -info "$f" | tail -n+6 | awk '{print $7}')
        FPS=$(echo "$DELAYS" | head -n1 | awk '{printf "%.0f", 1000/$1}')
        gifski --fps $FPS "$TMPDIR"/frame-*.png -o "${f%.*}.gif"
        rm -rf "$TMPDIR"
        rm "$f"
        continue
      else
        echo "Picture webp. Converting to jpg..."
        magick "$f" "${f%.*}.jpg"
        rm "$f"
        continue
      fi
    fi

    # Determine phash and destination folder
    if command -v dctfilename >/dev/null 2>&1 && ! echo "$FILEINFO" | grep -iq "video"; then
      NAME=`dctfilename $f`
    else
      NAME=`md5sum $f | awk '{print $1}'`
    fi
    FOLDER=${f%.*}
    FOLDER=${FOLDER%\(*}

    # Check if the file's hash already exists in .known-files
    if grep -F -q $NAME .known-files; then
      echo "$NAME is a duplicate file"
      mkdir -p duplicates
      mv -f $f "duplicates/$NAME.${f##*.}"
    else
      # For new files, add hash to .known-files and move to destination folder
      echo $NAME >> .known-files
      mkdir -p $FOLDER
      echo "Moving '$f' to '$FOLDER/$NAME.${f##*.}'"
      mv -f $f "$FOLDER/$NAME.${f##*.}"
    fi
    echo '------'
  done
  sleep 1
done
