Make sure to catch checksum errors
[llpp.git] / llppac
blob149664cd4d8b06965d2c317a516b9a78d2fe90a8
1 #!/bin/sh
3 set -e
5 CACHEDIR=$HOME/.config/llpp
7 die() {
8 echo "$1" >&2
9 exit 1
12 executable_p() {
13 command -v $1 >/dev/null 2>&1
16 trap '! test -z $casp && rm -f $casp' 0
18 test -d $CACHEDIR || die "cache directory $CACHEDIR does not exist"
19 while getopts m: opt; do
20 case "$opt" in
21 m) mime="$OPTARG";;
23 die "usage: $0 [-t mime/type] path";
24 exit 1;;
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) dc=zcat;;
34 application/x-xz) dc=xzcat;;
35 application/x-bzip2) dc=bzcat;;
36 *) unset dc || true;;
37 esac
39 test -z $dc || ft=$(file -L --mime-type -bz "$1") || die "$ft"
41 filt='${dc-cat} "$1" |'
43 mime=${mime:-$ft}
44 case $mime in
45 application/postscript) conv='ps2pdf - $casp';;
46 application/pdf) test -z $dc && exec llpp "$1" || conv='cat >$casp';;
47 image/vnd.djvu) conv='djvups - | ps2pdf - $casp';;
48 text/html) {
49 ! test -z $dc && die "can not convert compressed $mime"
50 unset filt
51 conv='prince "$1" -o $casp'
52 };;
53 application/msword) {
54 if executable_p unoconv && test -z $dc; then
55 unset filt
56 conv='unoconv -o $casp "$1"'
57 else
58 conv='antiword -m 8859-1.txt -a a4 - >$casp'
60 };;
61 application/vnd.openxmlformats-officedocument.*) {
62 ! test -z $dc && die "can not convert compressed $mime"
63 unset filt
64 conv='unoconv -o $casp "$1"'
65 };;
66 image/svg+xml) {
67 executable_p rsvg-convert \
68 && conv='rsvg-convert -f pdf -o $casp' \
69 || conv='inkscape -z -A $casp -'
70 };;
71 image/*) conv='convert - pdf:$casp';;
72 application/x-dvi) {
73 ! test -z $dc && die "can not convert compressed $mime"
74 unset filt
75 conv='dvipdf $1 $casp'
76 };;
77 *) die "unhandled file type: $mime";;
78 esac
80 hash=$(md5sum "$1") || die "$hash"
81 hash=${hash%% *}
82 casp="$CACHEDIR/$hash"
83 test -e $casp || eval "$filt" "$conv"
85 exec llpp -origin "$@" $casp