cbr-convert.sh 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #!/bin/sh
  2. #
  3. # author: lotus
  4. # license: isc (SEE LICENSE.md)
  5. # website: git.zerohack.xyz/lotus/cbr-convert
  6. #
  7. # --- Convert comic book files from .cbr to .cbz ---
  8. # The .cbr (comic book rar) file format is proprietary and doesn't play well
  9. # with all comic book readers. This script will recurse a directory and convert
  10. # each .cbr file found to .cbz
  11. #
  12. # Dependencies: trash-cli, zip, unar, gnu parallel
  13. VERSION="0.1.1"
  14. JOBS=2
  15. LOGDIR=~/.cbr-convert
  16. LOGFILE=$LOGDIR/errors.log
  17. trap fun_cleanup INT
  18. fun_echo_dirs () {
  19. SAVEIFS=$IFS
  20. IFS='
  21. '
  22. for DIR in $(find "$@") ; do
  23. if [ -d "$DIR" ] ; then
  24. echo "$DIR"
  25. fi
  26. done
  27. IFS=$SAVEIFS
  28. }
  29. # call this function to start the script
  30. fun_run () {
  31. fun_echo_dirs "$@" | parallel -j $JOBS runner-cbr "secretargument" {}
  32. }
  33. # log that an error occurred
  34. fun_log_error () {
  35. if ! [ -d $LOGDIR ] ; then
  36. mkdir $LOGDIR
  37. fi
  38. echo "$(date): $1" | tee -a $LOGFILE
  39. echo "(logged @ $LOGFILE)"
  40. }
  41. # pass the error message, we echo it, log it, and print the help info
  42. fun_error () {
  43. fun_log_error "$1"
  44. echo ""
  45. fun_print_usage
  46. exit 1
  47. }
  48. # run when ctrl-c is pressed
  49. fun_cleanup () {
  50. echo "caught sigint, exiting.."
  51. exit 0
  52. }
  53. fun_print_usage () {
  54. echo "usage:"
  55. echo " cbr-convert ./MyComicsDir # convert all cbr files found in this directory"
  56. echo " cbr-convert -j 8 ./Deadpool # convert using 8 threads"
  57. echo " cbr-convert -h # show this help message"
  58. echo " cbr-convert -v # show version"
  59. echo ""
  60. echo "!!! - After conversion .cbr files are moved to trash - !!!"
  61. echo "(Note that this will recurse down into each subdirectory)"
  62. }
  63. fun_print_info () {
  64. echo "cbr-convert - .cbr comic book files to .cbz"
  65. echo "author: lotus"
  66. echo "license: isc"
  67. fun_print_ver
  68. echo ""
  69. fun_print_usage
  70. }
  71. fun_print_ver () {
  72. echo "cbr-convert version: $VERSION"
  73. }
  74. # checks if we were passed a valid dir
  75. fun_check_dir () {
  76. if [ "$#" -gt 0 ] && [ -d "$*" ] && [ "$(ls -A "$@")" ] ; then
  77. echo "Starting up..."
  78. fun_run "$@"
  79. else
  80. fun_error "Please pass in a valid (i.e. non-empty) directory"
  81. fi
  82. }
  83. # set the number of jobs (threads) to spawn
  84. fun_set_numjobs () {
  85. if [ "$1" = "-j" ] ; then
  86. JOBS="$2"
  87. shift; shift
  88. fun_check_dir "$@"
  89. else
  90. fun_error "Bad State! - fun_set_numjobs"
  91. fi
  92. }
  93. fun_check_for_errors () {
  94. if [ -s $LOGFILE ] ; then
  95. echo "Potential errors detected!"
  96. echo "Please check file $LOGFILE to verify"
  97. fi
  98. }
  99. fun_clear_old_log () {
  100. if [ -s $LOGFILE ] ; then
  101. trash-put $LOGFILE
  102. fi
  103. }
  104. # parse what arguments were passed in
  105. # call a corresponding function
  106. fun_check_args () {
  107. if [ "$#" -ge 1 ] ; then
  108. case "$1" in
  109. -j) fun_set_numjobs "$@" ;;
  110. -h) fun_print_info "$@" ;;
  111. -v) fun_print_ver "$@" ;;
  112. *) fun_check_dir "$@" ;;
  113. esac
  114. else
  115. fun_error "Pass in a directory or flag!"
  116. fi
  117. }
  118. fun_check_deps () {
  119. if ! [ -x "$(command -v unar)" ]; then
  120. fun_error "error: cbr-convert needs unar, please install it."
  121. elif ! [ -x "$(command -v parallel)" ]; then
  122. fun_error "error: cbr-convert needs GNU parallel, please install it."
  123. elif ! [ -x "$(command -v trash-put)" ]; then
  124. fun_error "error: cbr-convert needs trash-cli, please install it."
  125. elif ! [ -x "$(command -v zip)" ]; then
  126. fun_error "error: cbr-convert needs zip, please install it."
  127. fi
  128. }
  129. # -- ENTRY POINT--
  130. fun_check_deps
  131. fun_clear_old_log
  132. fun_check_args "$@"
  133. fun_check_for_errors
  134. exit 0