tvdup.sh 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/bin/bash
  2. set failglob
  3. BASE="/mnt/media/TV"
  4. NOW="`date '+%s'`"
  5. THEN=$(( $NOW - 3600 ))
  6. function updateCount() {
  7. for i in "${dir}/${num} - "*; do
  8. if echo "${i}" | grep -q '.nfo$'; then
  9. continue
  10. fi
  11. if echo "${i}" | grep -q '.srt$'; then
  12. continue
  13. fi
  14. count=$(( $count + 1 ))
  15. done
  16. }
  17. declare -A seen
  18. while IFS= read -r -d '' lpath; do
  19. # Reset the loop
  20. count=0
  21. file="`basename "${lpath}"`"
  22. dir="`dirname "${lpath}"`"
  23. num="`echo "${file}" | sed 's%\([0-9]*\).*%\1%'`"
  24. # Only process things in a format we understand
  25. if [ -z "${num}" ]; then
  26. echo "Error on: ${lpath}" 1>&2
  27. continue
  28. fi
  29. # Skip if we're down to one
  30. updateCount
  31. if [ $count -lt 2 ]; then
  32. continue
  33. fi
  34. # Build a list of checked files and skip dups
  35. id="${dir}/${num} - "
  36. if [ -n "${seen["${id}"]}" ]; then
  37. continue
  38. fi
  39. seen["${id}"]=1
  40. # Skip things modified since $THEN
  41. if [ "`uname`" == "Darwin" ]; then
  42. MTIME="`stat -f '%m' "${id}"*`"
  43. else
  44. MTIME="`stat -c '%Y' "${id}"*`"
  45. fi
  46. MTIME="`echo "${MTIME}" | sort | head -n 1`"
  47. if [ $MTIME -gt $THEN ]; then
  48. echo "Skipping recent file: ${lpath}" 1>&2
  49. continue
  50. fi
  51. # Delete empty files
  52. DEL=0
  53. for i in "${BASE}/${short}"*; do
  54. if [ -s "${id}" ]; then
  55. continue
  56. fi
  57. sudo chattr -i "${i}"
  58. echo rm "${i}"
  59. DEL=1
  60. done
  61. # Skip if we're down to one
  62. updateCount
  63. if [ $count -lt 2 ]; then
  64. continue
  65. fi
  66. # Use mkvRename
  67. sudo chattr -i "${id}"*
  68. echo mkvRename "${lpath}"
  69. # Skip if we're down to one
  70. updateCount
  71. if [ $count -lt 2 ]; then
  72. continue
  73. fi
  74. # Human intervention
  75. sudo chattr -i "${id}"*
  76. touch "${id}"*
  77. echo "Multiple matches for: ${id}" 1>&2
  78. short="`echo "${id}" | sed "s%^${BASE}/%%"`"
  79. (cd "${BASE}" && ls -lhtQ "${short}"*)
  80. done< <(find "${BASE}" -type f -path '*Season*' -name '[0-9]*.*' -print0)