]> git.pld-linux.org Git - packages/lighttpd.git/blob - lighttpd-mime.types.sh
no dist-xz for ac
[packages/lighttpd.git] / lighttpd-mime.types.sh
1 #!/bin/sh
2 # Parse /etc/mime.types into lighttpd config format.
3 # Copyright (c) 2005 Elan Ruusamäe <glen@pld-linux.org>
4
5 mimetypes="$1"
6
7 # build mime.types from system mime.types
8 # get ones with extension
9 awk '!/^#/ && $2 { print } ' $mimetypes | \
10 # lay out mime types with multiple extension as separate lines \
11 awk '{for (a=2; a <= NF; a++) {printf("%s\t%s\n", $1, $a)}}' | \
12 # sort it \
13 LC_ALL=C sort -u > mime.types
14
15 # build lighttpd.conf fragment
16 awk '{
17         ext = $2;
18         type = $1;
19         charset = "";
20         # add charset for "text/*" types
21         if (type ~ "text/") {
22                 type = "\"" type "\" + mimetype_textcharsetheader"
23         } else {
24                 type = "\"" type "\""
25         }
26
27         printf("\t\".%s\"%s=> %s,\n", ext, (length(ext) > 4 ? "\t" : "\t\t"), type);
28 }' \
29         < mime.types | LC_ALL=C sort -r > mime.types.conf
30
31 # sanity check. there can't be more than one mime type mapping for same extension
32 dup=$(awk -F'"' '{print $2}' mime.types.conf | sort | uniq -c | grep -v '1' | awk '{print $NF}')
33 if [ "$dup" ]; then
34         echo >&2 Found $(echo "$dup" | wc -w) extensions which have non-unique mime-type mapping:
35         echo "$dup" | sed -e 's,^,  ,' >&2
36         exit 1
37 fi
38
39 mv -f mime.types.conf mime.types.conf.tmp
40
41 # header
42 cat >> mime.types.conf <<EOF
43 # charset used for "text/*" mimetypes
44 # Apache's AddCharset equivalent. Leave empty to add no charset.
45 # AddCharset "utf-8" would be "; charset=\"utf-8\""
46 # See this post about what it affects:
47 # http://lists.pld-linux.org/mailman/pipermail/pld-devel-en/2012-February/022499.html
48 mimetype_textcharsetheader = ""
49 #mimetype_textcharsetheader = "; charset=\"utf-8\""
50
51 # mimetype mapping
52 mimetype.assign = (
53 EOF
54
55 # save our hard work
56 cat mime.types.conf.tmp >> mime.types.conf
57
58 # footer
59 cat >> mime.types.conf <<EOF
60 )
61 EOF
62
63 rm -f mime.types.conf.tmp
This page took 0.05203 seconds and 3 git commands to generate.