daemon: if one of the standard fds is missing open it to /dev/null
[git] / git-instaweb.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2006 Eric Wong
4 #
5 USAGE='[--start] [--stop] [--restart]
6   [--local] [--httpd=<httpd>] [--port=<port>] [--browser=<browser>]
7   [--module-path=<path> (for Apache2 only)]'
8
9 . git-sh-setup
10
11 case "$GIT_DIR" in
12 /*)
13         fqgitdir="$GIT_DIR" ;;
14 *)
15         fqgitdir="$PWD/$GIT_DIR" ;;
16 esac
17
18 local="`git repo-config --bool --get instaweb.local`"
19 httpd="`git repo-config --get instaweb.httpd`"
20 browser="`git repo-config --get instaweb.browser`"
21 port=`git repo-config --get instaweb.port`
22 module_path="`git repo-config --get instaweb.modulepath`"
23
24 conf=$GIT_DIR/gitweb/httpd.conf
25
26 # Defaults:
27
28 # if installed, it doesn't need further configuration (module_path)
29 test -z "$httpd" && httpd='lighttpd -f'
30
31 # probably the most popular browser among gitweb users
32 test -z "$browser" && browser='firefox'
33
34 # any untaken local port will do...
35 test -z "$port" && port=1234
36
37 start_httpd () {
38         httpd_only="`echo $httpd | cut -f1 -d' '`"
39         if test "`expr index $httpd_only /`" -eq '1' || \
40                                 which $httpd_only >/dev/null
41         then
42                 $httpd $fqgitdir/gitweb/httpd.conf
43         else
44                 # many httpds are installed in /usr/sbin or /usr/local/sbin
45                 # these days and those are not in most users $PATHs
46                 for i in /usr/local/sbin /usr/sbin
47                 do
48                         if test -x "$i/$httpd_only"
49                         then
50                                 # don't quote $httpd, there can be
51                                 # arguments to it (-f)
52                                 $i/$httpd "$fqgitdir/gitweb/httpd.conf"
53                                 return
54                         fi
55                 done
56         fi
57 }
58
59 stop_httpd () {
60         test -f "$fqgitdir/pid" && kill `cat "$fqgitdir/pid"`
61 }
62
63 while case "$#" in 0) break ;; esac
64 do
65         case "$1" in
66         --stop|stop)
67                 stop_httpd
68                 exit 0
69                 ;;
70         --start|start)
71                 start_httpd
72                 exit 0
73                 ;;
74         --restart|restart)
75                 stop_httpd
76                 start_httpd
77                 exit 0
78                 ;;
79         --local|-l)
80                 local=true
81                 ;;
82         -d|--httpd|--httpd=*)
83                 case "$#,$1" in
84                 *,*=*)
85                         httpd=`expr "$1" : '-[^=]*=\(.*\)'` ;;
86                 1,*)
87                         usage ;;
88                 *)
89                         httpd="$2"
90                         shift ;;
91                 esac
92                 ;;
93         -b|--browser|--browser=*)
94                 case "$#,$1" in
95                 *,*=*)
96                         browser=`expr "$1" : '-[^=]*=\(.*\)'` ;;
97                 1,*)
98                         usage ;;
99                 *)
100                         browser="$2"
101                         shift ;;
102                 esac
103                 ;;
104         -p|--port|--port=*)
105                 case "$#,$1" in
106                 *,*=*)
107                         port=`expr "$1" : '-[^=]*=\(.*\)'` ;;
108                 1,*)
109                         usage ;;
110                 *)
111                         port="$2"
112                         shift ;;
113                 esac
114                 ;;
115         -m|--module-path=*|--module-path)
116                 case "$#,$1" in
117                 *,*=*)
118                         module_path=`expr "$1" : '-[^=]*=\(.*\)'` ;;
119                 1,*)
120                         usage ;;
121                 *)
122                         module_path="$2"
123                         shift ;;
124                 esac
125                 ;;
126         *)
127                 usage
128                 ;;
129         esac
130         shift
131 done
132
133 mkdir -p "$GIT_DIR/gitweb/tmp"
134 GIT_EXEC_PATH="`git --exec-path`"
135 GIT_DIR="$fqgitdir"
136 export GIT_EXEC_PATH GIT_DIR
137
138
139 lighttpd_conf () {
140         cat > "$conf" <<EOF
141 server.document-root = "$fqgitdir/gitweb"
142 server.port = $port
143 server.modules = ( "mod_cgi" )
144 server.indexfiles = ( "gitweb.cgi" )
145 server.pid-file = "$fqgitdir/pid"
146 cgi.assign = ( ".cgi" => "" )
147 mimetype.assign = ( ".css" => "text/css" )
148 EOF
149         test "$local" = true && echo 'server.bind = "127.0.0.1"' >> "$conf"
150 }
151
152 apache2_conf () {
153         test -z "$module_path" && module_path=/usr/lib/apache2/modules
154         mkdir -p "$GIT_DIR/gitweb/logs"
155         bind=
156         test "$local" = true && bind='127.0.0.1:'
157         echo 'text/css css' > $fqgitdir/mime.types
158         cat > "$conf" <<EOF
159 ServerRoot "$fqgitdir/gitweb"
160 DocumentRoot "$fqgitdir/gitweb"
161 PidFile "$fqgitdir/pid"
162 Listen $bind$port
163 TypesConfig $fqgitdir/mime.types
164 DirectoryIndex gitweb.cgi
165 EOF
166
167         # check to see if Dennis Stosberg's mod_perl compatibility patch
168         # (<20060621130708.Gcbc6e5c@leonov.stosberg.net>) has been applied
169         if test -f "$module_path/mod_perl.so" && grep '^our $gitbin' \
170                                 "$GIT_DIR/gitweb/gitweb.cgi" >/dev/null
171         then
172                 # favor mod_perl if available
173                 cat >> "$conf" <<EOF
174 LoadModule perl_module $module_path/mod_perl.so
175 PerlPassEnv GIT_DIR
176 PerlPassEnv GIT_EXEC_DIR
177 <Location /gitweb.cgi>
178         SetHandler perl-script
179         PerlResponseHandler ModPerl::Registry
180         PerlOptions +ParseHeaders
181         Options +ExecCGI
182 </Location>
183 EOF
184         else
185                 # plain-old CGI
186                 cat >> "$conf" <<EOF
187 LoadModule cgi_module $module_path/mod_cgi.so
188 AddHandler cgi-script .cgi
189 <Location /gitweb.cgi>
190         Options +ExecCGI
191 </Location>
192 EOF
193         fi
194 }
195
196 script='
197 s#^\(my\|our\) $projectroot =.*#\1 $projectroot = "'`dirname $fqgitdir`'";#
198 s#\(my\|our\) $gitbin =.*#\1 $gitbin = "'$GIT_EXEC_PATH'";#
199 s#\(my\|our\) $projects_list =.*#\1 $projects_list = $projectroot;#
200 s#\(my\|our\) $git_temp =.*#\1 $git_temp = "'$fqgitdir/gitweb/tmp'";#'
201
202 gitweb_cgi () {
203         cat > "$1.tmp" <<\EOFGITWEB
204 @@GITWEB_CGI@@
205 EOFGITWEB
206         sed "$script" "$1.tmp"  > "$1"
207         chmod +x "$1"
208         rm -f "$1.tmp"
209 }
210
211 gitweb_css () {
212         cat > "$1" <<\EOFGITWEB
213 @@GITWEB_CSS@@
214 EOFGITWEB
215 }
216
217 gitweb_cgi $GIT_DIR/gitweb/gitweb.cgi
218 gitweb_css $GIT_DIR/gitweb/gitweb.css
219
220 case "$httpd" in
221 *lighttpd*)
222         lighttpd_conf
223         ;;
224 *apache2*)
225         apache2_conf
226         ;;
227 *)
228         echo "Unknown httpd specified: $httpd"
229         exit 1
230         ;;
231 esac
232
233 start_httpd
234 test -z "$browser" && browser=echo
235 $browser http://127.0.0.1:$port