git-instaweb.shon commit typo fix: Directory `...' exist, ...: s/exist/exists/ (0484682)
   1#!/bin/sh
   2#
   3# Copyright (c) 2006 Eric Wong
   4#
   5
   6PERL='@@PERL@@'
   7OPTIONS_KEEPDASHDASH=
   8OPTIONS_SPEC="\
   9git instaweb [options] (--start | --stop | --restart)
  10--
  11l,local        only bind on 127.0.0.1
  12p,port=        the port to bind to
  13d,httpd=       the command to launch
  14b,browser=     the browser to launch
  15m,module-path= the module path (only needed for apache2)
  16 Action
  17stop           stop the web server
  18start          start the web server
  19restart        restart the web server
  20"
  21
  22. git-sh-setup
  23
  24fqgitdir="$GIT_DIR"
  25local="$(git config --bool --get instaweb.local)"
  26httpd="$(git config --get instaweb.httpd)"
  27port=$(git config --get instaweb.port)
  28module_path="$(git config --get instaweb.modulepath)"
  29
  30conf="$GIT_DIR/gitweb/httpd.conf"
  31
  32# Defaults:
  33
  34# if installed, it doesn't need further configuration (module_path)
  35test -z "$httpd" && httpd='lighttpd -f'
  36
  37# any untaken local port will do...
  38test -z "$port" && port=1234
  39
  40resolve_full_httpd () {
  41        case "$httpd" in
  42        *apache2*|*lighttpd*)
  43                # ensure that the apache2/lighttpd command ends with "-f"
  44                if ! echo "$httpd" | grep -- '-f *$' >/dev/null 2>&1
  45                then
  46                        httpd="$httpd -f"
  47                fi
  48                ;;
  49        esac
  50
  51        httpd_only="$(echo $httpd | cut -f1 -d' ')"
  52        if case "$httpd_only" in /*) : ;; *) which $httpd_only >/dev/null 2>&1;; esac
  53        then
  54                full_httpd=$httpd
  55        else
  56                # many httpds are installed in /usr/sbin or /usr/local/sbin
  57                # these days and those are not in most users $PATHs
  58                # in addition, we may have generated a server script
  59                # in $fqgitdir/gitweb.
  60                for i in /usr/local/sbin /usr/sbin "$fqgitdir/gitweb"
  61                do
  62                        if test -x "$i/$httpd_only"
  63                        then
  64                                full_httpd=$i/$httpd
  65                                return
  66                        fi
  67                done
  68
  69                echo >&2 "$httpd_only not found. Install $httpd_only or use" \
  70                     "--httpd to specify another httpd daemon."
  71                exit 1
  72        fi
  73}
  74
  75start_httpd () {
  76        # here $httpd should have a meaningful value
  77        resolve_full_httpd
  78
  79        # don't quote $full_httpd, there can be arguments to it (-f)
  80        case "$httpd" in
  81        *mongoose*)
  82                #The mongoose server doesn't have a daemon mode so we'll have to fork it
  83                $full_httpd "$fqgitdir/gitweb/httpd.conf" &
  84                #Save the pid before doing anything else (we'll print it later)
  85                pid=$!
  86
  87                if test $? != 0; then
  88                        echo "Could not execute http daemon $httpd."
  89                        exit 1
  90                fi
  91
  92                cat > "$fqgitdir/pid" <<EOF
  93$pid
  94EOF
  95                ;;
  96        *)
  97                $full_httpd "$fqgitdir/gitweb/httpd.conf"
  98                if test $? != 0; then
  99                        echo "Could not execute http daemon $httpd."
 100                        exit 1
 101                fi
 102                ;;
 103        esac
 104}
 105
 106stop_httpd () {
 107        test -f "$fqgitdir/pid" && kill $(cat "$fqgitdir/pid")
 108}
 109
 110while test $# != 0
 111do
 112        case "$1" in
 113        --stop|stop)
 114                stop_httpd
 115                exit 0
 116                ;;
 117        --start|start)
 118                start_httpd
 119                exit 0
 120                ;;
 121        --restart|restart)
 122                stop_httpd
 123                start_httpd
 124                exit 0
 125                ;;
 126        -l|--local)
 127                local=true
 128                ;;
 129        -d|--httpd)
 130                shift
 131                httpd="$1"
 132                ;;
 133        -b|--browser)
 134                shift
 135                browser="$1"
 136                ;;
 137        -p|--port)
 138                shift
 139                port="$1"
 140                ;;
 141        -m|--module-path)
 142                shift
 143                module_path="$1"
 144                ;;
 145        --)
 146                ;;
 147        *)
 148                usage
 149                ;;
 150        esac
 151        shift
 152done
 153
 154mkdir -p "$GIT_DIR/gitweb/tmp"
 155GIT_EXEC_PATH="$(git --exec-path)"
 156GIT_DIR="$fqgitdir"
 157export GIT_EXEC_PATH GIT_DIR
 158
 159
 160webrick_conf () {
 161        # generate a standalone server script in $fqgitdir/gitweb.
 162        cat >"$fqgitdir/gitweb/$httpd.rb" <<EOF
 163require 'webrick'
 164require 'yaml'
 165options = YAML::load_file(ARGV[0])
 166options[:StartCallback] = proc do
 167  File.open(options[:PidFile],"w") do |f|
 168    f.puts Process.pid
 169  end
 170end
 171options[:ServerType] = WEBrick::Daemon
 172server = WEBrick::HTTPServer.new(options)
 173['INT', 'TERM'].each do |signal|
 174  trap(signal) {server.shutdown}
 175end
 176server.start
 177EOF
 178        # generate a shell script to invoke the above ruby script,
 179        # which assumes _ruby_ is in the user's $PATH. that's _one_
 180        # portable way to run ruby, which could be installed anywhere,
 181        # really.
 182        cat >"$fqgitdir/gitweb/$httpd" <<EOF
 183#!/bin/sh
 184exec ruby "$fqgitdir/gitweb/$httpd.rb" \$*
 185EOF
 186        chmod +x "$fqgitdir/gitweb/$httpd"
 187
 188        cat >"$conf" <<EOF
 189:Port: $port
 190:DocumentRoot: "$fqgitdir/gitweb"
 191:DirectoryIndex: ["gitweb.cgi"]
 192:PidFile: "$fqgitdir/pid"
 193EOF
 194        test "$local" = true && echo ':BindAddress: "127.0.0.1"' >> "$conf"
 195}
 196
 197lighttpd_conf () {
 198        cat > "$conf" <<EOF
 199server.document-root = "$fqgitdir/gitweb"
 200server.port = $port
 201server.modules = ( "mod_setenv", "mod_cgi" )
 202server.indexfiles = ( "gitweb.cgi" )
 203server.pid-file = "$fqgitdir/pid"
 204server.errorlog = "$fqgitdir/gitweb/error.log"
 205
 206# to enable, add "mod_access", "mod_accesslog" to server.modules
 207# variable above and uncomment this
 208#accesslog.filename = "$fqgitdir/gitweb/access.log"
 209
 210setenv.add-environment = ( "PATH" => "/usr/local/bin:/usr/bin:/bin" )
 211
 212cgi.assign = ( ".cgi" => "" )
 213
 214# mimetype mapping
 215mimetype.assign             = (
 216  ".pdf"          =>      "application/pdf",
 217  ".sig"          =>      "application/pgp-signature",
 218  ".spl"          =>      "application/futuresplash",
 219  ".class"        =>      "application/octet-stream",
 220  ".ps"           =>      "application/postscript",
 221  ".torrent"      =>      "application/x-bittorrent",
 222  ".dvi"          =>      "application/x-dvi",
 223  ".gz"           =>      "application/x-gzip",
 224  ".pac"          =>      "application/x-ns-proxy-autoconfig",
 225  ".swf"          =>      "application/x-shockwave-flash",
 226  ".tar.gz"       =>      "application/x-tgz",
 227  ".tgz"          =>      "application/x-tgz",
 228  ".tar"          =>      "application/x-tar",
 229  ".zip"          =>      "application/zip",
 230  ".mp3"          =>      "audio/mpeg",
 231  ".m3u"          =>      "audio/x-mpegurl",
 232  ".wma"          =>      "audio/x-ms-wma",
 233  ".wax"          =>      "audio/x-ms-wax",
 234  ".ogg"          =>      "application/ogg",
 235  ".wav"          =>      "audio/x-wav",
 236  ".gif"          =>      "image/gif",
 237  ".jpg"          =>      "image/jpeg",
 238  ".jpeg"         =>      "image/jpeg",
 239  ".png"          =>      "image/png",
 240  ".xbm"          =>      "image/x-xbitmap",
 241  ".xpm"          =>      "image/x-xpixmap",
 242  ".xwd"          =>      "image/x-xwindowdump",
 243  ".css"          =>      "text/css",
 244  ".html"         =>      "text/html",
 245  ".htm"          =>      "text/html",
 246  ".js"           =>      "text/javascript",
 247  ".asc"          =>      "text/plain",
 248  ".c"            =>      "text/plain",
 249  ".cpp"          =>      "text/plain",
 250  ".log"          =>      "text/plain",
 251  ".conf"         =>      "text/plain",
 252  ".text"         =>      "text/plain",
 253  ".txt"          =>      "text/plain",
 254  ".dtd"          =>      "text/xml",
 255  ".xml"          =>      "text/xml",
 256  ".mpeg"         =>      "video/mpeg",
 257  ".mpg"          =>      "video/mpeg",
 258  ".mov"          =>      "video/quicktime",
 259  ".qt"           =>      "video/quicktime",
 260  ".avi"          =>      "video/x-msvideo",
 261  ".asf"          =>      "video/x-ms-asf",
 262  ".asx"          =>      "video/x-ms-asf",
 263  ".wmv"          =>      "video/x-ms-wmv",
 264  ".bz2"          =>      "application/x-bzip",
 265  ".tbz"          =>      "application/x-bzip-compressed-tar",
 266  ".tar.bz2"      =>      "application/x-bzip-compressed-tar",
 267  ""              =>      "text/plain"
 268 )
 269EOF
 270        test x"$local" = xtrue && echo 'server.bind = "127.0.0.1"' >> "$conf"
 271}
 272
 273apache2_conf () {
 274        test -z "$module_path" && module_path=/usr/lib/apache2/modules
 275        mkdir -p "$GIT_DIR/gitweb/logs"
 276        bind=
 277        test x"$local" = xtrue && bind='127.0.0.1:'
 278        echo 'text/css css' > "$fqgitdir/mime.types"
 279        cat > "$conf" <<EOF
 280ServerName "git-instaweb"
 281ServerRoot "$fqgitdir/gitweb"
 282DocumentRoot "$fqgitdir/gitweb"
 283PidFile "$fqgitdir/pid"
 284Listen $bind$port
 285EOF
 286
 287        for mod in mime dir; do
 288                if test -e $module_path/mod_${mod}.so; then
 289                        echo "LoadModule ${mod}_module " \
 290                             "$module_path/mod_${mod}.so" >> "$conf"
 291                fi
 292        done
 293        cat >> "$conf" <<EOF
 294TypesConfig "$fqgitdir/mime.types"
 295DirectoryIndex gitweb.cgi
 296EOF
 297
 298        # check to see if Dennis Stosberg's mod_perl compatibility patch
 299        # (<20060621130708.Gcbc6e5c@leonov.stosberg.net>) has been applied
 300        if test -f "$module_path/mod_perl.so" && grep 'MOD_PERL' \
 301                                "$GIT_DIR/gitweb/gitweb.cgi" >/dev/null
 302        then
 303                # favor mod_perl if available
 304                cat >> "$conf" <<EOF
 305LoadModule perl_module $module_path/mod_perl.so
 306PerlPassEnv GIT_DIR
 307PerlPassEnv GIT_EXEC_DIR
 308<Location /gitweb.cgi>
 309        SetHandler perl-script
 310        PerlResponseHandler ModPerl::Registry
 311        PerlOptions +ParseHeaders
 312        Options +ExecCGI
 313</Location>
 314EOF
 315        else
 316                # plain-old CGI
 317                resolve_full_httpd
 318                list_mods=$(echo "$full_httpd" | sed "s/-f$/-l/")
 319                $list_mods | grep 'mod_cgi\.c' >/dev/null 2>&1 || \
 320                echo "LoadModule cgi_module $module_path/mod_cgi.so" >> "$conf"
 321                cat >> "$conf" <<EOF
 322AddHandler cgi-script .cgi
 323<Location /gitweb.cgi>
 324        Options +ExecCGI
 325</Location>
 326EOF
 327        fi
 328}
 329
 330mongoose_conf() {
 331        cat > "$conf" <<EOF
 332# Mongoose web server configuration file.
 333# Lines starting with '#' and empty lines are ignored.
 334# For detailed description of every option, visit
 335# http://code.google.com/p/mongoose/wiki/MongooseManual
 336
 337root            $fqgitdir/gitweb
 338ports           $port
 339index_files     gitweb.cgi
 340#ssl_cert       $fqgitdir/gitweb/ssl_cert.pem
 341error_log       $fqgitdir/gitweb/error.log
 342access_log      $fqgitdir/gitweb/access.log
 343
 344#cgi setup
 345cgi_env         PATH=/usr/local/bin:/usr/bin:/bin,GIT_DIR=$GIT_DIR,GIT_EXEC_PATH=$GIT_EXEC_PATH
 346cgi_interp      $PERL
 347cgi_ext         cgi,pl
 348
 349# mimetype mapping
 350mime_types      .gz=application/x-gzip,.tar.gz=application/x-tgz,.tgz=application/x-tgz,.tar=application/x-tar,.zip=application/zip,.gif=image/gif,.jpg=image/jpeg,.jpeg=image/jpeg,.png=image/png,.css=text/css,.html=text/html,.htm=text/html,.js=text/javascript,.c=text/plain,.cpp=text/plain,.log=text/plain,.conf=text/plain,.text=text/plain,.txt=text/plain,.dtd=text/xml,.bz2=application/x-bzip,.tbz=application/x-bzip-compressed-tar,.tar.bz2=application/x-bzip-compressed-tar
 351EOF
 352}
 353
 354
 355script='
 356s#^(my|our) \$projectroot =.*#$1 \$projectroot = "'$(dirname "$fqgitdir")'";#;
 357s#(my|our) \$gitbin =.*#$1 \$gitbin = "'$GIT_EXEC_PATH'";#;
 358s#(my|our) \$projects_list =.*#$1 \$projects_list = \$projectroot;#;
 359s#(my|our) \$git_temp =.*#$1 \$git_temp = "'$fqgitdir/gitweb/tmp'";#;'
 360
 361gitweb_cgi () {
 362        cat > "$1.tmp" <<\EOFGITWEB
 363@@GITWEB_CGI@@
 364EOFGITWEB
 365        # Use the configured full path to perl to match the generated
 366        # scripts' 'hashpling' line
 367        "$PERL" -p -e "$script" "$1.tmp"  > "$1"
 368        chmod +x "$1"
 369        rm -f "$1.tmp"
 370}
 371
 372gitweb_css () {
 373        cat > "$1" <<\EOFGITWEB
 374@@GITWEB_CSS@@
 375EOFGITWEB
 376}
 377
 378gitweb_cgi "$GIT_DIR/gitweb/gitweb.cgi"
 379gitweb_css "$GIT_DIR/gitweb/gitweb.css"
 380
 381case "$httpd" in
 382*lighttpd*)
 383        lighttpd_conf
 384        ;;
 385*apache2*)
 386        apache2_conf
 387        ;;
 388webrick)
 389        webrick_conf
 390        ;;
 391*mongoose*)
 392        mongoose_conf
 393        ;;
 394*)
 395        echo "Unknown httpd specified: $httpd"
 396        exit 1
 397        ;;
 398esac
 399
 400start_httpd
 401url=http://127.0.0.1:$port
 402
 403if test -n "$browser"; then
 404        git web--browse -b "$browser" $url || echo $url
 405else
 406        git web--browse -c "instaweb.browser" $url || echo $url
 407fi