#!/bin/sh
set -e
BASEDIR="$(pwd)"
HTTP_PORT=8000

git init cgit
(
# Initialise test repository
cd cgit
# Solve bootstrap problem of needing the agefile hook before we fetch
# while the agefile hook is part of a later fetch
curl https://git.gitano.org.uk/cgit.git/plain/contrib/hooks/post-receive.agefile?h=richardmaw/namespaces | install -D -m755 /dev/stdin hooks/post-receive

# Add upstream namespace
git symbolic-ref refs/namespaces/upstream/HEAD refs/namespaces/upstream/refs/heads/master
git fetch git://git.zx2c4.com/cgit 'refs/*:refs/namespaces/upstream/refs/*'
# Manually invoke agefile hook since we didn't directly push
GIT_NAMESPACE=upstream hooks/post-receive

# Add gitano namespace
git symbolic-ref refs/namespaces/gitano/HEAD refs/namespaces/gitano/refs/heads/master
git fetch git://git.gitano.org.uk/cgit.git 'refs/*:refs/namespaces/gitano/refs/*'
# Manually invoke agefile hook since we didn't directly push
GIT_NAMESPACE=gitano hooks/post-receive

# Build the namespace supporting version of cgit
git checkout refs/namespaces/gitano/refs/heads/richardmaw/namespaces
git submodule update --init
make
)

# Generate apache config for serving cgit and git-http-backend
install -m755 /dev/stdin runapache.sh <<RUNAPACHE_EOF
#!/bin/sh
BASEDIR="\$(readlink -f "\$(dirname "\$0")")"
GIT_PROJECT_ROOT="\$BASEDIR"
tf="\$(mktemp)"
cat >"\$tf" <<EOF
Listen ${HTTP_PORT}
PidFile "\$BASEDIR/apache.pid"
ErrorLog "/dev/stderr"

# Fails completely if not loaded
LoadModule mpm_prefork_module /usr/lib/apache2/modules/mod_mpm_prefork.so
LoadModule authn_core_module /usr/lib/apache2/modules/mod_authn_core.so
LoadModule authz_core_module /usr/lib/apache2/modules/mod_authz_core.so

LoadModule env_module /usr/lib/apache2/modules/mod_env.so
LoadModule setenvif_module /usr/lib/apache2/modules/mod_setenvif.so

LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so

LoadModule alias_module /usr/lib/apache2/modules/mod_alias.so
LoadModule cgi_module /usr/lib/apache2/modules/mod_cgi.so

<Directory "/">
        AuthType None
        Require all granted
</Directory>

SetEnv GIT_HTTP_EXPORT_ALL
SetEnv GIT_PROJECT_ROOT "\$GIT_PROJECT_ROOT"
DocumentRoot "\$GIT_PROJECT_ROOT"

SetEnvIf Request_URI "^/git/~([^/]+)/" GIT_NAMESPACE=\\\$1

RewriteEngine On

<Directory "/usr/lib/git-core/">
    Options +ExecCGI
</Directory>
RewriteRule \
        "(?x)^/git(/~[^/]+)?(/.*/(HEAD | \
                        info/refs | \
                        objects/(info/[^/]+ | \
                                 [0-9a-f]{2}/[0-9a-f]{38} | \
                                 pack/pack-[0-9a-f]{40}\\\\.(pack|idx)) | \
                        git-(upload|receive)-pack))$" \
        "/usr/lib/git-core/git-http-backend\\\$2" [H=cgi-script]

# Serve cgit from cgit checkout
SetEnv CGIT_CONFIG "\$BASEDIR/cgitrc"
<Directory "\$BASEDIR">
    Options +ExecCGI
</Directory>
ScriptAlias /cgit/ "\$BASEDIR/cgit/cgit/"
Alias / "\$BASEDIR/cgit/"
EOF

/usr/sbin/apache2 -Xf "\$tf" "\$@"

ret="\$?"
rm "\$tf"
exit "\$ret"
RUNAPACHE_EOF

# Generate cgitrc
cat >cgitrc <<CGITRC_EOF
snapshots=tar.gz tar.bz zip
enable-log-filecount=1
enable-log-linecount=1
summary-log=5
summary-branches=5
summary-tags=5
enable-filter-overrides=1
enable-http-clone=1
clone-url=http://localhost:${HTTP_PORT}\$SCRIPT_NAME/\$CGIT_REPO_URL http://localhost:${HTTP_PORT}/git/\$CGIT_REPO_URL

repo.url=~upstream/cgit
repo.path=${BASEDIR}/cgit/.git
repo.namespace=upstream
repo.desc=upstream cgit repo
repo.agefile=info/web/namespaced/upstream/last-modified

repo.url=~gitano/cgit
repo.path=${BASEDIR}/cgit/.git
repo.desc=gitano cgit repo
repo.namespace=gitano
repo.agefile=info/web/namespaced/gitano/last-modified
CGITRC_EOF
