sync.sh 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #!/bin/bash
  2. # Parameters
  3. TARGET="Sync"
  4. BASE_DIR="`~/bin/video/mediaPath`"
  5. # Environmental parameters
  6. if [ "${DEBUG}" ]; then
  7. DEBUG=1
  8. else
  9. DEBUG=0
  10. fi
  11. # Command-line parameters
  12. SUB_DIR="${1}"
  13. # Deal with SMB mappings
  14. # I need some way to detect when this is necessary -- maybe try both paths?
  15. #SUB_DIR="`echo "${SUB_DIR}" | sed 's%\?%%g'`"
  16. # Allow absolute paths, or those relative to the media path
  17. if [ "`echo "${SUB_DIR}" | head -c 1`" == '/' ]; then
  18. SUB_DIR="`echo "${SUB_DIR}" | sed "s%^${BASE_DIR}/%%"`"
  19. fi
  20. # Allow single-file use
  21. if [ -f "${BASE_DIR}/${SUB_DIR}" ]; then
  22. FILE="`basename "${SUB_DIR}"`"
  23. SUB_DIR="`dirname "${SUB_DIR}"`"
  24. fi
  25. # Construct the final paths
  26. IN_DIR="${BASE_DIR}/${SUB_DIR}"
  27. OUT_DIR="${BASE_DIR}/${TARGET}/${SUB_DIR}"
  28. # Sanity check
  29. if [ ! -d "${IN_DIR}" ]; then
  30. echo "Invalid input directory: ${IN_DIR}" 1>&2
  31. exit 1
  32. fi
  33. # Remember where we came from
  34. START_DIR="`pwd`"
  35. # Go home
  36. cd "${IN_DIR}"
  37. # Ensure the output directory exists
  38. if [ ! -d "${OUT_DIR}" ]; then
  39. mkdir -p "${OUT_DIR}"
  40. fi
  41. # Export options for the encode script
  42. export OUT_DIR
  43. export MOBILE=1
  44. # Handle single-file, whole-folder, or recursive use
  45. if [ -n "${FILE}" ]; then
  46. FILES="${FILE}"
  47. else
  48. FILES="`ls [0-9]*\ -\ *.* 2>/dev/null`"
  49. fi
  50. # Allow recursive use for seasons
  51. for season in Season\ [0-9]*; do
  52. if [ ! -d "${season}" ]; then
  53. continue
  54. fi
  55. # Recurse for the season directory
  56. CMD="${0}"
  57. if [ "`echo "${CMD}" | head -c 1`" != '/' ]; then
  58. CMD="${START_DIR}/${0}"
  59. fi
  60. "${CMD}" "${IN_DIR}/${season}"
  61. # Disable the non-recursive case processing
  62. FILES=""
  63. done
  64. # Encode any missing files
  65. IFS=$'\n'
  66. for infile in $FILES; do
  67. # Skip non-files
  68. if [ ! -f "${infile}" ]; then
  69. continue
  70. fi
  71. # Don't get hung up on file extensions
  72. nobase="`echo "${infile}" | sed 's%\....$%%'`"
  73. # Copy NFO and other metadata, if present
  74. OLD_IFS="${IFS}"
  75. IFS=" "
  76. for meta in "${nobase}.nfo" tvshow.nfo poster.jpg poster.png; do
  77. if [ -e "${IN_DIR}/${meta}" ]; then
  78. if [ ! -e "${OUT_DIR}/${meta}" ]; then
  79. if [ $DEBUG -gt 0 ]; then
  80. echo "Copying: ${IN_DIR}/${meta}" 1>&2
  81. fi
  82. cp "${IN_DIR}/${meta}" "${OUT_DIR}/"
  83. fi
  84. fi
  85. done
  86. IFS="${OLD_IFS}"
  87. # Skip video files that exist
  88. if ls "${OUT_DIR}/${nobase}.m"[4k]v >/dev/null 2>&1; then
  89. if [ $DEBUG -gt 0 ]; then
  90. echo "Exists: ${SUB_DIR}/${infile}" 1>&2
  91. fi
  92. continue
  93. fi
  94. # Encode things that do not
  95. ERR="`~/bin/video/encode.pl "${IN_DIR}/${infile}" 2>&1`"
  96. # If the singular output error is "already exists" assume the error is related to filename encoding
  97. # Suppress the error and touch the output file to prevent it from being deleted
  98. if [ -n "${ERR}" ]; then
  99. if [ `echo "${ERR}" | wc -l` -eq 1 ] && \
  100. echo "${ERR}" | grep -q 'encode.pl: Output file exists: '; then
  101. OUTFILE="`echo "${ERR}" | \
  102. sed 's%encode.pl: Output file exists: %%' | \
  103. sed 's%. Skipping...%%'`"
  104. touch "${OUTFILE}"
  105. else
  106. echo "${ERR}" 1>&2
  107. fi
  108. fi
  109. done