Sync with upstream
[llpp.git] / llppac
blob7855541dd9f62c62bbbd0f686556991f5dbf7cf4
1 #!/bin/sh
3 set -e
5 CACHEDIR=$HOME/.cache/llpp
7 die() {
8 echo "$1" >&2
9 exit 1
12 executable_p() {
13 command -v "$1" >/dev/null 2>&1
16 trap 'test -n "$casp" && rm -f "$casp"' 0
18 test -d "$CACHEDIR" || die "cache directory '$CACHEDIR' does not exist"
19 while getopts m:t:f opt; do
20 case $opt in
21 m) mime=$OPTARG;;
22 t) type=$OPTARG;;
23 f) force=1;;
25 die "usage: $0 [-m mime/type] [-t filter] [-f] path";
26 exit 1;;
27 esac
28 done
29 shift $(($OPTIND - 1))
30 test -z "$1" && die "usage $0: path"
32 ft=$(file -L --mime-type -b "$1") || die "$ft"
34 case $ft in
35 application/x-gzip | application/x-compress) dc=zcat;;
36 application/x-xz) dc=xzcat;;
37 application/x-bzip2) dc=bzcat;;
38 *) unset dc || true;;
39 esac
41 filt='"${dc-cat}" "$1" |'
43 if test -z "$type"; then
44 test -z "$dc" || ft=$(file -L --mime-type -bz "$1") || die "$ft"
46 mime=${mime:-$ft}
47 case $mime in
48 application/postscript) type=ps;;
49 application/pdf) type=pdf;;
50 image/vnd.djvu) type=djvu;;
51 text/html) type=html;;
52 application/msword) type=word;;
53 application/vnd.openxmlformats-officedocument.* \
54 | application/vnd.ms-powerpoint \
55 | application/vnd.ms-excel \
56 | application/vnd.oasis.opendocument.*) type=uno;;
57 image/svg+xml) type=svg;;
58 image/png | image/jpeg) test -n "$dc" && type="image" || type="image2";;
59 image/*) type=image;;
60 application/x-dvi) type=dvi;;
61 *) die "unhandled file type: '$mime'";;
62 esac
65 suff=
66 opts=
68 case $type in
69 ps) conv='ps2pdf - "$casp"';;
70 pdf) test -z "$dc" && exec llpp "$@" || conv='cat >"$casp"';;
71 djvu) conv='djvups - | ps2pdf - "$casp"';;
72 html) {
73 test -n "$dc" && die "can not convert compressed '$mime'"
74 unset filt
75 conv='prince "$1" -o "$casp"'
76 };;
77 word) {
78 if executable_p unoconv && test -z "$dc"; then
79 unset filt
80 conv='unoconv -o "$casp" "$1"'
81 else
82 conv='antiword -m 8859-1.txt -a a4 - >"$casp"'
84 };;
85 uno) {
86 test -n "$dc" && die "can not convert compressed '$mime'"
87 unset filt
88 conv='unoconv -o "$casp" "$1"'
89 };;
90 svg) {
91 executable_p rsvg-convert \
92 && conv='rsvg-convert -f pdf -o "$casp"' \
93 || conv='inkscape -z -A "$casp" -'
94 };;
95 image) conv='convert - pdf:"$casp"';;
96 image2) {
97 suff=".cbz"
98 opts="-cxack"
99 conv='zip -0q "$casp" "$1"'
101 dvi) {
102 test -n "$dc" && die "can not convert compressed '$mime'"
103 unset filt
104 conv='dvipdf "$1" "$casp"'
106 *) die "unhandled filter type: '$type'";;
107 esac
109 hash=$(md5sum "$1") || die "$hash"
110 casp=$CACHEDIR/${hash%% *}$suff
111 (test -n "$force" -o ! -e "$casp") && eval "$filt" "$conv"
113 exec llpp $opts -origin "$@" "$casp"