cache the checksums of the files
[xonotic/xonotic.git] / misc / tools / cached-converter.sh
1 #!/bin/sh
2
3 set -e
4
5 : ${CACHEDIR:=$HOME/.xonotic-cached-converter}
6 : ${do_jpeg:=true}
7 : ${jpeg_qual_rgb:=95}
8 : ${jpeg_qual_a:=99}
9 : ${do_dds:=true}
10 : ${dds_tool:=compressonator-dxtc}
11 : ${do_ogg:=false}
12 : ${ogg_qual:=1}
13 : ${del_src:=false}
14
15 me=$0
16 case "$me" in
17         */*)
18                 meprefix=${me%/*}/
19                 ;;
20         *)
21                 meprefix=
22                 ;;
23 esac
24
25 tmpdir=`mktemp -d -t cached-converter.XXXXXX`
26 trap 'exit 1' INT
27 trap 'rm -rf "$tmpdir"' EXIT
28
29 lastinfiles=
30 lastinfileshash=
31 cached()
32 {
33         flag=$1; shift
34         method=$1; shift
35         infile1=$1; shift
36         infile2=$1; shift
37         outfile1=$1; shift
38         outfile2=$1; shift
39         if ! $flag; then
40                 return 0
41         fi
42         if [ x"$infile1" = x"$outfile1" ]; then
43                 keep=true
44         fi
45         options=`echo "$*" | git hash-object --stdin`
46         if [ x"$infile1/../$infile2" = x"$lastinfiles" ]; then
47                 sum=$lastinfileshash
48         else
49                 sum=`git hash-object "$infile1"`
50                 if [ -n "$infile2" ]; then
51                         sum=$sum`git hash-object "$infile2"`
52                 fi
53                 lastinfileshash=$sum
54         fi
55         mkdir -p "$CACHEDIR/$method-$options"
56         name1="$CACHEDIR/$method-$options/$sum-1.${outfile1##*.}"
57         [ -z "$outfile2" ] || name2="$CACHEDIR/$method-$options/$sum-2.${outfile2##*.}"
58         tempfile1="${name1%/*}/new-${name1##*/}"
59         [ -z "$outfile2" ] || tempfile2="${name2%/*}/new-${name2##*/}"
60         if [ -f "$name1" ] && { [ -z "$outfile2" ] || [ -f "$name2" ]; }; then
61                 case "$outfile1" in */*) mkdir -p "${outfile1%/*}"; esac && { ln "$name1" "$outfile1" 2>/dev/null || cp "$name1" "$outfile1"; }
62                 [ -z "$outfile2" ] || { case "$outfile2" in */*) mkdir -p "${outfile2%/*}"; esac && { ln "$name2" "$outfile2" 2>/dev/null || cp "$name2" "$outfile2"; }; }
63                 conv=true
64         elif "$method" "$infile1" "$infile2" "$tempfile1" "$tempfile2" "$@"; then
65                 mv "$tempfile1" "$name1"
66                 [ -z "$outfile2" ] || mv "$tempfile2" "$name2"
67                 case "$outfile1" in */*) mkdir -p "${outfile1%/*}"; esac && { ln "$name1" "$outfile1" 2>/dev/null || cp "$name1" "$outfile1"; }
68                 [ -z "$outfile2" ] || { case "$outfile2" in */*) mkdir -p "${outfile2%/*}"; esac && { ln "$name2" "$outfile2" 2>/dev/null || cp "$name2" "$outfile2"; }; }
69                 conv=true
70         else
71                 rm -f "$tempfile1"
72                 rm -f "$tempfile2"
73                 exit 1
74         fi
75 }
76
77 reduce_jpeg2_dds()
78 {
79         i=$1; shift
80         ia=$1; shift
81         o=$1; shift; shift 
82         convert "$i" "$ia" -compose CopyOpacity -composite "$tmpdir/x.tga" && \
83         "$meprefix"compress-texture "$dds_tool" dxt5 "$tmpdir/x.tga" "$o" $1
84 }
85
86 reduce_jpeg2_jpeg2()
87 {
88         i=$1; shift
89         ia=$1; shift
90         o=$1; shift
91         oa=$1; shift
92         cp "$i" "$o" && jpegoptim --strip-all -m"$1" "$o" && \
93         cp "$ia" "$oa" && jpegoptim --strip-all -m"$2" "$oa"
94 }
95
96 reduce_jpeg_jpeg()
97 {
98         i=$1; shift; shift
99         o=$1; shift; shift
100         cp "$i" "$o" && jpegoptim --strip-all -m"$1" "$o"
101 }
102
103 reduce_ogg()
104 {
105         i=$1; shift; shift
106         o=$1; shift; shift
107         oggdec -o "$tmpdir/x.wav" "$i" && \
108         oggenc -q"$1" -o "$o" "$tmpdir/x.wav"
109 }
110
111 reduce_rgba_dds()
112 {
113         i=$1; shift; shift
114         o=$1; shift; shift
115         convert "$i" "$tmpdir/x.tga" && \
116         "$meprefix"compress-texture "$dds_tool" dxt5 "$tmpdir/x.tga" "$o" $1
117 }
118
119 reduce_rgba_jpeg2()
120 {
121         i=$1; shift; shift
122         o=$1; shift
123         oa=$1; shift
124         convert "$i" -alpha extract -quality 100 "$o" && \
125         convert "$i" -alpha off     -quality 100 "$oa" && \
126         jpegoptim --strip-all -m"$1" "$o" && \
127         jpegoptim --strip-all -m"$2" "$oa"
128 }
129
130 reduce_rgb_dds()
131 {
132         i=$1; shift; shift
133         o=$1; shift; shift
134         convert "$i" "$tmpdir/x.tga" && \
135         "$meprefix"compress-texture "$dds_tool" dxt1 "$tmpdir/x.tga" "$o" $1
136 }
137
138 reduce_rgb_jpeg()
139 {
140         i=$1; shift; shift
141         o=$1; shift; shift
142         convert "$i" "$o" && \
143         jpegoptim --strip-all -m"$1" "$o"
144 }
145
146 has_alpha()
147 {
148         i=$1; shift; shift
149         o=$1; shift; shift
150         if convert "$F" -depth 16 RGBA:- | perl -e 'while(read STDIN, $_, 8) { substr($_, 6, 2) eq "\xFF\xFF" or exit 1; } exit 0;'; then
151                 # no alpha
152                 : > "$o"
153         else
154                 # has alpha
155                 echo yes > "$o"
156         fi
157 }
158
159 for F in "$@"; do
160         echo >&2 "Handling $F..."
161         conv=false
162         keep=false
163         case "$F" in
164                 *_alpha.jpg)
165                         # handle in *.jpg case
166
167                         # they always got converted, I assume
168                         conv=true
169                         keep=$do_jpeg
170                         ;;
171                 *.jpg)
172                         if [ -f "${F%.jpg}_alpha.jpg" ]; then
173                                 cached "$do_dds"  reduce_jpeg2_dds   "$F" "${F%.*}_alpha.jpg" "dds/${F%.*}.dds" ""                  "$dds_flags"
174                                 cached "$do_jpeg" reduce_jpeg2_jpeg2 "$F" "${F%.*}_alpha.jpg" "$F"              "${F%.*}_alpha.jpg" "$jpeg_qual_rgb" "$jpeg_qual_a"
175                         else                                   
176                                 cached "$do_dds"  reduce_rgb_dds     "$F" ""                  "dds/${F%.*}.dds" ""                  "$dds_flags"
177                                 cached "$do_jpeg" reduce_jpeg_jpeg   "$F" ""                  "$F"              ""                  "$jpeg_qual_rgb"
178                         fi
179                         ;;
180                 *.png|*.tga)
181                         cached true has_alpha "$F" "" "$F.hasalpha" ""
182                         conv=false
183                         if [ -s "$F.hasalpha" ]; then
184                                 cached "$do_dds"  reduce_rgba_dds    "$F" ""                  "dds/${F%.*}.dds" ""                  "$dds_flags"
185                                 cached "$do_jpeg" reduce_rgba_jpeg2  "$F" ""                  "${F%.*}.jpg"     "${F%.*}_alpha.jpg" "$jpeg_qual_rgb" "$jpeg_qual_a"
186                                 rm -f "$F" # TGA becomes useless after JPEGging
187                         else                                                             
188                                 cached "$do_dds"  reduce_rgb_dds     "$F" ""                  "dds/${F%.*}.dds" ""                  "$dds_flags"
189                                 cached "$do_jpeg" reduce_rgb_jpeg    "$F" ""                  "${F%.*}.jpg"     ""                  "$jpeg_qual_rgb"
190                                 rm -f "$F" # TGA becomes useless after JPEGging
191                         fi
192                         rm -f "$F.hasalpha"
193                         ;;
194                 *.ogg)
195                         cached "$do_ogg" reduce_ogg "$F" "" "$F" "" "$ogg_qual"
196                         ;;
197         esac
198         if $del_src; then
199                 if $conv; then
200                         if ! $keep; then
201                                 rm -f "$F"
202                         fi
203                 fi
204         fi
205 done