#!/bin/sh
# shellcheck source=./config.def

# A post-receive hook to form stagit pages, creating tarballs, reinitializing
# stagit-index.
#
# This script can be run for each repository, without pushing, by going to the
# repository directory and running 'hooks/post-receive'.

# Parse the configuration file
hooksdir=$(readlink -f "$0") hooksdir=${hooksdir%/*}
. "$hooksdir/config"
. "$hooksdir/lib"

# Store the repository name in a variable. We strip the directory names and
# '.git' here.
repository=${PWD##*/} repository=${repository%.git}
repository_path=${PWD}

# Push to mirrors if a 'mirrors' file exists on the base directory.
[ -f mirrors ] && while read -r mirror; do
    git push --mirror "$mirror"
done < mirrors

# Generate files parsed by 'stagit'.
[ -f owner ] || printf '%s\n' "$OWNER" > owner
[ -f url   ] || printf '%s\n' "$BASE_URL/$repository" > url

# Make sure we export the current repository.
:> git-daemon-export-ok

# Recreate the stagit structure
rm -rf "${STAGIT_DIR:?}/$repository"
mkdir -p "$STAGIT_DIR/$repository"

# Generate the index for every exported repository
set --
for repo in "$REPO_DIR/"*/git-daemon-export-ok; do set -- "$@" "${repo%/*}"; done
stagit-index "$@" |
    sed 's|log\.html|index.html|' |
    sed "s|<title>Repositories</title>|<title>${TITLE:-Repositories}</title>|" |
    sed "s|\"desc\">Repositories|\"desc\">${TITLE:-Repositories}|" |
    if [ "$EXTERN_URL" ]; then
        sed "s|<img|<a href=\"$EXTERN_URL\"><img|;s|</td>\$|</a></td>|"
    else
        cat
    fi > "$STAGIT_DIR/index.html"

# Install style files
[ "$CSS" ]     && install -Dm644 "$CSS"     "$STAGIT_DIR/style.css"
[ "$LOGO" ]    && install -Dm644 "$LOGO"    "$STAGIT_DIR/logo.png"
[ "$FAVICON" ] && install -Dm644 "$FAVICON" "$STAGIT_DIR/favicon.png"

(
cd "$STAGIT_DIR/$repository" || return 1

# We create a cache to the .cache file so log creation is faster on larger
# repositories.
stagit -c .cache "$repository_path"

for file in style.css logo.png favicon.png; do
    [ -f "../$file" ] && ln -sf "../$file" "$file"
done

# If a README file exists, link the index to the README. If it doesn't, link the
# index to the repository log.
rm -f index.html
# for file in file/*.html; do
#     [ -f "$file" ] || continue
#     case "${file##*/}" in README*)
#         sed 's|"../|"|' "$file" > index.html
#     esac
# done
[ -f index.html ] || ln -sf log.html index.html
)


# Create archives and plain raw files
[ "$ARCHIVE_ALL" = 1 ] || [ -f archive ] && {
    mkdir -p "$STAGIT_DIR/archive/$repository"
    git show-ref --tags --heads |
        while read -r _ ref; do
            tag=${ref#refs/*/}
            tarball="$STAGIT_DIR/archive/$repository/$repository-$tag.tar.gz"

            # Unless we are working with a branch, skip recreation of a tarball.
            [ "${ref##refs/heads/*}" ] && [ -f "$tarball" ] && continue

            git archive \
                --format tar.gz \
                --prefix "$repository-$tag/" \
                -o "$tarball" \
                -- "$tag"
        done
    for dir in archive "archive/$repository"; do (
        cd "$STAGIT_DIR/$dir" || exit 1
        lsindex "$STAGIT_DIR" > index.html
    ) done
}

[ "$RAW_ALL" = 1 ] || [ -f raw ] && {
    git show-ref --heads |
        while read -r _ ref; do
            branch=${ref##*/}
            branchdir="$STAGIT_DIR/raw/$repository/$branch"

            # Make sure we clean up the directory by removing it completely and
            # recreating it completely.
            rm -rf "$branchdir"; mkdir -p "$branchdir"

            # We create and extract a tar archive. I don't know a better way than
            # this, so suggestions are welcome.
            git archive --format tar -- "$branch" |
                (cd "$branchdir" || return 1 ; tar xf -)

            # Create an index for all directories in the repository
            find "$branchdir" -type d | while read -r dir; do (
                cd "$dir" || exit 1
                lsindex "$STAGIT_DIR" > index.html
            ) done

        done
    for dir in raw "raw/$repository"; do (
        cd "$STAGIT_DIR/$dir" || exit 1
        lsindex "$STAGIT_DIR" > index.html
    ) done
}

# If a repository has an individual hook named post-receive.extra, run it.
[ -f hooks/post-receive.extra ] && . hooks/post-receive.extra
