add some files that should be in this repo
[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                         if git log "origin/$r".."$r" | grep .; then
316                                 a=
317                                 while [ x"$a" != x"y" -a x"$a" != x"n" ]; do
318                                         echo "Push \"$r\" in $dv?"
319                                         read -r a
320                                 done
321                                 if [ x"$a" = x"y" ]; then
322                                         verbose git push `git config "branch.$r.remote" || echo origin` HEAD
323                                 fi
324                         fi
325                         cd "$d0"
326                 done
327                 ;;
328         compile)
329                 if [ -z "$MAKEFLAGS" ]; then
330                         if [ -f /proc/cpuinfo ]; then
331                                 ncpus=$((`grep -c '^processor   :' /proc/cpuinfo`+0))
332                                 if [ $ncpus -gt 1 ]; then
333                                         MAKEFLAGS=-j$ncpus
334                                 fi
335                         fi
336                 fi
337                 enter "$d0/fteqcc" verbose
338                 verbose make $MAKEFLAGS
339                 enter "$d0/data/xonotic-data.pk3dir" verbose
340                 verbose make FTEQCC="$d0/fteqcc/fteqcc.bin" $MAKEFLAGS
341                 enter "$d0/darkplaces" verbose
342                 verbose make $MAKEFLAGS sv-debug
343                 verbose make $MAKEFLAGS cl-debug
344                 verbose make $MAKEFLAGS sdl-debug
345                 ;;
346         run)
347                 client=-sdl
348                 case "$1" in
349                         sdl|glx|agl|dedicated)
350                                 client=-$1
351                                 shift
352                                 ;;
353                         wgl)
354                                 client=
355                                 shift
356                                 ;;
357                 esac
358                 if ! [ -x "darkplaces/darkplaces$client" ]; then
359                         if [ -x "darkplaces/darkplaces$client.exe" ]; then
360                                 client=$client.exe
361                         else
362                                 echo "Client darkplaces/darkplaces$client not found, aborting"
363                                 exit 1
364                         fi
365                 fi
366                 #verbose "darkplaces/darkplaces$client" -xonotic "$@"
367                 verbose "darkplaces/darkplaces$client" -nexuiz -customgamename Xonotic -customgamedirname1 data -customgamedirname2 "" -customgamescreenshotname xonotic -customgameuserdirname xonotic "$@"
368                 ;;
369         each|foreach)
370                 for d in $repos; do
371                         enter "$d0/$d" verbose
372                         verbose "$@"
373                         cd "$d0"
374                 done
375                 ;;
376         save-patches)
377                 outfile=$1
378                 patchdir=`mktemp -d -t save-patches.XXXXXX`
379                 for d in $repos; do
380                         enter "$d0/$d" verbose
381                         git branch -v -v | cut -c 3- | {
382                                 i=0
383                                 while read -r BRANCH REV UPSTREAM TEXT; do
384                                         case "$UPSTREAM" in
385                                                 \[*)
386                                                         UPSTREAM=${UPSTREAM#\[}
387                                                         UPSTREAM=${UPSTREAM%\]}
388                                                         UPSTREAM=${UPSTREAM%:*}
389                                                         TRACK=true
390                                                         ;;
391                                                 *)
392                                                         UPSTREAM=origin/master
393                                                         TRACK=false
394                                                         ;;
395                                         esac
396                                         if [ x"$REV" = x"->" ]; then
397                                                 continue
398                                         fi
399                                         if git format-patch -o "$patchdir/$i" "$UPSTREAM".."$BRANCH"; then
400                                                 echo "$d" > "$patchdir/$i/info.txt"
401                                                 echo "$BRANCH" >> "$patchdir/$i/info.txt"
402                                                 echo "$UPSTREAM" >> "$patchdir/$i/info.txt"
403                                                 echo "$TRACK" >> "$patchdir/$i/info.txt"
404                                                 i=$(($i+1))
405                                         else
406                                                 rm -rf "$patchdir/$i"
407                                         fi
408                                 done
409                         }
410                 done
411                 ( cd "$patchdir" && tar cvzf - . ) > "$outfile"
412                 rm -rf "$patchdir"
413                 ;;
414         restore-patches)
415                 infile=$1
416                 patchdir=`mktemp -d -t restore-patches.XXXXXX`
417                 ( cd "$patchdir" && tar xvzf - ) < "$infile"
418                 # detach the head
419                 for P in "$patchdir"/*/info.txt; do
420                         D=${P%/info.txt}
421                         exec 3<"$P"
422                         read -r d <&3
423                         read -r BRANCH <&3
424                         read -r UPSTREAM <&3
425                         read -r TRACK <&3
426                         verbose git checkout HEAD^0
427                         verbose git branch -D "$BRANCH"
428                         if [ x"$TRACK" = x"true" ]; then
429                                 verbose git checkout --track -b "$BRANCH" "$UPSTREAM"
430                         else
431                                 verbose git branch -b "$BRANCH" "$UPSTREAM"
432                         fi
433                         verbose git am "$D"
434                 done
435                 rm -rf "$patchdir"
436                 ;;
437         *)
438                 echo "Usage:"
439                 echo "  $SELF pull"
440                 echo "  $SELF merge"
441                 echo "  $SELF push"
442                 echo "  $SELF branches"
443                 echo "  $SELF branch [<remote>] <branchname>"
444                 echo "  $SELF branch <remote> <branchname> <srcbranchname>"
445                 echo "  $SELF checkout [<remote>] <branchname>"
446                 echo "  $SELF compile"
447                 echo "  $SELF run <client> <options>"
448                 echo "  $SELF each <command>"
449                 ;;
450 esac