1#!/bin/sh
2#
3# Copyright (c) 2006 Eric Wong
4#
5USAGE='[--start] [--stop] [--restart]
6 [--local] [--httpd=<httpd>] [--port=<port>] [--browser=<browser>]
7 [--module-path=<path> (for Apache2 only)]'
8
9. git-sh-setup
10
11fqgitdir="$GIT_DIR"
12local="`git config --bool --get instaweb.local`"
13httpd="`git config --get instaweb.httpd`"
14browser="`git config --get instaweb.browser`"
15port=`git config --get instaweb.port`
16module_path="`git config --get instaweb.modulepath`"
17
18conf=$GIT_DIR/gitweb/httpd.conf
19
20# Defaults:
21
22# if installed, it doesn't need further configuration (module_path)
23test -z "$httpd" && httpd='lighttpd -f'
24
25# probably the most popular browser among gitweb users
26test -z "$browser" && browser='firefox'
27
28# any untaken local port will do...
29test -z "$port" && port=1234
30
31start_httpd () {
32 httpd_only="`echo $httpd | cut -f1 -d' '`"
33 if test "`expr index $httpd_only /`" -eq '1' || \
34 which $httpd_only >/dev/null
35 then
36 $httpd $fqgitdir/gitweb/httpd.conf
37 else
38 # many httpds are installed in /usr/sbin or /usr/local/sbin
39 # these days and those are not in most users $PATHs
40 # in addition, we may have generated a server script
41 # in $fqgitdir/gitweb.
42 for i in /usr/local/sbin /usr/sbin "$fqgitdir/gitweb"
43 do
44 if test -x "$i/$httpd_only"
45 then
46 # don't quote $httpd, there can be
47 # arguments to it (-f)
48 $i/$httpd "$fqgitdir/gitweb/httpd.conf"
49 return
50 fi
51 done
52 echo "$httpd_only not found. Install $httpd_only or use" \
53 "--httpd to specify another http daemon."
54 exit 1
55 fi
56 if test $? != 0; then
57 echo "Could not execute http daemon $httpd."
58 exit 1
59 fi
60}
61
62stop_httpd () {
63 test -f "$fqgitdir/pid" && kill `cat "$fqgitdir/pid"`
64}
65
66while test $# != 0
67do
68 case "$1" in
69 --stop|stop)
70 stop_httpd
71 exit 0
72 ;;
73 --start|start)
74 start_httpd
75 exit 0
76 ;;
77 --restart|restart)
78 stop_httpd
79 start_httpd
80 exit 0
81 ;;
82 --local|-l)
83 local=true
84 ;;
85 -d|--httpd|--httpd=*)
86 case "$#,$1" in
87 *,*=*)
88 httpd=`expr "$1" : '-[^=]*=\(.*\)'` ;;
89 1,*)
90 usage ;;
91 *)
92 httpd="$2"
93 shift ;;
94 esac
95 ;;
96 -b|--browser|--browser=*)
97 case "$#,$1" in
98 *,*=*)
99 browser=`expr "$1" : '-[^=]*=\(.*\)'` ;;
100 1,*)
101 usage ;;
102 *)
103 browser="$2"
104 shift ;;
105 esac
106 ;;
107 -p|--port|--port=*)
108 case "$#,$1" in
109 *,*=*)
110 port=`expr "$1" : '-[^=]*=\(.*\)'` ;;
111 1,*)
112 usage ;;
113 *)
114 port="$2"
115 shift ;;
116 esac
117 ;;
118 -m|--module-path=*|--module-path)
119 case "$#,$1" in
120 *,*=*)
121 module_path=`expr "$1" : '-[^=]*=\(.*\)'` ;;
122 1,*)
123 usage ;;
124 *)
125 module_path="$2"
126 shift ;;
127 esac
128 ;;
129 *)
130 usage
131 ;;
132 esac
133 shift
134done
135
136mkdir -p "$GIT_DIR/gitweb/tmp"
137GIT_EXEC_PATH="`git --exec-path`"
138GIT_DIR="$fqgitdir"
139export GIT_EXEC_PATH GIT_DIR
140
141
142webrick_conf () {
143 # generate a standalone server script in $fqgitdir/gitweb.
144 cat >"$fqgitdir/gitweb/$httpd.rb" <<EOF
145require 'webrick'
146require 'yaml'
147options = YAML::load_file(ARGV[0])
148options[:StartCallback] = proc do
149 File.open(options[:PidFile],"w") do |f|
150 f.puts Process.pid
151 end
152end
153options[:ServerType] = WEBrick::Daemon
154server = WEBrick::HTTPServer.new(options)
155['INT', 'TERM'].each do |signal|
156 trap(signal) {server.shutdown}
157end
158server.start
159EOF
160 # generate a shell script to invoke the above ruby script,
161 # which assumes _ruby_ is in the user's $PATH. that's _one_
162 # portable way to run ruby, which could be installed anywhere,
163 # really.
164 cat >"$fqgitdir/gitweb/$httpd" <<EOF
165#!/bin/sh
166exec ruby "$fqgitdir/gitweb/$httpd.rb" \$*
167EOF
168 chmod +x "$fqgitdir/gitweb/$httpd"
169
170 cat >"$conf" <<EOF
171:Port: $port
172:DocumentRoot: "$fqgitdir/gitweb"
173:DirectoryIndex: ["gitweb.cgi"]
174:PidFile: "$fqgitdir/pid"
175EOF
176 test "$local" = true && echo ':BindAddress: "127.0.0.1"' >> "$conf"
177}
178
179lighttpd_conf () {
180 cat > "$conf" <<EOF
181server.document-root = "$fqgitdir/gitweb"
182server.port = $port
183server.modules = ( "mod_cgi" )
184server.indexfiles = ( "gitweb.cgi" )
185server.pid-file = "$fqgitdir/pid"
186cgi.assign = ( ".cgi" => "" )
187mimetype.assign = ( ".css" => "text/css" )
188EOF
189 test "$local" = true && echo 'server.bind = "127.0.0.1"' >> "$conf"
190}
191
192apache2_conf () {
193 test -z "$module_path" && module_path=/usr/lib/apache2/modules
194 mkdir -p "$GIT_DIR/gitweb/logs"
195 bind=
196 test "$local" = true && bind='127.0.0.1:'
197 echo 'text/css css' > $fqgitdir/mime.types
198 cat > "$conf" <<EOF
199ServerName "git-instaweb"
200ServerRoot "$fqgitdir/gitweb"
201DocumentRoot "$fqgitdir/gitweb"
202PidFile "$fqgitdir/pid"
203Listen $bind$port
204EOF
205
206 for mod in mime dir; do
207 if test -e $module_path/mod_${mod}.so; then
208 echo "LoadModule ${mod}_module " \
209 "$module_path/mod_${mod}.so" >> "$conf"
210 fi
211 done
212 cat >> "$conf" <<EOF
213TypesConfig $fqgitdir/mime.types
214DirectoryIndex gitweb.cgi
215EOF
216
217 # check to see if Dennis Stosberg's mod_perl compatibility patch
218 # (<20060621130708.Gcbc6e5c@leonov.stosberg.net>) has been applied
219 if test -f "$module_path/mod_perl.so" && grep '^our $gitbin' \
220 "$GIT_DIR/gitweb/gitweb.cgi" >/dev/null
221 then
222 # favor mod_perl if available
223 cat >> "$conf" <<EOF
224LoadModule perl_module $module_path/mod_perl.so
225PerlPassEnv GIT_DIR
226PerlPassEnv GIT_EXEC_DIR
227<Location /gitweb.cgi>
228 SetHandler perl-script
229 PerlResponseHandler ModPerl::Registry
230 PerlOptions +ParseHeaders
231 Options +ExecCGI
232</Location>
233EOF
234 else
235 # plain-old CGI
236 list_mods=`echo "$httpd" | sed "s/-f$/-l/"`
237 $list_mods | grep 'mod_cgi\.c' >/dev/null 2>&1 || \
238 echo "LoadModule cgi_module $module_path/mod_cgi.so" >> "$conf"
239 cat >> "$conf" <<EOF
240AddHandler cgi-script .cgi
241<Location /gitweb.cgi>
242 Options +ExecCGI
243</Location>
244EOF
245 fi
246}
247
248script='
249s#^\(my\|our\) $projectroot =.*#\1 $projectroot = "'`dirname $fqgitdir`'";#
250s#\(my\|our\) $gitbin =.*#\1 $gitbin = "'$GIT_EXEC_PATH'";#
251s#\(my\|our\) $projects_list =.*#\1 $projects_list = $projectroot;#
252s#\(my\|our\) $git_temp =.*#\1 $git_temp = "'$fqgitdir/gitweb/tmp'";#'
253
254gitweb_cgi () {
255 cat > "$1.tmp" <<\EOFGITWEB
256@@GITWEB_CGI@@
257EOFGITWEB
258 sed "$script" "$1.tmp" > "$1"
259 chmod +x "$1"
260 rm -f "$1.tmp"
261}
262
263gitweb_css () {
264 cat > "$1" <<\EOFGITWEB
265@@GITWEB_CSS@@
266EOFGITWEB
267}
268
269gitweb_cgi $GIT_DIR/gitweb/gitweb.cgi
270gitweb_css $GIT_DIR/gitweb/gitweb.css
271
272case "$httpd" in
273*lighttpd*)
274 lighttpd_conf
275 ;;
276*apache2*)
277 apache2_conf
278 ;;
279webrick)
280 webrick_conf
281 ;;
282*)
283 echo "Unknown httpd specified: $httpd"
284 exit 1
285 ;;
286esac
287
288start_httpd
289test -z "$browser" && browser=echo
290url=http://127.0.0.1:$port
291$browser $url || echo $url