sync.sh 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/bin/bash
  2. # Parameters
  3. TARGET="Heady"
  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. SUB_DIR="`echo "${SUB_DIR}" | sed 's%\?%%g'`"
  15. # Allow absolute paths, or those relative to the media path
  16. if [ "`echo "${SUB_DIR}" | head -c 1`" == '/' ]; then
  17. SUB_DIR="`echo "${SUB_DIR}" | sed "s%^${BASE_DIR}/%%"`"
  18. fi
  19. # Allow single-file use
  20. if [ -f "${BASE_DIR}/${SUB_DIR}" ]; then
  21. FILE="`basename "${SUB_DIR}"`"
  22. SUB_DIR="`dirname "${SUB_DIR}"`"
  23. fi
  24. # Construct the final paths
  25. IN_DIR="${BASE_DIR}/${SUB_DIR}"
  26. OUT_DIR="${BASE_DIR}/${TARGET}/${SUB_DIR}"
  27. # Sanity check
  28. if [ ! -d "${IN_DIR}" ]; then
  29. echo "Invalid input directory: ${IN_DIR}" 1>&2
  30. exit 1
  31. fi
  32. # Remember where we came from
  33. START_DIR="`pwd`"
  34. # Go home
  35. cd "${IN_DIR}"
  36. # Ensure the output directory exists
  37. if [ ! -d "${OUT_DIR}" ]; then
  38. mkdir -p "${OUT_DIR}"
  39. fi
  40. # Export options for the encode script
  41. export OUT_DIR
  42. export MOBILE=1
  43. # Handle single-file, whole-folder, or recursive use
  44. if [ -n "${FILE}" ]; then
  45. FILES="${FILE}"
  46. else
  47. FILES="`ls [0-9]*\ -\ *.* 2>/dev/null`"
  48. fi
  49. # Allow recursive use for seasons
  50. for season in Season\ [0-9]*; do
  51. if [ ! -d "${season}" ]; then
  52. continue
  53. fi
  54. # Recurse for the season directory
  55. CMD="${0}"
  56. if [ "`echo "${CMD}" | head -c 1`" != '/' ]; then
  57. CMD="${START_DIR}/${0}"
  58. fi
  59. "${CMD}" "${IN_DIR}/${season}"
  60. # Disable the non-recursive case processing
  61. FILES=""
  62. done
  63. # Encode any missing files
  64. IFS=$'\n'
  65. for infile in $FILES; do
  66. # Skip non-files
  67. if [ ! -f "${infile}" ]; then
  68. continue
  69. fi
  70. # Don't get hung up on file extensions
  71. nobase="`echo "${infile}" | sed 's%\....$%%'`"
  72. # Skip files that exist
  73. if ls "${OUT_DIR}/${nobase}".* >/dev/null 2>&1; then
  74. if [ $DEBUG -gt 0 ]; then
  75. echo "Skipping: ${infile}" 1>&2
  76. fi
  77. continue
  78. fi
  79. # Encode things that do not
  80. ~/bin/video/encode.pl "${IN_DIR}/${infile}"
  81. done