sync.sh 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. base="`echo "${infile}" | sed 's%^.*\.\(.*\)$%\1%'`"
  73. nobase="`echo "${infile}" | sed 's%\....$%%'`"
  74. # Skip things that are clearly not video files
  75. if [ "${base}" == "fake" ]; then
  76. if [ $DEBUG -gt 0 ]; then
  77. echo "Non-video file: ${SUB_DIR}/${infile}" 1>&2
  78. fi
  79. continue
  80. fi
  81. # Copy NFO and other metadata, if present
  82. OLD_IFS="${IFS}"
  83. IFS=" "
  84. for meta in "${nobase}.nfo" tvshow.nfo poster.jpg poster.png \
  85. "../${nobase}.nfo" ../tvshow.nfo ../poster.jpg ../poster.png; do
  86. if [ -e "${IN_DIR}/${meta}" ]; then
  87. if [ ! -e "${OUT_DIR}/${meta}" ]; then
  88. if [ $DEBUG -gt 0 ]; then
  89. echo "Copying: ${IN_DIR}/${meta}" 1>&2
  90. fi
  91. cp "${IN_DIR}/${meta}" "${OUT_DIR}/${meta}"
  92. fi
  93. fi
  94. done
  95. IFS="${OLD_IFS}"
  96. # Skip video files that exist
  97. if ls "${OUT_DIR}/${nobase}.m"[4k]v >/dev/null 2>&1; then
  98. if [ $DEBUG -gt 0 ]; then
  99. echo "Exists: ${SUB_DIR}/${infile}" 1>&2
  100. fi
  101. continue
  102. fi
  103. # Encode things that do not
  104. ERR="`~/bin/video/encode.pl "${IN_DIR}/${infile}" 2>&1`"
  105. # If the singular output error is "already exists" assume the error is related to filename encoding
  106. # Suppress the error and touch the output file to prevent it from being deleted
  107. if [ -n "${ERR}" ]; then
  108. if [ `echo "${ERR}" | wc -l` -eq 1 ] && \
  109. echo "${ERR}" | grep -q 'encode.pl: Output file exists: '; then
  110. OUTFILE="`echo "${ERR}" | \
  111. sed 's%encode.pl: Output file exists: %%' | \
  112. sed 's%. Skipping...%%'`"
  113. touch "${OUTFILE}"
  114. else
  115. echo "${ERR}" 1>&2
  116. fi
  117. fi
  118. done