#!/usr/bin/env sh

# name of remote sample repository to apply
REMOTE_REPO_NAME='sample.git'
# origin of sample repository to use; will bare clone
REMOTE_REPO_ORIGIN='https://github.com/VaasuDevanS/cowsay-python.git'
# docker-compose service name
DOCKERC_SERVICE_NAME='git-httpd'
# no spaces allowed, in addition to $, ideally RFC2616 quoted string
export REMOTE_USERNAME='sample-user'
# no spaces allowed, in addition to $, ideally RFC2616 quoted string
export REMOTE_PASSWORD='sample-password'

LOCAL_IP_PORT='8080'

export REPO_URL='http://$REMOTE_USERNAME:$REMOTE_PASSWORD@'"$(hostname -i):$LOCAL_IP_PORT/$REMOTE_REPO_NAME"

echo "$REPO_URL"

# check for any OCI-compatible executable
export DOCKER=docker
command -v docker 1>/dev/null
test $? -ne 0 && {
    command -v podman 1>/dev/null
    if test $? -ne 0; then
        echo "neither docker, nor podman found. exiting..." >&2
        exit 1;
    else
        export DOCKER='podman'
    fi
}

# check for any Compose-spec compatible executable
export DOCKER_COMPOSE=docker-compose
command -v docker-compose 1>/dev/null
test $? -ne 0 && {
    command -v podman-compose 1>/dev/null
    if test $? -ne 0; then
        echo "neither docker-compose, nor podman-compose found. exiting..." >&2
        exit 1;
    else
        export DOCKER_COMPOSE='podman-compose'
    fi
}

# dump the default httpd.conf and remove all comments
sh -cx '$DOCKER run --rm httpd:2.4 cat /usr/local/apache2/conf/httpd.conf' \
| sed 's|^[[:space:]]*#.*$||' | grep . > httpd.conf

# append git-http specific virtualhost to local httpd.conf
cat << EOF >> httpd.conf

LoadModule cgi_module modules/mod_cgi.so
LoadModule alias_module modules/mod_alias.so
LoadModule cgi_module modules/mod_cgi.so

<VirtualHost *:80>
    SetEnv GIT_PROJECT_ROOT /srv/git
    SetEnv GIT_HTTP_EXPORT_ALL
    ScriptAliasMatch \
        "(?x)^/(.*/(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/\$1"
    <Directory /usr/lib/git-core>
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        AllowOverride None
        # Require all granted

        AuthType Basic
        AuthName "User Authentication"
        AuthUserFile /usr/local/apache2/.htpasswd
        Require valid-user
    </Directory>

    # not important (anymore), just for debugging
    LogLevel trace8
    ErrorLog /var/log/apache2/git/error.log
    CustomLog /var/log/apache2/git/access.log combined
</VirtualHost>
EOF

# create Dockerfile
cat << EOF > Dockerfile
FROM httpd:2.4

# https://git-scm.com/docs/git-http-backend
RUN apt-get update -y && apt-get install -y git apache2-utils

COPY httpd.conf /usr/local/apache2/conf/httpd.conf

RUN mkdir -p /var/log/apache2/git
RUN chown -R www-data:www-data /var/log/apache2
EOF

# create docker-compose.yml
cat << EOF > docker-compose.yml
services:
  $DOCKERC_SERVICE_NAME:
    build: .
    ports:
      - "$LOCAL_IP_PORT:80"
EOF


# just a wrapper for repetitive calls to the linux container
remotecmd() {
    sh -cx "$DOCKER_COMPOSE exec -w /srv/git/$REMOTE_REPO_NAME $DOCKERC_SERVICE_NAME -- $@"
}

# start the container
sh -cx '$DOCKER_COMPOSE up --detach --build --force-recreate'

# initialize the sample repository directory within the container
sh -cx "$DOCKER_COMPOSE exec $DOCKERC_SERVICE_NAME mkdir -p /srv/git/$REMOTE_REPO_NAME"

remotecmd "git clone --bare --shared $REMOTE_REPO_ORIGIN ."

remotecmd 'git update-server-info'

remotecmd 'chmod -R 755 ..'

remotecmd 'chown -R www-data:www-data ..'

remotecmd "htpasswd -cb /usr/local/apache2/.htpasswd $REMOTE_USERNAME $REMOTE_PASSWORD"

remotecmd 'cat /usr/local/apache2/.htpasswd'

sh -cx "python3 -m venv .venv"

sh -cx ".venv/bin/pip install pipenv"

sh -cx '.venv/bin/pipenv install $REPO_URL'

