changing separator to / to work around startx bug
[divverent/nexuiz.git] / nexuiz-linux-sdl.sh
1 #!/bin/sh
2
3 path="`dirname \"${0}\"`"
4 link="`readlink \"${0}\"`"
5
6 [ -n "${link}" ] && path="`dirname \"${link}\"`"
7 cd "${path}"
8
9 case "$(uname -m)" in
10   x86_64)       nexuiz="nexuiz-linux-x86_64-sdl" ;;
11   *)            nexuiz="nexuiz-linux-686-sdl" ;;
12 esac
13
14 set -- ./${nexuiz} "${@}"
15
16 xserver=
17 xlayout=
18
19 setdisplay()
20 {
21         VALUE=$1
22         VALUE=${VALUE#\"}
23         VALUE=${VALUE%\"}
24         case "$VALUE" in
25                 :*)
26                         ;;
27                 *)
28                         VALUE=:$VALUE
29                         ;;
30         esac
31         VALUE="$VALUE/"
32         xserver="${VALUE%%/*}"
33         xserver=${xserver#:}
34         xlayout=${VALUE#*/}
35         xlayout=${xlayout%/}
36 }
37
38 # now how do we execute it?
39 if [ -r ~/.nexuiz/data/config.cfg ]; then
40         while read -r CMD KEY VALUE; do
41                 case "$CMD:$KEY" in
42                         seta:vid_x11_display)
43                                 setdisplay "$VALUE"
44                                 ;;
45                 esac
46         done < ~/.nexuiz/data/config.cfg
47 fi
48
49 m=0
50 for X in "$@"; do
51         case "$m:$X" in
52                 0:+vid_x11_display)
53                         m=1
54                         ;;
55                 0:+vid_x11_display\ *)
56                         setdisplay "${X#+vid_x11_display }"
57                         ;;
58                 1:*)
59                         setdisplay "$X"
60                         m=0
61                         ;;
62                 *)
63                         ;;
64         esac
65 done
66
67 case "$xserver" in
68         '')
69                 ;;
70         *[!0-9]*)
71                 echo "Not using display ':$xserver': evil characters"
72                 ;;
73         *)
74                 msg=
75                 lf='
76 '
77                 prefix=
78
79                 # check for a listening X server on that socket
80                 if netstat -nl | grep -F " /tmp/.X11-unix/X$xserver" >/dev/null; then
81                         # X server already exists
82                         export DISPLAY=:$xserver
83                         prefix="DISPLAY=:$xserver "
84                         msg=$msg$lf"- Running Nexuiz on already existing display :$xserver"
85                 else
86                         set -- startx "$@" -fullscreen -- ":$xserver"
87                         msg=$msg$lf"- Running Nexuiz on a newly created X server :$xserver."
88                         case "$xlayout" in
89                                 '')
90                                         ;;
91                                 *[!A-Za-z0-9]*)
92                                         echo >&2 "Not using layout '$xlayout': evil characters"
93                                         xlayout=
94                                         ;;
95                                 *)
96                                         set -- "$@" -layout "$xlayout"
97                                         msg=$msg$lf"- Using the ServerLayout section named $xlayout."
98                                         ;;
99                         esac
100                 fi
101
102                 echo "X SERVER OVERRIDES IN EFFECT:$msg"
103                 echo
104                 echo "Resulting command line:"
105                 echo "  $prefix$*"
106                 echo
107                 echo "To undo these overrides, edit ~/.nexuiz/data/config.cfg and remove the line"
108                 echo "starting with 'seta vid_x11_display'."
109                 echo
110                 echo
111                 ;;
112 esac
113
114 exec "$@"