#!/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

[[ -v 1 ]] && DIR=$1 || DIR=$PWD
cd $DIR

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

    # Handle webp as a special case, and defer to next iteration
    if [ ${f##*.} == "webp" ]
    then
      if [ `identify $f | wc -l` -gt 1 ]
      then
        echo "Animated webp. Converting to gif..."
        convert $f ${f%.*}.gif
        rm $f
        continue
      else
        echo "Picture webp. Converting to jpg..."
        convert $f ${f%.*}.jpg
        rm $f
        continue
      fi
    fi

    # Determine phash and destination folder
    # If you don't have your own phash algorithm at home,
    # you might consider using md5sum
    # NAME=`md5sum $f | awk '{print $1}'`
    NAME=`dctfilename $f`
    FOLDER=${f%.*}
    FOLDER=${FOLDER%\(*}

    # Check for duplicate phashes in the .known-files file
    if fgrep -q $NAME .known-files; then
      echo "$NAME is a duplicate file"
      mkdir -p duplicates
      mv -f $f "duplicates/$NAME.${f##*.}"
    else
      echo $NAME >> .known-files
      mkdir -p $FOLDER
      echo "$f"
      echo `dctfilename $f -v`
      mv -f $f "$FOLDER/$NAME.${f##*.}"
    fi
    echo '------'
  done
  sleep 1
done
