Add a verb
[llpp.git] / llppac
blob506b3d3650af2935010e193d8b9cbdda2ed4be6b
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;;
24 ?) die "usage: $0 [-m mime/type] [-t filter] [-f] path";;
25 esac
26 done
27 shift $(($OPTIND - 1))
28 test -z "$1" && die "usage $0: path"
30 ft=$(file -L --mime-type -b "$1") || die "$ft"
32 case $ft in
33 application/x-gzip | application/x-compress) dc=zcat;;
34 application/x-xz) dc=xzcat;;
35 application/x-bzip2) dc=bzcat;;
36 *) unset dc || true;;
37 esac
39 filt='"${dc-cat}" "$1" |'
41 if test -z "$type"; then
42 test -z "$dc" || ft=$(file -L --mime-type -bz "$1") || die "$ft"
44 mime=${mime:-$ft}
45 case $mime in
46 application/postscript) type=ps;;
47 application/pdf) type=pdf;;
48 image/vnd.djvu) type=djvu;;
49 text/html) type=html;;
50 application/msword) type=word;;
51 application/vnd.openxmlformats-officedocument.* \
52 | application/vnd.ms-powerpoint \
53 | application/vnd.ms-excel \
54 | application/vnd.oasis.opendocument.*) type=uno;;
55 image/svg+xml) type=svg;;
56 image/png | image/jpeg) test -n "$dc" && type="image" || type="image2";;
57 image/*) type=image;;
58 application/x-dvi) type=dvi;;
59 *) die "unhandled file type: '$mime'";;
60 esac
63 opts=
65 case $type in
66 ps) conv='ps2pdf - "$casp"';;
67 image2|pdf) test -z "$dc" && exec llpp "$@" || conv='cat >"$casp"';;
68 djvu) conv='djvups - | ps2pdf - "$casp"';;
69 html) {
70 test -n "$dc" && die "can not convert compressed '$mime'"
71 unset filt
72 conv='prince "$1" -o "$casp"'
73 };;
74 word) {
75 if executable_p unoconv && test -z "$dc"; then
76 unset filt
77 conv='unoconv -o "$casp" "$1"'
78 else
79 conv='antiword -m 8859-1.txt -a a4 - >"$casp"'
81 };;
82 uno) {
83 test -n "$dc" && die "can not convert compressed '$mime'"
84 unset filt
85 conv='unoconv -o "$casp" "$1"'
86 };;
87 svg) {
88 executable_p rsvg-convert \
89 && conv='rsvg-convert -f pdf -o "$casp"' \
90 || conv='inkscape -z -A "$casp" -'
91 };;
92 image) conv='convert - pdf:"$casp"';;
93 dvi) {
94 test -n "$dc" && die "can not convert compressed '$mime'"
95 unset filt
96 conv='dvipdf "$1" "$casp"'
97 };;
98 *) die "unhandled filter type: '$type'";;
99 esac
101 hash=$(md5sum "$1") || die "$hash"
102 casp=$CACHEDIR/${hash%% *}
103 (test -n "$force" -o ! -e "$casp") && eval "$filt" "$conv"
105 exec llpp $opts -origin "$@" "$casp"