./all run: use pulseaudio if present
[xonotic/xonotic.git] / all
1 #!/bin/sh
2 # vim: filetype=zsh
3
4 set -e
5
6 d00=`pwd`
7 while ! [ -f ./all ]; do
8         if [ x"`pwd`" = x"/" ]; then
9                 echo "Cannot find myself."
10                 echo "Please run this script with the working directory inside a Xonotic checkout."
11                 exit 1
12         fi
13         cd ..
14 done
15 d0=`pwd`
16 SELF="$d0/all"
17
18 # If we are on WINDOWS:
19 case "$0" in
20         all|*/all)
21                 case "`uname`" in
22                         MINGW*|Win*)
23                                 # Windows hates users. So this script has to copy itself elsewhere first...
24                                 tname=
25                                 cp "$SELF" ../all.xonotic.sh
26                                 export WE_HATE_OUR_USERS=1
27                                 exec ../all.xonotic.sh "$@"
28                                 ;;
29                 esac
30                 ;;
31 esac
32
33 msg()
34 {
35         echo "\e[1m$*\e[m"
36 }
37
38 checksum()
39 {
40         if [ -x /usr/bin/md5sum ]; then
41                 /usr/bin/md5sum "$@"
42         elif [ -x /bin/md5sum ]; then
43                 /bin/md5sum "$@"
44         elif [ -x /usr/bin/cksum ]; then
45                 /usr/bin/cksum "$@"
46         else
47                 echo "NOCHECKSUM"
48         fi
49 }
50
51 self=`checksum "$SELF"`
52 checkself()
53 {
54         self_new=`checksum "$SELF"`
55         if [ x"$self" != x"$self_new" ]; then
56                 msg "./all has changed."
57                 if [ -z "$XONOTIC_FORBID_RERUN_ALL" ]; then
58                         msg "Rerunning the requested operation to make sure."
59                         export XONOTIC_FORBID_RERUN_ALL=1
60                         exec "$SELF" "$@"
61                 else
62                         msg "Please try $SELF update, and then retry your requested operation."
63                         exit 1
64                 fi
65         fi
66         return 0
67 }
68
69 verbose()
70 {
71         msg "+ $*"
72         "$@"
73 }
74
75 visible_repo_name()
76 {
77         case "$1" in
78                 .)
79                         echo "the root directory"
80                         ;;
81                 *)
82                         echo "\"$1\""
83                         ;;
84         esac
85 }
86
87 check_mergeconflict()
88 {
89         if git ls-files -u | grep ' 1   '; then
90                 echo
91                 echo "MERGE CONFLICT."
92                 echo "change into the \"$1\" project directory, and then:"
93                 echo "- edit the files mentioned above with your favorite editor,"
94                 echo "  and fix the conflicts (marked with <<<<<<< blocks)"
95                 echo "- for binary files, you can select the files using"
96                 echo "  git checkout --ours or git checkout --theirs"
97                 echo "- when done with a file, 'git add' the file"
98                 echo "- when done, 'git commit'"
99                 echo
100                 exit 1
101         fi
102 }
103
104 yesno()
105 {
106         yesno=
107         while [ x"$yesno" != x"y" -a x"$yesno" != x"n" ]; do
108                 echo "$1"
109                 IFS= read -r yesno
110         done
111         [ x"$yesno" = x"y" ]
112 }
113
114 enter()
115 {
116         $2 cd "$1"
117         check_mergeconflict "$1"
118 }
119
120 repos_urls="
121 .                             |                                                   | master
122 data/xonotic-data.pk3dir      |                                                   | master
123 data/xonotic-maps.pk3dir      |                                                   | master
124 data/xonotic-music.pk3dir     |                                                   | master
125 data/xonotic-nexcompat.pk3dir |                                                   | master
126 mediasource                   |                                                   | master
127 darkplaces                    |                                                   | div0-stable
128 fteqcc                        | git://github.com/Blub/qclib.git                   | master
129 div0-gittools                 | git://git.icculus.org/divverent/div0-gittools.git | master
130 netradiant                    |                                                   | master
131 "
132 # todo: in darkplaces, change repobranch to div0-stable
133
134 repos=`echo "$repos_urls" | grep . | cut -d '|' -f 1 | tr -d ' '`
135
136 base=`git config remote.origin.url`
137 case "$base" in
138         */xonotic.git)
139                 base=${base%xonotic.git}
140                 ;;
141         *)
142                 echo "The main repo is not xonotic.git, what have you done?"
143                 exit 1
144                 ;;
145 esac
146
147 repourl()
148 {
149         t=`echo "$repos_urls" | grep "^$1 " | cut -d '|' -f 2 | tr -d ' '`
150         if [ -n "$t" ]; then
151                 case "$t" in
152                         *://*)
153                                 echo "$t"
154                                 ;;
155                         *)
156                                 echo "$base$t"
157                                 ;;
158                 esac
159         else
160                 if [ x"$1" = x"." ]; then
161                         echo "$base""xonotic.git"
162                 else
163                         echo "$base${1##*/}.git"
164                 fi
165         fi
166 }
167
168 repobranch()
169 {
170         t=`echo "$repos_urls" | grep "^$1 " | cut -d '|' -f 3 | tr -d ' '`
171         if [ -n "$t" ]; then
172                 echo "$t"
173         else
174                 echo "master"
175         fi
176 }
177
178 repos=`for d in $repos; do
179         p="${d%dir}"
180         if [ x"$p" = x"$d" ] || [ -d "$d" ] || ! [ -f "$p" ]; then
181                 echo "$d"
182         fi
183 done`
184
185 if [ "$#" = 0 ]; then
186         set -- help
187 fi
188 cmd=$1
189 shift
190
191 case "$cmd" in
192         update|pull)
193                 allow_pull=true
194                 if [ x"$1" = x"-N" ]; then
195                         allow_pull=false
196                 fi
197                 for d in $repos; do
198                         url=`repourl "$d"`
199                         branch=`repobranch "$d"`
200                         if [ -d "$d0/$d" ]; then
201                                 if $allow_pull; then
202                                         enter "$d0/$d" verbose
203                                         verbose git config remote.origin.url "$url"
204                                         verbose git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
205                                                 # TODO remove this line later
206
207                                         verbose git config core.autocrlf false
208                                         verbose git config core.safecrlf false # we don't NEED that...
209
210                                         r=`git symbolic-ref HEAD`
211                                         r=${r#refs/heads/}
212                                         if git config branch.$r.remote >/dev/null 2>&1; then
213                                                 if ! verbose git pull; then
214                                                         check_mergeconflict "$d"
215                                                         echo "Pulling failed. Press ENTER to continue, or Ctrl-C to abort."
216                                                         read -r DUMMY
217                                                 fi
218                                         fi
219
220                                         cd "$d00"
221                                         checkself "$cmd" "$@"
222                                         cd "$d0/$d"
223                                         verbose git remote prune origin
224                                         cd "$d0"
225                                 fi
226                         else
227                                 verbose git clone "$url" "$d0/$d"
228                                 enter "$d0/$d" verbose
229                                 verbose git checkout "$branch"
230                                 cd "$d0"
231                         fi
232                 done
233                 ;;
234         checkout|switch)
235                 remote=$1
236                 branch=$2
237                 if [ -z "$branch" ]; then
238                         branch=$remote
239                         remote=origin
240                 fi
241                 exists=false
242                 for d in $repos; do
243                         enter "$d0/$d" verbose
244                         if git rev-parse "refs/heads/$branch" >/dev/null 2>&1; then
245                                 exists=true
246                                 verbose git checkout "$branch"
247                         elif git rev-parse "refs/remotes/$remote/$branch" >/dev/null 2>&1; then
248                                 exists=true
249                                 verbose git checkout --track -b "$branch" "$remote/$branch"
250                         else
251                                 verbose git checkout "`repobranch "$d"`"
252                         fi
253                         cd "$d00"
254                         checkself "$cmd" "$@"
255                         cd "$d0"
256                 done
257                 if ! $exists; then
258                         echo "The requested branch was not found in any repository."
259                 fi
260                 exec "$SELF" branch
261                 ;;
262         branch)
263                 remote=$1
264                 branch=$2
265                 srcbranch=$3
266                 if [ -z "$branch" ]; then
267                         branch=$remote
268                         remote=origin
269                 fi
270                 if [ -z "$branch" ]; then
271                         for d in $repos; do
272                                 enter "$d0/$d"
273                                 r=`git symbolic-ref HEAD`
274                                 r=${r#refs/heads/}
275                                 echo "$d is at $r"
276                                 cd "$d0"
277                         done
278                 else
279                         for d in $repos; do
280                                 dv=`visible_repo_name "$d"`
281                                 enter "$d0/$d" verbose
282                                 if git rev-parse "refs/heads/$branch" >/dev/null 2>&1; then
283                                         echo "Already having this branch in $dv."
284                                 else
285                                         if yesno "Branch in $dv?"; then
286                                                 if [ -n "$srcbranch" ]; then
287                                                         b=$srcbranch
288                                                 else
289                                                         b="`repobranch "$d"`"
290                                                 fi
291                                                 # TODO do this without pushing
292                                                 verbose git push "$remote" "$b":"$branch"
293                                                 verbose git checkout --track -b "$branch" "$remote/$branch"
294                                         fi
295                                 fi
296                                 cd "$d0"
297                         done
298                         "$SELF" branch
299                 fi
300                 ;;
301         branches)
302                 for d in $repos; do
303                         enter "$d0/$d"
304                         echo "In $d:"
305                         git branch -a -v -v | cut -c 3- | while read -r BRANCH REV UPSTREAM TEXT; do
306                                 case "$UPSTREAM" in
307                                         \[*)
308                                                 UPSTREAM=${UPSTREAM#\[}
309                                                 UPSTREAM=${UPSTREAM%\]}
310                                                 UPSTREAM=${UPSTREAM%:*}
311                                                 ;;
312                                         *)
313                                                 TEXT="$UPSTREAM $TEXT"
314                                                 UPSTREAM=
315                                                 ;;
316                                 esac
317                                 if [ x"$REV" = x"->" ]; then
318                                         continue
319                                 fi
320                                 BRANCH=${BRANCH#remotes/}
321                                 echo -n "  $BRANCH"
322                                 if [ -n "$UPSTREAM" ]; then
323                                         echo -n " (tracking $UPSTREAM)"
324                                 fi
325                                 #echo " $TEXT"
326                                 echo
327                         done
328                 done
329                 ;;
330         branches_short)
331                 for d in $repos; do
332                         cd "$d0/$d" # am in a pipe, shouldn't use enter
333                         git branch -a -v -v | cut -c 3- | sed "s,^,$d ,"
334                         cd "$d0"
335                 done | {
336                         branches_list=
337                         # branches_repos_*=
338                         while read -r d BRANCH REV UPSTREAM TEXT; do
339                                 case "$UPSTREAM" in
340                                         \[*)
341                                                 UPSTREAM=${UPSTREAM#\[}
342                                                 UPSTREAM=${UPSTREAM%\]}
343                                                 UPSTREAM=${UPSTREAM%:*}
344                                                 ;;
345                                         *)
346                                                 TEXT="$UPSTREAM $TEXT"
347                                                 UPSTREAM=
348                                                 ;;
349                                 esac
350                                 if [ x"$REV" = x"->" ]; then
351                                         continue
352                                 fi
353                                 BRANCH=${BRANCH#remotes/}
354                                 ID=`echo "$BRANCH" | tr -c "A-Za-z0-9." "_"`
355                                 branches_list="$branches_list $BRANCH" # TEH SORT MAKEZ IT UNIEQ
356                                 eval "r=\$branches_repos_$ID"
357                                 r="$r $d:$UPSTREAM"
358                                 eval "branches_repos_$ID=\$r"
359                         done
360                         echo -n "$branches_list" | xargs -n 1 echo | sort -u | while IFS= read -r BRANCH; do
361                                 ID=`echo "$BRANCH" | tr -c "A-Za-z0-9." "_"`
362                                 eval "r=\$branches_repos_$ID"
363                                 echo "$BRANCH: $r"
364                         done
365                 }
366                 ;;
367         merge)
368                 for d in $repos; do
369                         dv=`visible_repo_name "$d"`
370                         enter "$d0/$d" verbose
371                         r=`git symbolic-ref HEAD`
372                         r=${r#refs/heads/}
373                         if git log HEAD..origin/"`repobranch "$d"`" | grep .; then
374                                 # we have uncommitted changes
375                                 if yesno "Could merge from \"`repobranch "$d"`\" into \"$r\" in $dv. Do it?"; then
376                                         if ! verbose git merge origin/"`repobranch "$d"`"; then
377                                                 check_mergeconflict "$d"
378                                                 exit 1 # this should ALWAYS be fatal
379                                         fi
380                                 fi
381                         fi
382                         cd "$d0"
383                 done
384                 ;;
385         push|commit)
386                 submit=$1
387                 for d in $repos; do
388                         dv=`visible_repo_name "$d"`
389                         enter "$d0/$d" verbose
390                         r=`git symbolic-ref HEAD`
391                         r=${r#refs/heads/}
392                         if git diff HEAD | grep .; then
393                                 # we have uncommitted changes
394                                 if yesno "Uncommitted changes in \"$r\" in $dv. Commit?"; then
395                                         verbose git commit -a
396                                 fi
397                         fi
398                         rem=`git config "branch.$r.remote" || echo origin`
399                         if { git log "$rem/$r".."$r" || git log origin/"`repobranch "$d"`".."$r"; } | grep .; then
400                                 if yesno "Push \"$r\" in $dv?"; then
401                                         verbose git push "$rem" HEAD
402                                 fi
403                         fi
404                         if [ x"$submit" = x"-s" ]; then
405                                 case "$r" in
406                                         */*)
407                                                 verbose git push "$rem" HEAD:"${r%%/*}/finished/${r#*/}"
408                                                 ;;
409                                 esac
410                         fi
411                         cd "$d0"
412                 done
413                 ;;
414         compile)
415                 if [ -n "$WE_HATE_OUR_USERS" ]; then
416                         TARGETS="sv-debug cl-debug"
417                 else
418                         TARGETS="sv-debug cl-debug sdl-debug"
419                 fi
420                 case "$1" in
421                         -c)
422                                 clean=true
423                                 shift
424                                 ;;
425                         *)
426                                 clean=false
427                                 ;;
428                 esac
429                 case "$1" in
430                         sdl)
431                                 TARGETS="sdl-debug"
432                                 shift
433                                 ;;
434                         glx|agl|wgl)
435                                 TARGETS="cl-debug"
436                                 shift
437                                 ;;
438                         dedicated)
439                                 TARGETS="sv-debug"
440                                 shift
441                                 ;;
442                 esac
443                 if [ -z "$MAKEFLAGS" ]; then
444                         if [ -f /proc/cpuinfo ]; then
445                                 ncpus=$((`grep -c '^processor   :' /proc/cpuinfo`+0))
446                                 if [ $ncpus -gt 1 ]; then
447                                         MAKEFLAGS=-j$ncpus
448                                 fi
449                         fi
450                         case "`uname`" in
451                                 Linux|*BSD)
452                                         MAKEFLAGS="$MAKEFLAGS DP_LINK_TO_LIBJPEG=1"
453                                         ;;
454                         esac
455                         if [ -n "$WE_HATE_OUR_USERS" ]; then
456                                 MAKEFLAGS="$MAKEFLAGS DP_MAKE_TARGET=mingw"
457                         fi
458                 fi
459                 enter "$d0/fteqcc" verbose
460                 if $clean; then
461                         verbose make $MAKEFLAGS clean
462                 fi
463                 verbose make $MAKEFLAGS
464                 enter "$d0/data/xonotic-data.pk3dir" verbose
465                 if $clean; then
466                         verbose make $MAKEFLAGS clean
467                 fi
468                 verbose make FTEQCC="$d0/fteqcc/fteqcc.bin" "$@" $MAKEFLAGS
469                 enter "$d0/darkplaces" verbose
470                 if $clean; then
471                         verbose make $MAKEFLAGS clean
472                 fi
473                 for T in $TARGETS; do
474                         verbose make $MAKEFLAGS "$@" "$T"
475                 done
476                 ;;
477         run)
478                 if [ -n "$WE_HATE_OUR_USERS" ]; then
479                         client=
480                         export PATH="$d0/misc/buildfiles/w32:$PATH"
481                 elif [ x"`uname`" = x"Darwin" ]; then
482                         export DYLD_LIBRARY_PATH="$d0/misc/buildfiles/osx/Nexuiz.app/Contents/MacOS"
483                         client=-sdl
484                 else
485                         client=-sdl
486                 fi
487                 case "$1" in
488                         sdl|glx|agl|dedicated)
489                                 client=-$1
490                                 shift
491                                 ;;
492                         wgl)
493                                 client=
494                                 shift
495                                 ;;
496                 esac
497                 if ! [ -x "darkplaces/darkplaces$client" ]; then
498                         if [ -x "darkplaces/darkplaces$client.exe" ]; then
499                                 client=$client.exe
500                         else
501                                 echo "Client darkplaces/darkplaces$client not found, aborting"
502                                 exit 1
503                         fi
504                 fi
505                 set -- "darkplaces/darkplaces$client" -nexuiz -customgamename Xonotic -customgamedirname1 data -customgamedirname2 "" -customgamescreenshotname xonotic -customgameuserdirname xonotic "$@"
506
507                 # if pulseaudio is running: USE IT
508                 if ps -C pulseaudio >/dev/null; then
509                         if ldd /usr/lib/libSDL.so 2>/dev/null | grep pulse >/dev/null; then
510                                 export SDL_AUDIODRIVER=pulse
511                         fi
512                 fi
513
514                 if [ -n "$USE_GDB" ]; then
515                         set -- gdb --args "$@"
516                 fi
517                 "$@"
518                 ;;
519         each|foreach)
520                 for d in $repos; do
521                         if verbose cd "$d0/$d"; then
522                                 verbose "$@"
523                                 cd "$d0"
524                         fi
525                 done
526                 ;;
527         save-patches)
528                 outfile=$1
529                 patchdir=`mktemp -d -t save-patches.XXXXXX`
530                 for d in $repos; do
531                         enter "$d0/$d" verbose
532                         git branch -v -v | cut -c 3- | {
533                                 i=0
534                                 while read -r BRANCH REV UPSTREAM TEXT; do
535                                         case "$UPSTREAM" in
536                                                 \[*)
537                                                         UPSTREAM=${UPSTREAM#\[}
538                                                         UPSTREAM=${UPSTREAM%\]}
539                                                         UPSTREAM=${UPSTREAM%:*}
540                                                         TRACK=true
541                                                         ;;
542                                                 *)
543                                                         UPSTREAM=origin/"`repobranch "$d"`"
544                                                         TRACK=false
545                                                         ;;
546                                         esac
547                                         if [ x"$REV" = x"->" ]; then
548                                                 continue
549                                         fi
550                                         if git format-patch -o "$patchdir/$i" "$UPSTREAM".."$BRANCH"; then
551                                                 echo "$d" > "$patchdir/$i/info.txt"
552                                                 echo "$BRANCH" >> "$patchdir/$i/info.txt"
553                                                 echo "$UPSTREAM" >> "$patchdir/$i/info.txt"
554                                                 echo "$TRACK" >> "$patchdir/$i/info.txt"
555                                                 i=$(($i+1))
556                                         else
557                                                 rm -rf "$patchdir/$i"
558                                         fi
559                                 done
560                         }
561                 done
562                 ( cd "$patchdir" && tar cvzf - . ) > "$outfile"
563                 rm -rf "$patchdir"
564                 ;;
565         restore-patches)
566                 infile=$1
567                 patchdir=`mktemp -d -t restore-patches.XXXXXX`
568                 ( cd "$patchdir" && tar xvzf - ) < "$infile"
569                 # detach the head
570                 for P in "$patchdir"/*/info.txt; do
571                         D=${P%/info.txt}
572                         exec 3<"$P"
573                         read -r d <&3
574                         read -r BRANCH <&3
575                         read -r UPSTREAM <&3
576                         read -r TRACK <&3
577                         verbose git checkout HEAD^0
578                         verbose git branch -D "$BRANCH"
579                         if [ x"$TRACK" = x"true" ]; then
580                                 verbose git checkout --track -b "$BRANCH" "$UPSTREAM"
581                         else
582                                 verbose git branch -b "$BRANCH" "$UPSTREAM"
583                         fi
584                         verbose git am "$D"
585                 done
586                 rm -rf "$patchdir"
587                 ;;
588         admin-merge)
589                 if [ "$#" = 1 ]; then
590                         set -- "${1%%/*}" "${1#*/}"
591                 fi
592                 for d in $repos; do
593                         enter "$d0/$d" verbose
594                         git rev-parse "$1/$2" || continue
595                         # 1. review
596                         {
597                                 git log HEAD.."$1/$2"
598                                 git diff HEAD..."$1/$2"
599                         } | less
600                         if yesno "Merge \"$1/$2\" into `git symbolic-ref HEAD` of $d?"; then
601                                 git merge "$1/$2"
602                                 if "$SELF" compile && yesno "Still merge \"$1/$2\" into `git symbolic-ref HEAD` of $d? Maybe you want to test first."; then
603                                         git push origin HEAD
604                                         git push "$1" :"$2"
605                                 else
606                                         git reset --hard HEAD@{1}
607                                 fi
608                         fi
609                 done
610                 ;;
611         admin-merge-2)
612                 t=`mktemp`
613                 for d in $repos; do
614                         enter "$d0/$d" verbose
615                         for ref in `git for-each-ref --format='%(refname)' refs/remotes/origin/`; do
616                                 case "${ref#refs/remotes/origin/}" in
617                                         "`repobranch "$d"`")
618                                                 continue
619                                                 ;;
620                                         HEAD|master)
621                                                 continue
622                                                 ;;
623                                 esac
624                                 echo "$ref"
625                                 if git notes --ref "refs/notes/admin-merge" show "$ref" 2>/dev/null; then
626                                         echo "Not merging, already had this one"
627                                 elif yesno "Branch \"$ref\" may want to get merged. Do it?" '{ git log HEAD.."$ref"; git diff HEAD.."$ref"; } | less'; then
628                                         org=`git rev-parse HEAD`
629                                         if ! git merge "$ref" 2>&1 | tee "$t"; then
630                                                 git reset --hard "$org"
631                                                 git notes --ref "refs/notes/admin-merge" add -m "Merge failed:
632 `cat "$t"`" "$ref"
633                                         elif ! "$SELF" compile 2>&1 | tee "$t"; then
634                                                 git reset --hard "$org"
635                                                 git notes --ref "refs/notes/admin-merge" add -m "Compile failed:
636 `cat "$t"`" "$ref"
637                                         elif ! yesno "Still merge \"$ref\" into `git symbolic-ref HEAD` of $d? Maybe you want to test first."; then
638                                                 git reset --hard HEAD@{1}
639                                                 git notes --ref "refs/notes/admin-merge" add "$ref"
640                                         else
641                                                 git push origin HEAD
642                                                 git push origin :"${ref#refs/remotes/origin/}"
643                                         fi
644                                 else
645                                         git reset --hard HEAD@{1}
646                                         git notes --ref "refs/notes/admin-merge" add "$ref"
647                                 fi
648                         done
649                 done
650                 rm -f "$t"
651                 ;;
652         *)
653                 echo "Usage:"
654                 echo "  $SELF pull"
655                 echo "  $SELF merge"
656                 echo "  $SELF push [-s]"
657                 echo "  $SELF branches"
658                 echo "  $SELF branch [<remote>] <branchname>"
659                 echo "  $SELF branch <remote> <branchname> <srcbranchname>"
660                 echo "  $SELF checkout [<remote>] <branchname>"
661                 echo "  $SELF compile [-c] [<client>] <options>"
662                 echo "  $SELF run [<client>] <options>"
663                 echo "  $SELF each <command>"
664                 ;;
665 esac