git-instaweb.shon commit instaweb: load Apache mime and dir modules if they are needed (44a167b)
   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        if test $? != 0; then
  58                echo "Could not execute http daemon $httpd."
  59                exit 1
  60        fi
  61}
  62
  63stop_httpd () {
  64        test -f "$fqgitdir/pid" && kill `cat "$fqgitdir/pid"`
  65}
  66
  67while case "$#" in 0) break ;; esac
  68do
  69        case "$1" in
  70        --stop|stop)
  71                stop_httpd
  72                exit 0
  73                ;;
  74        --start|start)
  75                start_httpd
  76                exit 0
  77                ;;
  78        --restart|restart)
  79                stop_httpd
  80                start_httpd
  81                exit 0
  82                ;;
  83        --local|-l)
  84                local=true
  85                ;;
  86        -d|--httpd|--httpd=*)
  87                case "$#,$1" in
  88                *,*=*)
  89                        httpd=`expr "$1" : '-[^=]*=\(.*\)'` ;;
  90                1,*)
  91                        usage ;;
  92                *)
  93                        httpd="$2"
  94                        shift ;;
  95                esac
  96                ;;
  97        -b|--browser|--browser=*)
  98                case "$#,$1" in
  99                *,*=*)
 100                        browser=`expr "$1" : '-[^=]*=\(.*\)'` ;;
 101                1,*)
 102                        usage ;;
 103                *)
 104                        browser="$2"
 105                        shift ;;
 106                esac
 107                ;;
 108        -p|--port|--port=*)
 109                case "$#,$1" in
 110                *,*=*)
 111                        port=`expr "$1" : '-[^=]*=\(.*\)'` ;;
 112                1,*)
 113                        usage ;;
 114                *)
 115                        port="$2"
 116                        shift ;;
 117                esac
 118                ;;
 119        -m|--module-path=*|--module-path)
 120                case "$#,$1" in
 121                *,*=*)
 122                        module_path=`expr "$1" : '-[^=]*=\(.*\)'` ;;
 123                1,*)
 124                        usage ;;
 125                *)
 126                        module_path="$2"
 127                        shift ;;
 128                esac
 129                ;;
 130        *)
 131                usage
 132                ;;
 133        esac
 134        shift
 135done
 136
 137mkdir -p "$GIT_DIR/gitweb/tmp"
 138GIT_EXEC_PATH="`git --exec-path`"
 139GIT_DIR="$fqgitdir"
 140export GIT_EXEC_PATH GIT_DIR
 141
 142
 143lighttpd_conf () {
 144        cat > "$conf" <<EOF
 145server.document-root = "$fqgitdir/gitweb"
 146server.port = $port
 147server.modules = ( "mod_cgi" )
 148server.indexfiles = ( "gitweb.cgi" )
 149server.pid-file = "$fqgitdir/pid"
 150cgi.assign = ( ".cgi" => "" )
 151mimetype.assign = ( ".css" => "text/css" )
 152EOF
 153        test "$local" = true && echo 'server.bind = "127.0.0.1"' >> "$conf"
 154}
 155
 156apache2_conf () {
 157        test -z "$module_path" && module_path=/usr/lib/apache2/modules
 158        mkdir -p "$GIT_DIR/gitweb/logs"
 159        bind=
 160        test "$local" = true && bind='127.0.0.1:'
 161        echo 'text/css css' > $fqgitdir/mime.types
 162        cat > "$conf" <<EOF
 163ServerName "git-instaweb"
 164ServerRoot "$fqgitdir/gitweb"
 165DocumentRoot "$fqgitdir/gitweb"
 166PidFile "$fqgitdir/pid"
 167Listen $bind$port
 168EOF
 169
 170        for mod in mime dir; do
 171                if test -e $module_path/mod_${mod}.so; then
 172                        echo "LoadModule ${mod}_module " \
 173                             "$module_path/mod_${mod}.so" >> "$conf"
 174                fi
 175        done
 176        cat >> "$conf" <<EOF
 177TypesConfig $fqgitdir/mime.types
 178DirectoryIndex gitweb.cgi
 179EOF
 180
 181        # check to see if Dennis Stosberg's mod_perl compatibility patch
 182        # (<20060621130708.Gcbc6e5c@leonov.stosberg.net>) has been applied
 183        if test -f "$module_path/mod_perl.so" && grep '^our $gitbin' \
 184                                "$GIT_DIR/gitweb/gitweb.cgi" >/dev/null
 185        then
 186                # favor mod_perl if available
 187                cat >> "$conf" <<EOF
 188LoadModule perl_module $module_path/mod_perl.so
 189PerlPassEnv GIT_DIR
 190PerlPassEnv GIT_EXEC_DIR
 191<Location /gitweb.cgi>
 192        SetHandler perl-script
 193        PerlResponseHandler ModPerl::Registry
 194        PerlOptions +ParseHeaders
 195        Options +ExecCGI
 196</Location>
 197EOF
 198        else
 199                # plain-old CGI
 200                list_mods=`echo "$httpd" | sed "s/-f$/-l/"`
 201                $list_mods | grep 'mod_cgi\.c' >/dev/null 2>&1 || \
 202                echo "LoadModule cgi_module $module_path/mod_cgi.so" >> "$conf"
 203                cat >> "$conf" <<EOF
 204AddHandler cgi-script .cgi
 205<Location /gitweb.cgi>
 206        Options +ExecCGI
 207</Location>
 208EOF
 209        fi
 210}
 211
 212script='
 213s#^\(my\|our\) $projectroot =.*#\1 $projectroot = "'`dirname $fqgitdir`'";#
 214s#\(my\|our\) $gitbin =.*#\1 $gitbin = "'$GIT_EXEC_PATH'";#
 215s#\(my\|our\) $projects_list =.*#\1 $projects_list = $projectroot;#
 216s#\(my\|our\) $git_temp =.*#\1 $git_temp = "'$fqgitdir/gitweb/tmp'";#'
 217
 218gitweb_cgi () {
 219        cat > "$1.tmp" <<\EOFGITWEB
 220@@GITWEB_CGI@@
 221EOFGITWEB
 222        sed "$script" "$1.tmp"  > "$1"
 223        chmod +x "$1"
 224        rm -f "$1.tmp"
 225}
 226
 227gitweb_css () {
 228        cat > "$1" <<\EOFGITWEB
 229@@GITWEB_CSS@@
 230EOFGITWEB
 231}
 232
 233gitweb_cgi $GIT_DIR/gitweb/gitweb.cgi
 234gitweb_css $GIT_DIR/gitweb/gitweb.css
 235
 236case "$httpd" in
 237*lighttpd*)
 238        lighttpd_conf
 239        ;;
 240*apache2*)
 241        apache2_conf
 242        ;;
 243*)
 244        echo "Unknown httpd specified: $httpd"
 245        exit 1
 246        ;;
 247esac
 248
 249start_httpd
 250test -z "$browser" && browser=echo
 251url=http://127.0.0.1:$port
 252$browser $url || echo $url