Compile C with some optimizations
[llpp.git] / misc / llppac
blob19d4da052e5340e45815a09e6f5b6e09330fe7ab
1 #!/bin/sh
2 set -e
4 die() {
5 echo "$1" >&2
6 exit 1
9 cachedir="${XDG_CACHE_HOME:-$HOME/.cache}/llpp"
10 test -d "$cachedir" || die "cache directory '$cachedir' does not exist"
12 caspsuf=
13 type=
15 executable_p() {
16 command -v "$1" >/dev/null 2>&1
19 maketext() {
20 cat >"$cachedir/fonts/text" <<EOF
21 Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
22 eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
23 minim veniam, quis nostrud exercitation ullamco laboris nisi ut
24 aliquip ex ea commodo consequat. Duis aute irure dolor in
25 reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
26 pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
27 culpa qui officia deserunt mollit anim id est laborum.
28 Posting Soccer Goals On Vine Is Illegal, Say England's Premier League
29 abcdefghizklmnopqrstwxyz
30 ABCDEFGHIZKLMNOPQRSTWXYZ
31 \`1234567890-=\\
32 ~!@#\$%^&*()_+|
33 [];',./
34 {}:"<>?
36 EOF
39 trap 'test -n "$casp" && rm -f "$casp"' 0
41 while getopts m:t:f opt; do
42 case $opt in
43 m) mime=$OPTARG;;
44 t) type=$OPTARG;;
45 f) force=1;;
46 ?) die "usage: $0 [-m mime/type] [-t filter] [-f] [path|url]";;
47 esac
48 done
49 shift $(($OPTIND - 1))
50 test -z "$1" && die "usage $0: path"
52 if test ! -e "$1" && expr >/dev/null "$1" : "\(ftp\|https\?\)://"; then
53 if executable_p wget; then
54 dl() {
55 wget -q -c -nc $1 -O $2
57 elif executable_p curl; then
58 dl() {
59 curl $1 -o $2
61 else
62 die "no program to fetch remote urls found"
65 md5of="$cachedir/$(basename "$1")"
66 dl "$1" "$md5of" || test -e "$md5of"
67 set -- "$md5of" ${2+"@"}
68 else
69 md5of="$1"
72 test -z "$type" && {
73 ft=$(file -L --mime-type -b "$1") || die "$ft"
76 case $ft in
77 application/x-gzip | application/x-compress) dc=zcat;;
78 application/x-xz) dc=xzcat;;
79 application/x-bzip2) dc=bzcat;;
80 *) unset dc || true;;
81 esac
83 filt='"${dc-cat}" "$1" |'
85 if test -z "$type"; then
86 test -z "$dc" || ft=$(file -L --mime-type -bz "$1") || die "$ft"
88 mime=${mime:-$ft}
89 case $mime in
90 application/postscript) type=ps;;
91 application/pdf) type=pdf;;
92 image/vnd.djvu) type=djvu;;
93 text/html) type=html;;
94 application/msword) type=word;;
95 application/vnd.openxmlformats-officedocument.* \
96 | application/vnd.ms-powerpoint \
97 | application/vnd.ms-excel \
98 | application/vnd.oasis.opendocument.*) type=uno;;
99 image/svg+xml) type=svg;;
100 image/png | image/jpeg) test -n "$dc" && type="image" || type="image2";;
101 image/*) type=image;;
102 application/x-dvi) type=dvi;;
103 application/x-font-ttf | application/vnd.ms-opentype) type=font;;
104 *) die "unhandled file type: '$mime'";;
105 esac
108 case $type in
109 ps) conv='ps2pdf - "$casp"';;
110 image2|pdf) test -z "$dc" && exec llpp "$@" || conv='cat >"$casp"';;
111 djvu) conv='djvups - | ps2pdf - "$casp"';;
112 html) {
113 test -n "$dc" && die "cannot convert compressed '$mime'"
114 unset filt
115 conv='prince "$1" -o "$casp"'
117 word) {
118 if executable_p unoconv && test -z "$dc"; then
119 unset filt
120 conv='unoconv -o "$casp" "$1"'
121 else
122 conv='antiword -m 8859-1.txt -a a4 - >"$casp"'
125 uno) {
126 test -n "$dc" && die "cannot convert compressed '$mime'"
127 unset filt
128 conv='unoconv -o "$casp" "$1"'
130 svg) {
131 executable_p rsvg-convert \
132 && conv='rsvg-convert -f pdf -o "$casp"' \
133 || conv='inkscape -z -A "$casp" -'
135 font) {
136 if test -e "$2"; then
137 arg="$2"
138 else
139 arg="--waterfall $cachedir/fonts/text"
140 maketext
142 # https://mail.gnome.org/archives/gtk-i18n-list/2010-June/msg00000.html
143 eval $filt cat >$cachedir/fonts/font
144 cat >$cachedir/fonts/fonts.conf <<EOF
145 <?xml version="1.0"?>
146 <!DOCTYPE fontconfig SYSTEM "fonts.dtd">
147 <fontconfig>
148 <dir>$cachedir/fonts/</dir>
149 <cachedir>$cachedir/fonts/</cachedir>
150 <config></config>
151 </fontconfig>
153 unset filt
154 conv='FONTCONFIG_PATH=$cachedir/fonts/ pango-view -q $arg -o $casp'
155 caspsuf=.pdf
157 fcfont) {
158 if test -e "$2"; then
159 arg="$2"
160 else
161 arg="--waterfall $cachedir/fonts/text"
162 maketext
164 conv='pango-view --font="$1" -q $arg -o $casp'
165 caspsuf=.pdf
166 md5of=/dev/null
167 filt=
168 force=1
170 image) conv='convert - pdf:"$casp"';;
171 dvi) {
172 test -n "$dc" && die "cannot convert compressed '$mime'"
173 unset filt
174 conv='dvipdf "$1" "$casp"'
176 *) die "unhandled filter type: '$type'";;
177 esac
179 hash=$(md5sum "$md5of") || die "$hash"
180 casp=$cachedir/${hash%% *}$caspsuf
181 (test -n "$force" -o ! -e "$casp") && eval "$filt" "$conv"
183 exec llpp -origin "$@" "$casp"