5e713f9c3d0164670f499badd7562e3230af5f25
[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*)
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 repos_urls="
74         .
75         data/xonotic-data.pk3dir
76         data/xonotic-maps.pk3dir
77         data/xonotic-music.pk3dir
78         data/xonotic-nexcompat.pk3dir
79         darkplaces
80         fteqcc@git://github.com/Blub/qclib.git
81         div0-gittools@git://git.icculus.org/divverent/div0-gittools.git
82         netradiant
83 "
84
85 repos=`for X in $repos_urls; do echo "${X%%@*}"; done`
86
87 if [ "$#" = 0 ]; then
88         set -- help
89 fi
90 cmd=$1
91 shift
92
93 case "$cmd" in
94         update|pull)
95                 base=`git config remote.origin.url`
96                 base=${base%xonotic.git}
97                 for dcomplete in $repos_urls; do
98                         case "$dcomplete" in
99                                 *@*)
100                                         d=${dcomplete%%@*}
101                                         url=${dcomplete#*@}
102                                         switch=false
103                                         ;;
104                                 *)
105                                         d=${dcomplete%%@*}
106                                         url=$base${d##*/}.git
107                                         switch=true
108                                         ;;
109                         esac
110                         if [ -d "$d0/$d" ]; then
111                                 verbose cd "$d0/$d"
112                                 case "$d" in
113                                         .)
114                                                 ;;
115                                         *)
116                                                 if $switch; then
117                                                         verbose git config remote.origin.url "$url"
118                                                 fi
119                                                 ;;
120                                 esac
121                                 verbose git pull || true # errors if the branch is not tracking anything
122                                 cd "$d00"
123                                 checkself "$SELF" "$@"
124                                 cd "$d0/$d"
125                                 verbose git remote prune origin
126                                 cd "$d0"
127                         else
128                                 verbose git clone "$url" "$d0/$d"
129                         fi
130                 done
131                 ;;
132         checkout|switch)
133                 remote=$1
134                 branch=$2
135                 if [ -z "$branch" ]; then
136                         branch=$remote
137                         remote=origin
138                 fi
139                 exists=false
140                 for d in $repos; do
141                         verbose cd "$d0/$d"
142                         if git rev-parse "refs/heads/$branch" >/dev/null 2>&1; then
143                                 exists=true
144                                 verbose git checkout "$branch"
145                         elif git rev-parse "refs/remotes/$remote/$branch" >/dev/null 2>&1; then
146                                 exists=true
147                                 verbose git checkout --track -b "$branch" "$remote/$branch"
148                         else
149                                 verbose git checkout master
150                         fi
151                         cd "$d00"
152                         checkself "$SELF" "$@"
153                         cd "$d0"
154                 done
155                 if ! $exists; then
156                         echo "The requested branch was not found in any repository."
157                 fi
158                 exec "$SELF" branch
159                 ;;
160         branch)
161                 remote=$1
162                 branch=$2
163                 if [ -z "$branch" ]; then
164                         branch=$remote
165                         remote=origin
166                 fi
167                 if [ -z "$branch" ]; then
168                         for d in $repos; do
169                                 cd "$d0/$d"
170                                 r=`git symbolic-ref HEAD`
171                                 r=${r#refs/heads/}
172                                 echo "$d is at $r"
173                                 cd "$d0"
174                         done
175                 else
176                         for d in $repos; do
177                                 cd "$d0/$d"
178                                 a=
179                                 while [ x"$a" != x"y" -a x"$a" != x"n" ]; do
180                                         echo "Branch in \"$d\"?"
181                                         read -r a
182                                 done
183                                 if [ x"$a" = x"y" ]; then
184                                         verbose git push "$remote" HEAD:"$branch"
185                                         verbose git checkout --track -b "$branch" "$remote/$branch"
186                                 fi
187                                 cd "$d0"
188                         done
189                         "$SELF" branch
190                 fi
191                 ;;
192         branches)
193                 for d in $repos; do
194                         cd "$d0/$d"
195                         echo "In $d:"
196                         git branch -a | sed 's/^/  /; /->/d'
197                         cd "$d0"
198                 done
199                 ;;
200         push)
201                 for d in $repos; do
202                         cd "$d0/$d"
203                         r=`git symbolic-ref HEAD`
204                         r=${r#refs/heads/}
205                         if git diff HEAD | grep .; then
206                                 # we have uncommitted changes
207                                 a=
208                                 while [ x"$a" != x"y" -a x"$a" != x"n" ]; do
209                                         echo "Uncommitted changes in \"$r\" in \"$d\". Commit?"
210                                         read -r a
211                                 done
212                                 if [ x"$a" = x"y" ]; then
213                                         verbose git commit -a
214                                 fi
215                         fi
216                         if git log "origin/$r".."$r" | grep .; then
217                                 a=
218                                 while [ x"$a" != x"y" -a x"$a" != x"n" ]; do
219                                         echo "Push \"$r\" in \"$d\"?"
220                                         read -r a
221                                 done
222                                 if [ x"$a" = x"y" ]; then
223                                         verbose git push `git config "branch.$r.remote" || echo origin` HEAD
224                                 fi
225                         fi
226                         cd "$d0"
227                 done
228                 ;;
229         compile)
230                 if [ -z "$MAKEFLAGS" ]; then
231                         if [ -f /proc/cpuinfo ]; then
232                                 ncpus=$((`grep -c '^processor   :' /proc/cpuinfo`+0))
233                                 if [ $ncpus -gt 1 ]; then
234                                         MAKEFLAGS=-j$ncpus
235                                 fi
236                         fi
237                 fi
238                 verbose cd "$d0/fteqcc"
239                 verbose make $MAKEFLAGS
240                 verbose cd "$d0/data/xonotic-data.pk3dir"
241                 verbose make FTEQCC="$d0/fteqcc/fteqcc.bin" $MAKEFLAGS
242                 verbose cd "$d0/darkplaces"
243                 verbose make $MAKEFLAGS sv-debug
244                 verbose make $MAKEFLAGS cl-debug
245                 verbose make $MAKEFLAGS sdl-debug
246                 ;;
247         run)
248                 client=-sdl
249                 case "$1" in
250                         sdl|glx|agl|dedicated)
251                                 client=-$1
252                                 shift
253                                 ;;
254                         wgl)
255                                 client=
256                                 shift
257                                 ;;
258                 esac
259                 if ! [ -x "darkplaces/darkplaces$client" ]; then
260                         if [ -x "darkplaces/darkplaces$client.exe" ]; then
261                                 client=$client.exe
262                         else
263                                 echo "Client darkplaces/darkplaces$client not found, aborting"
264                                 exit 1
265                         fi
266                 fi
267                 #verbose "darkplaces/darkplaces$client" -xonotic "$@"
268                 verbose "darkplaces/darkplaces$client" -nexuiz -customgamename Xonotic -customgamedirname1 data -customgamedirname2 "" -customgamescreenshotname xonotic -customgameuserdirname xonotic "$@"
269                 ;;
270         each|foreach)
271                 for d in $repos; do
272                         verbose cd "$d0/$d"
273                         verbose "$@"
274                         cd "$d0"
275                 done
276                 ;;
277         *)
278                 echo "Usage:"
279                 echo "  $SELF pull"
280                 echo "  $SELF push"
281                 echo "  $SELF branches"
282                 echo "  $SELF branch <remote> <branchname>"
283                 echo "  $SELF checkout"
284                 echo "  $SELF compile"
285                 echo "  $SELF run <client> <options>"
286                 echo "  $SELF each <command>"
287                 ;;
288 esac