lit2epub 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/bin/bash
  2. # 20130220: lit2epub by B. Watson (part of convertlit slackbuilds.org build)
  3. # Licensed under the WTFPL.
  4. # Wrapper script for convertlit, zips up convertlit's output dir
  5. # and cleans it up afterward. Leave the shebang line alone, this
  6. # uses at least one bash-specific feature.
  7. NAME=$( basename $0 )
  8. TMP=${TMP:-/tmp}
  9. OUTDIR=$TMP/$NAME.$$.$RANDOM
  10. set -e
  11. if [ "$1" = "" -o "$1" = "--help" -o "$1" = "-h" ]; then
  12. cat <<EOF
  13. $NAME - convert a DRM1 .lit file to .epub
  14. Usage: $NAME input.lit [ output.epub ]
  15. Default output file is written to the current directory, named after the
  16. input filename with the .lit or .LIT extension changed to .epub, or the
  17. input filename with .epub appended, if there is no .lit extension.
  18. Use - for the output file, to output to stdout.
  19. Exit status is 0 on success, non-zero on failure.
  20. If you need to convert a non-DRM1 .lit file, use convertlit to downconvert
  21. to DRM1 first.
  22. EOF
  23. exit 0
  24. fi
  25. INFILE="$1"
  26. case "$INFILE" in
  27. *.lit) EXT=.lit ;;
  28. *.LIT) EXT=.LIT ;;
  29. *) EXT="" ;;
  30. esac
  31. trap "rm -rf \"$OUTDIR\"" EXIT ERR
  32. # All this rigamarole with dirname/basename/readlink needed because we run
  33. # zip from within our temp directory.
  34. if [ "$2" = "" ]; then
  35. OUTFILE="$( pwd )/$( basename "$INFILE" $EXT ).epub"
  36. if [ -e "$OUTFILE" ]; then
  37. echo "$NAME: $OUTFILE already exists, aborting" 1>&2
  38. exit 1
  39. fi
  40. elif [ "$2" = "-" ]; then
  41. OUTFILE="$OUTDIR/output.epub"
  42. else
  43. OUTFILE="$( readlink -f "$2" )"
  44. fi
  45. # convertlit and zip both want to spew status messages to stdout,
  46. # redirect to stderr so we can output just the .epub to stdout.
  47. convertlit -d "$INFILE" "$OUTDIR"/ 1>&2
  48. ( cd "$OUTDIR" ; zip -r "$OUTFILE" * 1>&2 )
  49. if [ "$2" = "-" ]; then
  50. cat "$OUTFILE"
  51. OUTFILE="(standard output)"
  52. fi
  53. echo 1>&2
  54. echo "$INFILE => $OUTFILE" 1>&2
  55. exit 0