Build system improvements
[llpp.git] / llppac
blob94ccc3b359009c0bfaa23abd2dcdab58eca0e4a8
1 #!/bin/sh
2 set -e
4 CACHEDIR=$HOME/.cache/llpp
5 caspsuf=
6 type=
8 die() {
9 echo "$1" >&2
10 exit 1
13 executable_p() {
14 command -v "$1" >/dev/null 2>&1
17 maketext() {
18 cat >"$CACHEDIR/fonts/text" <<EOF
19 Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
20 eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
21 minim veniam, quis nostrud exercitation ullamco laboris nisi ut
22 aliquip ex ea commodo consequat. Duis aute irure dolor in
23 reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
24 pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
25 culpa qui officia deserunt mollit anim id est laborum.
26 abcdefghizklmnopqrstwxyz
27 ABCDEFGHIZKLMNOPQRSTWXYZ
28 \`1234567890-=\\
29 ~!@#\$%^&*()_+|
30 [];',./
31 {}:"<>?
33 EOF
36 md5of="$1"
37 trap 'test -n "$casp" && rm -f "$casp"' 0
39 test -d "$CACHEDIR" || die "cache directory '$CACHEDIR' does not exist"
40 while getopts m:t:f opt; do
41 case $opt in
42 m) mime=$OPTARG;;
43 t) type=$OPTARG;;
44 f) force=1;;
45 ?) die "usage: $0 [-m mime/type] [-t filter] [-f] path";;
46 esac
47 done
48 shift $(($OPTIND - 1))
49 test -z "$1" && die "usage $0: path"
51 test -z "$type" && {
52 ft=$(file -L --mime-type -b "$1") || die "$ft"
55 case $ft in
56 application/x-gzip | application/x-compress) dc=zcat;;
57 application/x-xz) dc=xzcat;;
58 application/x-bzip2) dc=bzcat;;
59 *) unset dc || true;;
60 esac
62 filt='"${dc-cat}" "$1" |'
64 if test -z "$type"; then
65 test -z "$dc" || ft=$(file -L --mime-type -bz "$1") || die "$ft"
67 mime=${mime:-$ft}
68 case $mime in
69 application/postscript) type=ps;;
70 application/pdf) type=pdf;;
71 image/vnd.djvu) type=djvu;;
72 text/html) type=html;;
73 application/msword) type=word;;
74 application/vnd.openxmlformats-officedocument.* \
75 | application/vnd.ms-powerpoint \
76 | application/vnd.ms-excel \
77 | application/vnd.oasis.opendocument.*) type=uno;;
78 image/svg+xml) type=svg;;
79 image/png | image/jpeg) test -n "$dc" && type="image" || type="image2";;
80 image/*) type=image;;
81 application/x-dvi) type=dvi;;
82 application/x-font-ttf | application/vnd.ms-opentype) type=font;;
83 *) die "unhandled file type: '$mime'";;
84 esac
87 case $type in
88 ps) conv='ps2pdf - "$casp"';;
89 image2|pdf) test -z "$dc" && exec llpp "$@" || conv='cat >"$casp"';;
90 djvu) conv='djvups - | ps2pdf - "$casp"';;
91 html) {
92 test -n "$dc" && die "can not convert compressed '$mime'"
93 unset filt
94 conv='prince "$1" -o "$casp"'
95 };;
96 word) {
97 if executable_p unoconv && test -z "$dc"; then
98 unset filt
99 conv='unoconv -o "$casp" "$1"'
100 else
101 conv='antiword -m 8859-1.txt -a a4 - >"$casp"'
104 uno) {
105 test -n "$dc" && die "can not convert compressed '$mime'"
106 unset filt
107 conv='unoconv -o "$casp" "$1"'
109 svg) {
110 executable_p rsvg-convert \
111 && conv='rsvg-convert -f pdf -o "$casp"' \
112 || conv='inkscape -z -A "$casp" -'
114 font) {
115 if test -e "$2"; then
116 arg="$2"
117 else
118 arg="--waterfall $CACHEDIR/fonts/text"
119 maketext
121 # https://mail.gnome.org/archives/gtk-i18n-list/2010-June/msg00000.html
122 eval $filt cat >$CACHEDIR/fonts/font
123 cat >$CACHEDIR/fonts/fonts.conf <<EOF
124 <?xml version="1.0"?>
125 <!DOCTYPE fontconfig SYSTEM "fonts.dtd">
126 <fontconfig>
127 <dir>$CACHEDIR/fonts/</dir>
128 <cachedir>$CACHEDIR/fonts/</cachedir>
129 <config></config>
130 </fontconfig>
132 unset filt
133 conv='FONTCONFIG_PATH=$CACHEDIR/fonts/ pango-view -q $arg -o $casp'
134 caspsuf=.pdf
136 fcfont) {
137 if test -e "$2"; then
138 arg="$2"
139 else
140 arg="--waterfall $CACHEDIR/fonts/text"
141 maketext
143 conv='pango-view --font="$1" -q $arg -o $casp'
144 caspsuf=.pdf
145 md5of=/dev/null
146 filt=
147 force=1
149 image) conv='convert - pdf:"$casp"';;
150 dvi) {
151 test -n "$dc" && die "can not convert compressed '$mime'"
152 unset filt
153 conv='dvipdf "$1" "$casp"'
155 *) die "unhandled filter type: '$type'";;
156 esac
158 hash=$(md5sum "$md5of") || die "$hash"
159 casp=$CACHEDIR/${hash%% *}$caspsuf
160 (test -n "$force" -o ! -e "$casp") && eval "$filt" "$conv"
162 exec llpp -origin "$@" "$casp"