summaryrefslogtreecommitdiff
path: root/office/convertlit/lit2epub
blob: 3dca90e68b5c9e5227fba2ac2e3ae40d5b19bbe8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/bin/bash

# 20130220: lit2epub by B. Watson (part of convertlit slackbuilds.org build)
# Licensed under the WTFPL.

# Wrapper script for convertlit, zips up convertlit's output dir
# and cleans it up afterward. Leave the shebang line alone, this
# uses at least one bash-specific feature.

NAME=$( basename $0 )
TMP=${TMP:-/tmp}
OUTDIR=$TMP/$NAME.$$.$RANDOM

set -e

if [ "$1" = "" -o "$1" = "--help" -o "$1" = "-h" ]; then
  cat <<EOF
$NAME - convert a DRM1 .lit file to .epub

Usage: $NAME input.lit [ output.epub ]

Default output file is written to the current directory, named after the
input filename with the .lit or .LIT extension changed to .epub, or the
input filename with .epub appended, if there is no .lit extension.

Use - for the output file, to output to stdout.

Exit status is 0 on success, non-zero on failure.

If you need to convert a non-DRM1 .lit file, use convertlit to downconvert
to DRM1 first.
EOF
  exit 0
fi

INFILE="$1"
case "$INFILE" in
  *.lit) EXT=.lit ;;
  *.LIT) EXT=.LIT ;;
  *) EXT="" ;;
esac

trap "rm -rf \"$OUTDIR\"" EXIT ERR

# All this rigamarole with dirname/basename/readlink needed because we run
# zip from within our temp directory.
if [ "$2" = "" ]; then
  OUTFILE="$( pwd )/$( basename "$INFILE" $EXT ).epub"
  if [ -e "$OUTFILE" ]; then
    echo "$NAME: $OUTFILE already exists, aborting" 1>&2
    exit 1
  fi
elif [ "$2" = "-" ]; then
  OUTFILE="$OUTDIR/output.epub"
else
  OUTFILE="$( readlink -f "$OUTFILE" )"
fi

# convertlit and zip both want to spew status messages to stdout,
# redirect to stderr so we can output just the .epub to stdout.
convertlit -d "$INFILE" "$OUTDIR"/ 1>&2
( cd "$OUTDIR" ; zip -r "$OUTFILE" * 1>&2 )

if [ "$2" = "-" ]; then
  cat "$OUTFILE"
  OUTFILE="(standard output)"
fi

echo 1>&2
echo "$INFILE => $OUTFILE" 1>&2
exit 0