instaweb: allow for use of auto-generated scripts
[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 fqgitdir="$GIT_DIR"
12 local="`git config --bool --get instaweb.local`"
13 httpd="`git config --get instaweb.httpd`"
14 browser="`git config --get instaweb.browser`"
15 port=`git config --get instaweb.port`
16 module_path="`git config --get instaweb.modulepath`"
17
18 conf=$GIT_DIR/gitweb/httpd.conf
19
20 # Defaults:
21
22 # if installed, it doesn't need further configuration (module_path)
23 test -z "$httpd" && httpd='lighttpd -f'
24
25 # probably the most popular browser among gitweb users
26 test -z "$browser" && browser='firefox'
27
28 # any untaken local port will do...
29 test -z "$port" && port=1234
30
31 start_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
62 stop_httpd () {
63         test -f "$fqgitdir/pid" && kill `cat "$fqgitdir/pid"`
64 }
65
66 while test $# != 0
67 do
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
134 done
135
136 mkdir -p "$GIT_DIR/gitweb/tmp"
137 GIT_EXEC_PATH="`git --exec-path`"
138 GIT_DIR="$fqgitdir"
139 export GIT_EXEC_PATH GIT_DIR
140
141
142 lighttpd_conf () {
143         cat > "$conf" <<EOF
144 server.document-root = "$fqgitdir/gitweb"
145 server.port = $port
146 server.modules = ( "mod_cgi" )
147 server.indexfiles = ( "gitweb.cgi" )
148 server.pid-file = "$fqgitdir/pid"
149 cgi.assign = ( ".cgi" => "" )
150 mimetype.assign = ( ".css" => "text/css" )
151 EOF
152         test "$local" = true && echo 'server.bind = "127.0.0.1"' >> "$conf"
153 }
154
155 apache2_conf () {
156         test -z "$module_path" && module_path=/usr/lib/apache2/modules
157         mkdir -p "$GIT_DIR/gitweb/logs"
158         bind=
159         test "$local" = true && bind='127.0.0.1:'
160         echo 'text/css css' > $fqgitdir/mime.types
161         cat > "$conf" <<EOF
162 ServerName "git-instaweb"
163 ServerRoot "$fqgitdir/gitweb"
164 DocumentRoot "$fqgitdir/gitweb"
165 PidFile "$fqgitdir/pid"
166 Listen $bind$port
167 EOF
168
169         for mod in mime dir; do
170                 if test -e $module_path/mod_${mod}.so; then
171                         echo "LoadModule ${mod}_module " \
172                              "$module_path/mod_${mod}.so" >> "$conf"
173                 fi
174         done
175         cat >> "$conf" <<EOF
176 TypesConfig $fqgitdir/mime.types
177 DirectoryIndex gitweb.cgi
178 EOF
179
180         # check to see if Dennis Stosberg's mod_perl compatibility patch
181         # (<20060621130708.Gcbc6e5c@leonov.stosberg.net>) has been applied
182         if test -f "$module_path/mod_perl.so" && grep '^our $gitbin' \
183                                 "$GIT_DIR/gitweb/gitweb.cgi" >/dev/null
184         then
185                 # favor mod_perl if available
186                 cat >> "$conf" <<EOF
187 LoadModule perl_module $module_path/mod_perl.so
188 PerlPassEnv GIT_DIR
189 PerlPassEnv GIT_EXEC_DIR
190 <Location /gitweb.cgi>
191         SetHandler perl-script
192         PerlResponseHandler ModPerl::Registry
193         PerlOptions +ParseHeaders
194         Options +ExecCGI
195 </Location>
196 EOF
197         else
198                 # plain-old CGI
199                 list_mods=`echo "$httpd" | sed "s/-f$/-l/"`
200                 $list_mods | grep 'mod_cgi\.c' >/dev/null 2>&1 || \
201                 echo "LoadModule cgi_module $module_path/mod_cgi.so" >> "$conf"
202                 cat >> "$conf" <<EOF
203 AddHandler cgi-script .cgi
204 <Location /gitweb.cgi>
205         Options +ExecCGI
206 </Location>
207 EOF
208         fi
209 }
210
211 script='
212 s#^\(my\|our\) $projectroot =.*#\1 $projectroot = "'`dirname $fqgitdir`'";#
213 s#\(my\|our\) $gitbin =.*#\1 $gitbin = "'$GIT_EXEC_PATH'";#
214 s#\(my\|our\) $projects_list =.*#\1 $projects_list = $projectroot;#
215 s#\(my\|our\) $git_temp =.*#\1 $git_temp = "'$fqgitdir/gitweb/tmp'";#'
216
217 gitweb_cgi () {
218         cat > "$1.tmp" <<\EOFGITWEB
219 @@GITWEB_CGI@@
220 EOFGITWEB
221         sed "$script" "$1.tmp"  > "$1"
222         chmod +x "$1"
223         rm -f "$1.tmp"
224 }
225
226 gitweb_css () {
227         cat > "$1" <<\EOFGITWEB
228 @@GITWEB_CSS@@
229 EOFGITWEB
230 }
231
232 gitweb_cgi $GIT_DIR/gitweb/gitweb.cgi
233 gitweb_css $GIT_DIR/gitweb/gitweb.css
234
235 case "$httpd" in
236 *lighttpd*)
237         lighttpd_conf
238         ;;
239 *apache2*)
240         apache2_conf
241         ;;
242 *)
243         echo "Unknown httpd specified: $httpd"
244         exit 1
245         ;;
246 esac
247
248 start_httpd
249 test -z "$browser" && browser=echo
250 url=http://127.0.0.1:$port
251 $browser $url || echo $url