fix another wrong chdir
[xonotic/xonotic.git] / misc / tools / xonotic-map-compiler-autobuild
1 #!/bin/sh
2
3 set -e
4
5 bspdir="$PWD/data"
6 url_http=http://beta.xonotic.org/autobuild-bsp/
7 url_ssh=xonotic-beta:autobuild-bsp/
8 build_cachedir="$HOME/xonotic-map-compiler.cache/"
9 screenshot_cachedir="$HOME/xonotic-map-screenshot.cache/"
10 build_override="-bsp_timeout 900 -vis_timeout 3600 -light_timeout 14400 -minimap_timeout 900 -scale_timeout 900"
11 screenshot_override="9 600 +g_ctf 1"
12
13 getthemap_fail=false
14
15 allmaps()
16 {
17         commithash=$1
18         shift
19         for F in `git show "${commithash}:maps/"`; do
20                 case "$F" in
21                         *.map.options)
22                                 ;;
23                         *)
24                                 continue
25                                 ;;
26                 esac
27                 M=${F%.map.options}
28                 blobhash=`git rev-parse --revs-only "${commithash}:maps/$M.map.options" || true`-`git rev-parse --revs-only "${commithash}:maps/$M.map" || true`
29                 case "$blobhash" in
30                         -*)
31                                 ;;
32                         *-)
33                                 ;;
34                         *)
35                                 "$@" "$M" "$blobhash" "$commithash"
36                                 ;;
37                 esac
38         done
39 }
40
41 pre2spam()
42 {
43         map=$1
44         url=$2
45         branch=$3
46         hash=$4
47
48         branch=${branch##refs/heads/}
49         branch=${branch##refs/remotes/}
50         branch=${branch##origin/}
51         hash=`echo "$hash" | cut -c 1-7`
52
53         echo "[$branch $hash] starting map compile of $map"
54 }
55
56 ss2spam()
57 {
58         map=$1
59         url=$2
60         branch=$3
61         hash=$4
62         status=$5
63
64         branch=${branch##refs/heads/}
65         branch=${branch##refs/remotes/}
66         branch=${branch##origin/}
67         hash=`echo "$hash" | cut -c 1-7`
68
69         s_error=
70
71         if [ "$status" -ne 0 ]; then
72                 s_error="exited with status $status"
73         fi
74
75         if [ -n "$s_error" ]; then
76                 echo "[$branch $hash] \ 34screenshots of $map could not be made: $s_error"
77         else
78                 echo "[$branch $hash] screenshots of $map are available on $url"
79         fi
80 }
81
82 log2spam()
83 {
84         map=$1
85         url=$2
86         branch=$3
87         hash=$4
88         status=$5
89         time=$6
90
91         hash=`echo "$hash" | cut -c 1-7`
92
93         branch=${branch##refs/heads/}
94         branch=${branch##refs/remotes/}
95         branch=${branch##origin/}
96
97         s_samplesize=
98         s_failshaders=
99         s_leaked=
100         s_error=
101
102         if [ "$status" -ne 0 ]; then
103                 s_error="exited with status $status"
104         fi
105         while IFS= read -r L; do
106                 case "$L" in
107                         WARNING:\ surface\ at\ *\ too\ large\ for\ desired\ samplesize*)
108                                 s=${L##* }
109                                 if [ -z "$s_samplesize" ] || [ "$s" -gt "$s_samplesize" ]; then
110                                         s_samplesize=$s
111                                 fi
112                                 ;;
113                         WARNING:\ Couldn\'t\ find\ image\ for\ shader\ textures/NULL)
114                                 # radiant stupid
115                                 ;;
116                         WARNING:\ Couldn\'t\ find\ image\ for\ shader\ *)
117                                 s_failshaders="$s_failshaders ${L##* }"
118                                 ;;
119                         '******* leaked *******')
120                                 s_leaked=1
121                                 ;;
122                         '************ ERROR ************')
123                                 IFS= read -r s_error
124                                 ;;
125                 esac
126         done
127         s_failshaders=`echo "$s_failshaders" | sed "s, textures/, ,g"`
128         s_failshaders=${s_failshaders# }
129         if [ -n "$s_error" ]; then
130                 echo -n "[$branch $hash] \ 34failed"
131         else
132                 echo -n "[$branch $hash] finished"
133         fi
134         echo -n " map compile of $map ($url): $time sec"
135         if [ -n "$s_samplesize" ]; then
136                 echo -n ", FIX samplesize >= $s_samplesize"
137         fi
138         if [ -n "$s_failshaders" ]; then
139                 if [ -n "`echo "$s_failshaders" | cut -d ' ' -f 4-`" ]; then
140                         s_failshaders="`echo "$s_failshaders" | cut -d ' ' -f 1-3`..."
141                 fi
142                 echo -n ", FIX shaders $s_failshaders"
143         fi
144         if [ -n "$s_leaked" ]; then
145                 echo -n ", FIX LEAK"
146         fi
147         if [ -n "$s_error" ]; then
148                 s_error=`echo "$s_error" | sed "s,$PWD/\?,,g"`
149                 echo -n ", ERROR: $s_error"
150         fi
151         echo
152 }
153
154 buildthemap()
155 {
156         REFNAME=$1
157         url=$2
158         M=$3
159         blobhash=$4
160         HASH=$5
161         if HEAD "$url$M-$blobhash.pk3"; then
162                 continue
163         fi
164
165         git reset --hard
166         git clean -xfd
167         git checkout -f "$HASH"
168
169         if [ -n "$IRCSPAM" ]; then
170                 pre2spam "$M" "$url$M-$blobhash.pk3" "$REFNAME" "$HASH" | $IRCSPAM
171         fi
172         t0=`date +%s`
173         (
174                 cd maps
175                 ../../../misc/tools/xonotic-map-compiler "$M" `grep ^- "$M.map.options" | cut -d '#' -f 1` $build_override > "$M.log"
176         )
177         t1=`date +%s`
178         dt=$(($t1 - $t0))
179         status=$?
180         if [ -n "$IRCSPAM" ]; then
181                 cat "maps/$M.log" | log2spam "$M" "$url$M-$blobhash.pk3" "$REFNAME" "$HASH" "$status" "$dt" > "maps/$M.irc"
182         fi
183         zip -9r "$M-$blobhash.pk3" "maps/$M.bsp" "maps/$M.log" "maps/$M.irc" "maps/$M/" "maps/$M.lin" "gfx/${M}_mini.tga"
184         ln -snf "../$M-$blobhash.pk3" "$M.pk3" # from ALL branches, so beware!
185         cp "$M-$blobhash.pk3" "$M-full-$blobhash.pk3"
186         zip -9r "$M-full-$blobhash.pk3" `git diff --name-only --diff-filter=ACMRTUXB master...HEAD` || true
187         ln -snf "../$M-full-$blobhash.pk3" "$M-full.pk3" # from ALL branches, so beware!
188         rsync -vaSHP "$M-$blobhash.pk3" "$M-full-$blobhash.pk3" "$url_ssh"
189         rsync -vaSHP "$M.pk3" "$M-full.pk3" "$url_ssh""latest/"
190         if [ -n "$IRCSPAM" ]; then
191                 $IRCSPAM < "maps/$M.irc"
192         fi
193 }
194
195 screenshotthemap()
196 {
197         REFNAME=$1
198         url=$2
199         M=$3
200         blobhash=$4
201         HASH=$5
202         if HEAD "$url$M-$blobhash/"; then
203                 continue
204         fi
205
206         git reset --hard
207         git clean -xfd
208         git checkout -f "$HASH"
209
210         rm -rf ~/.xonotic
211         (
212                 cd ../..
213                 if [ -n "$DISPLAY" ]; then
214                         misc/tools/xonotic-map-screenshot "$M" $screenshot_override +"scr_screenshot_name \"$M-\""
215                 else
216                         startx "$PWD/misc/tools/xonotic-map-screenshot" "$M" $screenshot_override +"scr_screenshot_name \"$M-\"" -- :8
217                 fi
218         )
219         if ! mv ~/.xonotic/data/screenshots "$M-$blobhash"; then
220                 if [ -n "$IRCSPAM" ]; then
221                         #ss2spam "$M" "$url$M-$blobhash/" "$REFNAME" "$HASH" 1 > "maps/$M.ircss"
222                         ss2spam "$M" "$url?d" "$REFNAME" "$HASH" 1 > "maps/$M.ircss"
223                 fi
224                 return 1
225         fi
226         if [ -n "$IRCSPAM" ]; then
227                 #ss2spam "$M" "$url$M-$blobhash/" "$REFNAME" "$HASH" 0 > "maps/$M.ircss"
228                 ss2spam "$M" "$url?d" "$REFNAME" "$HASH" 0 > "maps/$M.ircss"
229         fi
230         chmod 1777 "$M-$blobhash"
231         ln -snf "../$M-$blobhash" "$M" # from ALL branches, so beware!
232         rsync -vaSHP "$M-$blobhash" "$url_ssh"
233         rsync -vaSHP "$M" "$url_ssh""latest/"
234         if [ -n "$IRCSPAM" ]; then
235                 $IRCSPAM < "maps/$M.ircss"
236         fi
237 }
238
239 getthemap()
240 {
241         url=$1
242         bspdir_old=$2
243         bspdir=$3
244         M=$4
245         blobhash=$5
246         HASH=$6
247         if mv "$bspdir_old/$M-$blobhash.pk3" "$bspdir/$M-$blobhash.pk3"; then
248                 continue
249         fi
250         if ! wget -O "$bspdir/$M-$blobhash.pk3" "$url$M-$blobhash.pk3"; then
251                 if ! curl -o "$bspdir/$M-$blobhash.pk3" "$url$M-$blobhash.pk3"; then
252                         rm -f "$bspdir/$M-$blobhash.pk3"
253                         echo "WARNING: could not download $url$M-$blobhash.pk3, maybe not ready yet"
254                         getthemap_fail=true
255                         return 0
256                 fi
257         fi
258         if ! unzip -l "$bspdir/$M-$blobhash.pk3"; then
259                 rm -f "$bspdir/$M-$blobhash.pk3"
260                 echo "WARNING: could not download $url$M-$blobhash.pk3, invalid zip file"
261                 getthemap_fail=true
262                 return 0
263         fi
264 }
265
266 indexthemap()
267 {
268         REFNAME=$1
269         M=$2
270         blobhash=$3
271         HASH=$4
272         echo "$M $blobhash $HASH $REFNAME"
273 }
274
275 rundownload()
276 {
277         mkdir -p "$bspdir" "$bspdir.old"
278         for b in "$bspdir"/*-????????????????????????????????????????-????????????????????????????????????????.pk3; do
279                 if [ -e "$b" ]; then
280                         mv "$b" "$bspdir.old"/
281                 fi
282         done
283         cd data/xonotic-maps.pk3dir
284         allmaps "HEAD" getthemap "$url_http" "$bspdir.old" "$bspdir"
285         cd ../..
286 }
287
288 branches()
289 {
290         git for-each-ref 'refs/remotes' | grep -vE '    refs/remotes/([^/]*/HEAD|.*/archived/.*)$'
291 }
292
293 runmakeindex()
294 {
295         cd data/xonotic-maps.pk3dir
296         branches | while read -r HASH TYPE REFNAME; do
297                 allmaps "$HASH" indexthemap "$REFNAME"
298         done
299         cd ../..
300 }
301
302 case "$1" in
303         build)
304                 cd data/xonotic-maps.pk3dir
305                 branches | while read -r HASH TYPE REFNAME; do
306                         if [ -f "$build_cachedir/$HASH" ]; then
307                                 continue
308                         fi
309                         allmaps "$HASH" buildthemap "$REFNAME" "$url_http"
310                         touch "$build_cachedir/$HASH"
311                 done
312                 git checkout -f master
313                 ;;
314         screenshot)
315                 runmakeindex > "branches.idx.new"
316                 rsync -vaSHP "branches.idx.new" "$url_ssh""branches.idx.new"
317                 rm -f "branches.idx.new"
318                 cd data/xonotic-maps.pk3dir
319                 branches | while read -r HASH TYPE REFNAME; do
320                         if [ -f "$screenshot_cachedir/$HASH" ]; then
321                                 continue
322                         fi
323
324                         rundownload
325                         if $getthemap_fail; then
326                                 continue
327                         fi
328
329                         cd data/xonotic-maps.pk3dir
330                         allmaps "$HASH" screenshotthemap "$REFNAME" "$url_http"
331                         cd ../..
332
333                         touch "$screenshot_cachedir/$HASH"
334                 done
335                 git checkout -f master
336                 ;;
337         makeindex)
338                 runmakeindex
339                 ;;
340         download)
341                 cd data/xonotic-maps.pk3dir
342                 rundownload
343                 cd ../..
344                 echo "List of maps that got deleted (if any) and currently are in $bspdir.old:"
345                 ls -l "$bspdir.old" || true
346                 ;;
347         download-latest)
348                 mkdir -p "$bspdir"
349                 cd "$bspdir"
350                 rm -f *-????????????????????????????????????????-????????????????????????????????????????.pk3
351                 wget -r -l1 -A "*.pk3" -N --no-parent --no-directories "$url_http""latest"
352                 ;;
353         log2spam-test)
354                 log2spam "mapname" "http://mapurl" "branch" "commit" "0"
355                 ;;
356 esac