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 case "$httpd_only" in /*) : ;; *) which $httpd_only >/dev/null;; esac
34 then
35 $httpd "$fqgitdir/gitweb/httpd.conf"
36 else
37 # many httpds are installed in /usr/sbin or /usr/local/sbin
38 # these days and those are not in most users $PATHs
39 for i in /usr/local/sbin /usr/sbin
40 do
41 if test -x "$i/$httpd_only"
42 then
43 # don't quote $httpd, there can be
44 # arguments to it (-f)
45 $i/$httpd "$fqgitdir/gitweb/httpd.conf"
46 return
47 fi
48 done
49 echo "$httpd_only not found. Install $httpd_only or use" \
50 "--httpd to specify another http daemon."
51 exit 1
52 fi
53 if test $? != 0; then
54 echo "Could not execute http daemon $httpd."
55 exit 1
56 fi
57}
58
59stop_httpd () {
60 test -f "$fqgitdir/pid" && kill `cat "$fqgitdir/pid"`
61}
62
63while test $# != 0
64do
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
131done
132
133mkdir -p "$GIT_DIR/gitweb/tmp"
134GIT_EXEC_PATH="`git --exec-path`"
135GIT_DIR="$fqgitdir"
136export GIT_EXEC_PATH GIT_DIR
137
138
139lighttpd_conf () {
140 cat > "$conf" <<EOF
141server.document-root = "$fqgitdir/gitweb"
142server.port = $port
143server.modules = ( "mod_cgi" )
144server.indexfiles = ( "gitweb.cgi" )
145server.pid-file = "$fqgitdir/pid"
146cgi.assign = ( ".cgi" => "" )
147mimetype.assign = ( ".css" => "text/css" )
148EOF
149 test x"$local" = xtrue && echo 'server.bind = "127.0.0.1"' >> "$conf"
150}
151
152apache2_conf () {
153 test -z "$module_path" && module_path=/usr/lib/apache2/modules
154 mkdir -p "$GIT_DIR/gitweb/logs"
155 bind=
156 test x"$local" = xtrue && bind='127.0.0.1:'
157 echo 'text/css css' > $fqgitdir/mime.types
158 cat > "$conf" <<EOF
159ServerName "git-instaweb"
160ServerRoot "$fqgitdir/gitweb"
161DocumentRoot "$fqgitdir/gitweb"
162PidFile "$fqgitdir/pid"
163Listen $bind$port
164EOF
165
166 for mod in mime dir; do
167 if test -e $module_path/mod_${mod}.so; then
168 echo "LoadModule ${mod}_module " \
169 "$module_path/mod_${mod}.so" >> "$conf"
170 fi
171 done
172 cat >> "$conf" <<EOF
173TypesConfig $fqgitdir/mime.types
174DirectoryIndex gitweb.cgi
175EOF
176
177 # check to see if Dennis Stosberg's mod_perl compatibility patch
178 # (<20060621130708.Gcbc6e5c@leonov.stosberg.net>) has been applied
179 if test -f "$module_path/mod_perl.so" && grep '^our $gitbin' \
180 "$GIT_DIR/gitweb/gitweb.cgi" >/dev/null
181 then
182 # favor mod_perl if available
183 cat >> "$conf" <<EOF
184LoadModule perl_module $module_path/mod_perl.so
185PerlPassEnv GIT_DIR
186PerlPassEnv GIT_EXEC_DIR
187<Location /gitweb.cgi>
188 SetHandler perl-script
189 PerlResponseHandler ModPerl::Registry
190 PerlOptions +ParseHeaders
191 Options +ExecCGI
192</Location>
193EOF
194 else
195 # plain-old CGI
196 list_mods=`echo "$httpd" | sed "s/-f$/-l/"`
197 $list_mods | grep 'mod_cgi\.c' >/dev/null 2>&1 || \
198 echo "LoadModule cgi_module $module_path/mod_cgi.so" >> "$conf"
199 cat >> "$conf" <<EOF
200AddHandler cgi-script .cgi
201<Location /gitweb.cgi>
202 Options +ExecCGI
203</Location>
204EOF
205 fi
206}
207
208script='
209s#^\(my\|our\) $projectroot =.*#\1 $projectroot = "'$(dirname "$fqgitdir")'";#
210s#\(my\|our\) $gitbin =.*#\1 $gitbin = "'$GIT_EXEC_PATH'";#
211s#\(my\|our\) $projects_list =.*#\1 $projects_list = $projectroot;#
212s#\(my\|our\) $git_temp =.*#\1 $git_temp = "'$fqgitdir/gitweb/tmp'";#'
213
214gitweb_cgi () {
215 cat > "$1.tmp" <<\EOFGITWEB
216@@GITWEB_CGI@@
217EOFGITWEB
218 sed "$script" "$1.tmp" > "$1"
219 chmod +x "$1"
220 rm -f "$1.tmp"
221}
222
223gitweb_css () {
224 cat > "$1" <<\EOFGITWEB
225@@GITWEB_CSS@@
226EOFGITWEB
227}
228
229gitweb_cgi "$GIT_DIR/gitweb/gitweb.cgi"
230gitweb_css "$GIT_DIR/gitweb/gitweb.css"
231
232case "$httpd" in
233*lighttpd*)
234 lighttpd_conf
235 ;;
236*apache2*)
237 apache2_conf
238 ;;
239*)
240 echo "Unknown httpd specified: $httpd"
241 exit 1
242 ;;
243esac
244
245start_httpd
246url=http://127.0.0.1:$port
247"$browser" $url || echo $url