Ubuntu CI: make apt update before apt install
[llpp.git] / misc / llppac
blob46dd72776f34279aac5956f69452c1527e08956b
1 #!/bin/sh
2 set -eu
4 die() {
5 echo "$1" >&2
6 exit 1
9 cachedir="$HOME/.cache/llpp"
10 test -d "$cachedir" || die "cache directory '$cachedir' does not exist"
12 caspsuf=
13 type=
15 executable_p() { command -v "$1" >/dev/null 2>&1; }
17 missing() {
18 executable_p $1 || \
19 eval "$1() { die \"$2 is needed for \$type conversion\"; }"
21 trap 'test -n "${casp-}" && rm -f "$casp"' 0
23 while getopts s:c:m:t:fu opt; do
24 case $opt in
25 m) mime=$OPTARG;;
26 t) type=$OPTARG;;
27 f) force=true;;
28 c) css="-s $OPTARG";;
29 ?) die "usage: $0 [-c css] [-m mime/type] [-t filter] [-f] [path|url]";;
30 esac
31 done
32 shift $(($OPTIND - 1))
33 test -z "$1" && die "usage $0: path"
35 origin="$1"
36 if ${force-test ! -e "$1"} && expr >/dev/null "$1" : "\(ftp\|https\?\)://";
37 then
38 if executable_p wget; then
39 dl() { wget -q $1 -O $2; }
40 elif executable_p curl; then
41 dl() { curl -L $1 -o $2; }
42 else
43 die "no program to fetch remote urls found"
46 hashof="$cachedir/$(basename "$1")"
47 dl "$1" "$hashof" || test -e "$md5of"
48 shift
49 set -- "$hashof" "$@"
50 else
51 hashof="$1"
54 test -z "$type" && {
55 ft=$(file -L --mime-type -b "$1") || die "$ft";
57 case $ft in
58 application/x-gzip | application/x-compress) dc=zcat;;
59 application/x-xz) dc=xzcat;;
60 application/x-bzip2) dc=bzcat;;
61 *) unset dc || true;;
62 esac
65 filt='"${dc-cat}" "$1" |'
67 typeofmime() {
68 case "$1" in
69 application/postscript) type=ps;;
70 application/pdf) type=pdf;;
71 image/vnd.djvu) type=djvu;;
72 text/html) type=html;;
73 text/plain) type=text;;
74 application/msword) type=word;;
75 application/vnd.openxmlformats-officedocument.* \
76 | application/vnd.ms-powerpoint \
77 | application/vnd.ms-excel \
78 | application/vnd.oasis.opendocument.*) type=uno;;
79 image/svg+xml) type=svg;;
80 image/png | image/jpeg)
81 test -n "${dc-}" && type="image" || type="image2";;
82 image/*) type=image;;
83 application/x-dvi) type=dvi;;
84 *) return 1;;
85 esac
86 return 0
89 if test -z "$type"; then
90 test -z "${mime-}" && \
91 mime=$(file -L --mime-type -b "$1" || die "$mime") || \
92 $(file -L --mime-type -bz "$1" || die "$mime")
93 typeofmime "$mime" || die "unhandled file type: '$mime'"
96 caspsuf=".pdf"
97 case $type in
98 ps)
99 missing ps2pdf "ps2df(https://ghostscript.com/)"
100 conv='ps2pdf - "$casp"'
102 image2|pdf) test -z "${dc-}" && exec llpp "$@" || conv='cat >"$casp"';;
103 texi)
104 missing texi2html "texi2html(http://www.nongnu.org/texi2html/)"
105 conv='texi2html - -o $casp'
106 caspsuf=.html
108 djvu)
109 missing ddjvu "ddjvu(http://djvu.sourceforge.net/doc/man/ddjvu.html)"
110 conv='ddjvu -format=pdf - "$casp"'
112 html)
113 missing prince "Prince(http://www.princexml.com/)"
114 conv='prince $css - -o "$casp"'
116 html2epub)
117 missing pandoc "pandoc(http://pandoc.org)"
118 caspsuf=".epub"
119 conv='pandoc -r html - -w epub2 -o "$casp"'
121 word)
122 missing unoconv "unoconv"
123 filt=
124 conv='unoconv -o "$casp" "$1"'
126 uno)
127 test -n "$dc" && die "cannot convert compressed '$mime'"
128 unset filt
129 missing unoconv "unoconv(http://dag.wiee.rs/home-made/unoconv/)"
130 conv='unoconv -o "$casp" "$1"'
132 svg)
133 if executable_p inkscape && test -z "$dc"; then
134 unset filt
135 conv='inkscape -z -A "$casp" "$1"'
136 else
137 if executable_p rsvg-convert; then
138 conv='rsvg-convert -f pdf -o "$casp"'
139 else
140 test -n "$dc" && die "cannot convert compressed '$mime'"
141 unset filt
142 missing unoconv "unoconv(http://dag.wiee.rs/home-made/unoconv/)"
143 conv='unoconv -o "$casp" "$1"'
147 image)
148 missing convert "convert(http://www.imagemagick.org/script/convert.php)"
149 conv='convert - pdf:"$casp"'
151 dvi)
152 test -n "$dc" && die "cannot convert compressed '$mime'"
153 unset filt
154 missing dvipdf "dvipdf(http://ghostscript.com/)"
155 conv='dvipdf "$1" "$casp"'
157 text)
158 missing pandoc "pandoc(http://pandoc.org/)"
159 conv='pandoc -t epub2 - -o "$casp"'
160 caspsuf=.epub
162 *) die "unhandled filter type: '$type'";;
163 esac
165 hash=$(cksum "$hashof") || die "$hash"
166 casp=$cachedir/${hash%% *}$caspsuf
168 { test -n "${force-}" || test ! -e "$casp"; } && eval "$filt" "$conv"
169 shift
171 exec llpp -origin $origin "$@" "$casp"