e4316a0c8a2d84a92ba3ee6f5f196c0fa1cad81b
[xonotic/xonotic.git] / all
1 #!/bin/sh
2
3 set -e
4
5 d00=`pwd`
6 while ! [ -f ./all ]; do
7         if [ x"`pwd`" = x"/" ]; then
8                 echo "Cannot find myself."
9                 echo "Please run this script with the working directory inside a Xonotic checkout."
10                 exit 1
11         fi
12         cd ..
13 done
14 d0=`pwd`
15 SELF="$d0/all"
16
17 # If we are on WINDOWS:
18 case "$0" in
19         all|*/all)
20                 case "`uname`" in
21                         MINGW*|Win*)
22                                 # Windows hates users. So this script has to copy itself elsewhere first...
23                                 tname=
24                                 cp "$SELF" ../all.xonotic.sh
25                                 exec ../all.xonotic.sh "$@"
26                                 ;;
27                 esac
28                 ;;
29 esac
30
31 msg()
32 {
33         echo "\e[1m$*\e[m"
34 }
35
36 checksum()
37 {
38         if [ -x /usr/bin/md5sum ]; then
39                 /usr/bin/md5sum "$@"
40         elif [ -x /bin/md5sum ]; then
41                 /bin/md5sum "$@"
42         elif [ -x /usr/bin/cksum ]; then
43                 /usr/bin/cksum "$@"
44         else
45                 echo "NOCHECKSUM"
46         fi
47 }
48
49 self=`checksum "$SELF"`
50 checkself()
51 {
52         self_new=`checksum "$SELF"`
53         if [ x"$self" != x"$self_new" ]; then
54                 msg "./all has changed."
55                 if [ -z "$XONOTIC_FORBID_RERUN_ALL" ]; then
56                         msg "Rerunning the requested operation to make sure."
57                         export XONOTIC_FORBID_RERUN_ALL=1
58                         exec "$SELF" "$@"
59                 else
60                         msg "Please try $SELF update, and then retry your requested operation."
61                         exit 1
62                 fi
63         fi
64         return 0
65 }
66
67 verbose()
68 {
69         msg "+ $*"
70         "$@"
71 }
72
73 visible_repo_name()
74 {
75         case "$1" in
76                 .)
77                         echo "the root directory"
78                         ;;
79                 *)
80                         echo "\"$1\""
81                         ;;
82         esac
83 }
84
85 check_mergeconflict()
86 {
87         if git ls-files -u | grep ' 1   '; then
88                 echo
89                 echo "MERGE CONFLICT."
90                 echo "change into the \"$1\" project directory, and then:"
91                 echo "- edit the files mentioned above with your favorite editor,"
92                 echo "  and fix the conflicts (marked with <<<<<<< blocks)"
93                 echo "- for binary files, you can select the files using"
94                 echo "  git checkout --ours or git checkout --theirs"
95                 echo "- when done with a file, 'git add' the file"
96                 echo "- when done, 'git commit'"
97                 echo
98                 exit 1
99         fi
100 }
101
102 enter()
103 {
104         $2 cd "$1"
105         check_mergeconflict "$1"
106 }
107
108 repos_urls="
109         .
110         data/xonotic-data.pk3dir
111         data/xonotic-maps.pk3dir
112         data/xonotic-music.pk3dir
113         data/xonotic-nexcompat.pk3dir
114         darkplaces
115         fteqcc@git://github.com/Blub/qclib.git
116         div0-gittools@git://git.icculus.org/divverent/div0-gittools.git
117         netradiant
118 "
119
120 repos=`for X in $repos_urls; do echo "${X%%@*}"; done`
121
122 if [ "$#" = 0 ]; then
123         set -- help
124 fi
125 cmd=$1
126 shift
127
128 case "$cmd" in
129         update|pull)
130                 base=`git config remote.origin.url`
131                 base=${base%xonotic.git}
132                 for dcomplete in $repos_urls; do
133                         case "$dcomplete" in
134                                 *@*)
135                                         d=${dcomplete%%@*}
136                                         url=${dcomplete#*@}
137                                         switch=false
138                                         ;;
139                                 *)
140                                         d=${dcomplete%%@*}
141                                         url=$base${d##*/}.git
142                                         switch=true
143                                         ;;
144                         esac
145                         if [ -d "$d0/$d" ]; then
146                                 enter "$d0/$d" verbose
147                                 case "$d" in
148                                         .)
149                                                 ;;
150                                         *)
151                                                 if $switch; then
152                                                         verbose git config remote.origin.url "$url"
153                                                 fi
154                                                 ;;
155                                 esac
156                                 verbose git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
157                                         # TODO remove this line later
158
159                                 git config core.autocrlf input
160                                 git config core.safecrlf true
161
162                                 r=`git symbolic-ref HEAD`
163                                 r=${r#refs/heads/}
164                                 if git config branch.$r.remote >/dev/null 2>&1; then
165                                         if ! verbose git pull; then
166                                                 check_mergeconflict "$d"
167                                                 exit 1 # FATAL
168                                         fi
169                                 fi
170
171                                 cd "$d00"
172                                 checkself "$cmd" "$@"
173                                 cd "$d0/$d"
174                                 verbose git remote prune origin
175                                 cd "$d0"
176                         else
177                                 verbose git clone "$url" "$d0/$d"
178                         fi
179                 done
180                 ;;
181         checkout|switch)
182                 remote=$1
183                 branch=$2
184                 if [ -z "$branch" ]; then
185                         branch=$remote
186                         remote=origin
187                 fi
188                 exists=false
189                 for d in $repos; do
190                         enter "$d0/$d" verbose
191                         if git rev-parse "refs/heads/$branch" >/dev/null 2>&1; then
192                                 exists=true
193                                 verbose git checkout "$branch"
194                         elif git rev-parse "refs/remotes/$remote/$branch" >/dev/null 2>&1; then
195                                 exists=true
196                                 verbose git checkout --track -b "$branch" "$remote/$branch"
197                         else
198                                 verbose git checkout master
199                         fi
200                         cd "$d00"
201                         checkself "$cmd" "$@"
202                         cd "$d0"
203                 done
204                 if ! $exists; then
205                         echo "The requested branch was not found in any repository."
206                 fi
207                 exec "$SELF" branch
208                 ;;
209         branch)
210                 remote=$1
211                 branch=$2
212                 srcbranch=$3
213                 if [ -z "$branch" ]; then
214                         branch=$remote
215                         remote=origin
216                 fi
217                 if [ -z "$srcbranch" ]; then
218                         srcbranch=master
219                 fi
220                 if [ -z "$branch" ]; then
221                         for d in $repos; do
222                                 enter "$d0/$d"
223                                 r=`git symbolic-ref HEAD`
224                                 r=${r#refs/heads/}
225                                 echo "$d is at $r"
226                                 cd "$d0"
227                         done
228                 else
229                         for d in $repos; do
230                                 dv=`visible_repo_name "$d"`
231                                 enter "$d0/$d" verbose
232                                 a=
233                                 while [ x"$a" != x"y" -a x"$a" != x"n" ]; do
234                                         echo "Branch in $dv?"
235                                         read -r a
236                                 done
237                                 if [ x"$a" = x"y" ]; then
238                                         verbose git push "$remote" "$srcbranch":"$branch"
239                                         verbose git checkout --track -b "$branch" "$remote/$branch"
240                                 fi
241                                 cd "$d0"
242                         done
243                         "$SELF" branch
244                 fi
245                 ;;
246         branches)
247                 for d in $repos; do
248                         enter "$d0/$d"
249                         echo "In $d:"
250                         git branch -a -v -v | cut -c 3- | while read -r BRANCH REV UPSTREAM TEXT; do
251                                 case "$UPSTREAM" in
252                                         \[*)
253                                                 UPSTREAM=${UPSTREAM#\[}
254                                                 UPSTREAM=${UPSTREAM%\]}
255                                                 UPSTREAM=${UPSTREAM%:*}
256                                                 ;;
257                                         *)
258                                                 TEXT="$UPSTREAM $TEXT"
259                                                 UPSTREAM=
260                                                 ;;
261                                 esac
262                                 if [ x"$REV" = x"->" ]; then
263                                         continue
264                                 fi
265                                 BRANCH=${BRANCH#remotes/}
266                                 echo -n "  $BRANCH"
267                                 if [ -n "$UPSTREAM" ]; then
268                                         echo -n " (tracking $UPSTREAM)"
269                                 fi
270                                 #echo " $TEXT"
271                                 echo
272                         done
273                 done
274                 ;;
275         merge)
276                 for d in $repos; do
277                         dv=`visible_repo_name "$d"`
278                         enter "$d0/$d" verbose
279                         r=`git symbolic-ref HEAD`
280                         r=${r#refs/heads/}
281                         if git log HEAD..origin/master | grep .; then
282                                 # we have uncommitted changes
283                                 a=
284                                 while [ x"$a" != x"y" -a x"$a" != x"n" ]; do
285                                         echo "Could merge from \"master\" into \"$r\" in $dv. Do it?"
286                                         read -r a
287                                 done
288                                 if [ x"$a" = x"y" ]; then
289                                         if ! verbose git merge origin/master; then
290                                                 check_mergeconflict "$d"
291                                                 exit 1 # this should ALWAYS be fatal
292                                         fi
293                                 fi
294                         fi
295                         cd "$d0"
296                 done
297                 ;;
298         push|commit)
299                 for d in $repos; do
300                         dv=`visible_repo_name "$d"`
301                         enter "$d0/$d" verbose
302                         r=`git symbolic-ref HEAD`
303                         r=${r#refs/heads/}
304                         if git diff HEAD | grep .; then
305                                 # we have uncommitted changes
306                                 a=
307                                 while [ x"$a" != x"y" -a x"$a" != x"n" ]; do
308                                         echo "Uncommitted changes in \"$r\" in $dv. Commit?"
309                                         read -r a
310                                 done
311                                 if [ x"$a" = x"y" ]; then
312                                         verbose git commit -a
313                                 fi
314                         fi
315                         rem=`git config "branch.$r.remote" || echo origin`
316                         if git log "$rem/$r".."$r" | grep .; then
317                                 a=
318                                 while [ x"$a" != x"y" -a x"$a" != x"n" ]; do
319                                         echo "Push \"$r\" in $dv?"
320                                         read -r a
321                                 done
322                                 if [ x"$a" = x"y" ]; then
323                                         verbose git push "$rem" HEAD
324                                 fi
325                         fi
326                         cd "$d0"
327                 done
328                 ;;
329         compile)
330                 if [ -z "$MAKEFLAGS" ]; then
331                         if [ -f /proc/cpuinfo ]; then
332                                 ncpus=$((`grep -c '^processor   :' /proc/cpuinfo`+0))
333                                 if [ $ncpus -gt 1 ]; then
334                                         MAKEFLAGS=-j$ncpus
335                                 fi
336                         fi
337                 fi
338                 enter "$d0/fteqcc" verbose
339                 verbose make $MAKEFLAGS
340                 enter "$d0/data/xonotic-data.pk3dir" verbose
341                 verbose make FTEQCC="$d0/fteqcc/fteqcc.bin" $MAKEFLAGS
342                 enter "$d0/darkplaces" verbose
343                 verbose make $MAKEFLAGS sv-debug
344                 verbose make $MAKEFLAGS cl-debug
345                 verbose make $MAKEFLAGS sdl-debug
346                 ;;
347         run)
348                 client=-sdl
349                 case "$1" in
350                         sdl|glx|agl|dedicated)
351                                 client=-$1
352                                 shift
353                                 ;;
354                         wgl)
355                                 client=
356                                 shift
357                                 ;;
358                 esac
359                 if ! [ -x "darkplaces/darkplaces$client" ]; then
360                         if [ -x "darkplaces/darkplaces$client.exe" ]; then
361                                 client=$client.exe
362                         else
363                                 echo "Client darkplaces/darkplaces$client not found, aborting"
364                                 exit 1
365                         fi
366                 fi
367                 #verbose "darkplaces/darkplaces$client" -xonotic "$@"
368                 verbose "darkplaces/darkplaces$client" -nexuiz -customgamename Xonotic -customgamedirname1 data -customgamedirname2 "" -customgamescreenshotname xonotic -customgameuserdirname xonotic "$@"
369                 ;;
370         each|foreach)
371                 for d in $repos; do
372                         enter "$d0/$d" verbose
373                         verbose "$@"
374                         cd "$d0"
375                 done
376                 ;;
377         save-patches)
378                 outfile=$1
379                 patchdir=`mktemp -d -t save-patches.XXXXXX`
380                 for d in $repos; do
381                         enter "$d0/$d" verbose
382                         git branch -v -v | cut -c 3- | {
383                                 i=0
384                                 while read -r BRANCH REV UPSTREAM TEXT; do
385                                         case "$UPSTREAM" in
386                                                 \[*)
387                                                         UPSTREAM=${UPSTREAM#\[}
388                                                         UPSTREAM=${UPSTREAM%\]}
389                                                         UPSTREAM=${UPSTREAM%:*}
390                                                         TRACK=true
391                                                         ;;
392                                                 *)
393                                                         UPSTREAM=origin/master
394                                                         TRACK=false
395                                                         ;;
396                                         esac
397                                         if [ x"$REV" = x"->" ]; then
398                                                 continue
399                                         fi
400                                         if git format-patch -o "$patchdir/$i" "$UPSTREAM".."$BRANCH"; then
401                                                 echo "$d" > "$patchdir/$i/info.txt"
402                                                 echo "$BRANCH" >> "$patchdir/$i/info.txt"
403                                                 echo "$UPSTREAM" >> "$patchdir/$i/info.txt"
404                                                 echo "$TRACK" >> "$patchdir/$i/info.txt"
405                                                 i=$(($i+1))
406                                         else
407                                                 rm -rf "$patchdir/$i"
408                                         fi
409                                 done
410                         }
411                 done
412                 ( cd "$patchdir" && tar cvzf - . ) > "$outfile"
413                 rm -rf "$patchdir"
414                 ;;
415         restore-patches)
416                 infile=$1
417                 patchdir=`mktemp -d -t restore-patches.XXXXXX`
418                 ( cd "$patchdir" && tar xvzf - ) < "$infile"
419                 # detach the head
420                 for P in "$patchdir"/*/info.txt; do
421                         D=${P%/info.txt}
422                         exec 3<"$P"
423                         read -r d <&3
424                         read -r BRANCH <&3
425                         read -r UPSTREAM <&3
426                         read -r TRACK <&3
427                         verbose git checkout HEAD^0
428                         verbose git branch -D "$BRANCH"
429                         if [ x"$TRACK" = x"true" ]; then
430                                 verbose git checkout --track -b "$BRANCH" "$UPSTREAM"
431                         else
432                                 verbose git branch -b "$BRANCH" "$UPSTREAM"
433                         fi
434                         verbose git am "$D"
435                 done
436                 rm -rf "$patchdir"
437                 ;;
438         *)
439                 echo "Usage:"
440                 echo "  $SELF pull"
441                 echo "  $SELF merge"
442                 echo "  $SELF push"
443                 echo "  $SELF branches"
444                 echo "  $SELF branch [<remote>] <branchname>"
445                 echo "  $SELF branch <remote> <branchname> <srcbranchname>"
446                 echo "  $SELF checkout [<remote>] <branchname>"
447                 echo "  $SELF compile"
448                 echo "  $SELF run <client> <options>"
449                 echo "  $SELF each <command>"
450                 ;;
451 esac