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
11case "$GIT_DIR" in
12/*)
13 fqgitdir="$GIT_DIR" ;;
14*)
15 fqgitdir="$PWD/$GIT_DIR" ;;
16esac
17
18local="`git repo-config --bool --get instaweb.local`"
19httpd="`git repo-config --get instaweb.httpd`"
20browser="`git repo-config --get instaweb.browser`"
21port=`git repo-config --get instaweb.port`
22module_path="`git repo-config --get instaweb.modulepath`"
23
24conf=$GIT_DIR/gitweb/httpd.conf
25
26# Defaults:
27
28# if installed, it doesn't need further configuration (module_path)
29test -z "$httpd" && httpd='lighttpd -f'
30
31# probably the most popular browser among gitweb users
32test -z "$browser" && browser='firefox'
33
34# any untaken local port will do...
35test -z "$port" && port=1234
36
37start_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
59stop_httpd () {
60 test -f "$fqgitdir/pid" && kill `cat "$fqgitdir/pid"`
61}
62
63while case "$#" in 0) break ;; esac
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 "$local" = true && 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 "$local" = true && bind='127.0.0.1:'
157 echo 'text/css css' > $fqgitdir/mime.types
158 cat > "$conf" <<EOF
159ServerRoot "$fqgitdir/gitweb"
160DocumentRoot "$fqgitdir/gitweb"
161PidFile "$fqgitdir/pid"
162Listen $bind$port
163TypesConfig $fqgitdir/mime.types
164DirectoryIndex gitweb.cgi
165EOF
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
174LoadModule perl_module $module_path/mod_perl.so
175PerlPassEnv GIT_DIR
176PerlPassEnv GIT_EXEC_DIR
177<Location /gitweb.cgi>
178 SetHandler perl-script
179 PerlResponseHandler ModPerl::Registry
180 PerlOptions +ParseHeaders
181 Options +ExecCGI
182</Location>
183EOF
184 else
185 # plain-old CGI
186 cat >> "$conf" <<EOF
187LoadModule cgi_module $module_path/mod_cgi.so
188AddHandler cgi-script .cgi
189<Location /gitweb.cgi>
190 Options +ExecCGI
191</Location>
192EOF
193 fi
194}
195
196script='
197s#^\(my\|our\) $projectroot =.*#\1 $projectroot = "'`dirname $fqgitdir`'";#
198s#\(my\|our\) $gitbin =.*#\1 $gitbin = "'$GIT_EXEC_PATH'";#
199s#\(my\|our\) $projects_list =.*#\1 $projects_list = $projectroot;#
200s#\(my\|our\) $git_temp =.*#\1 $git_temp = "'$fqgitdir/gitweb/tmp'";#'
201
202gitweb_cgi () {
203 cat > "$1.tmp" <<\EOFGITWEB
204@@GITWEB_CGI@@
205EOFGITWEB
206 sed "$script" "$1.tmp" > "$1"
207 chmod +x "$1"
208 rm -f "$1.tmp"
209}
210
211gitweb_css () {
212 cat > "$1" <<\EOFGITWEB
213@@GITWEB_CSS@@
214EOFGITWEB
215}
216
217gitweb_cgi $GIT_DIR/gitweb/gitweb.cgi
218gitweb_css $GIT_DIR/gitweb/gitweb.css
219
220case "$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 ;;
231esac
232
233start_httpd
234test -z "$browser" && browser=echo
235$browser http://127.0.0.1:$port