#!/bin/sh -euf
#
# release-guide
#
# A release that commits the HTML guide to another git repository
#
# We update the version number in a Dockerfile, and tests that
# the resulting URLs can be fetched. Commits the changes and pushes
# the result.
#
# We expect an 'html' subdirectory and likely an 'index.html' file there.
#
# Arguments are described here. Most arguments have an equivalent envvar.
#
# -f tgz     RELEASE_SOURCE=tbz     The tarball to extract documentation from
# -q         RELEASE_QUIET=1        Make output more quiet
# -v         RELEASE_VERBOSE=1      Make output more verbose
#

set -euf

# Various arguments
TRANSACTION=${RELEASE_TRANSACTION:-0}
QUIET=${RELEASE_QUIET:-0}
VERBOSE=${RELEASE_VERBOSE:-0}
SRPM=${RELEASE_SRPM:-$PWD/srpm}
SOURCE=${RELEASE_SOURCE:-}
CHECK=${RELEASE_CHECK:-0}

REPO=""
WORKDIR=""
DIRECTORY=""
TARBALL=""

usage()
{
    echo "usage: release-guide [-qvxz] [-f TARBALL] DIRECTORY REPO" >&2
    exit ${1-2}
}

trace()
{
    if [ $QUIET -eq 0 ]; then
        echo "> $@" >&2
    fi
}

message()
{
    echo "release-guide: $@" >&2
}

parse_version()
{
    echo "$@" | sed -n 's/.\+-\([0-9].*\)/\1/p'
}

check()
{
    check-git-rw git@github.com "$REPO"
}

prepare()
{
    local version subdir

    trace "Extracting the tarball $TARBALL"

    WORKDIR=$(mktemp --directory source.XXXXXX)
    mkdir -p $WORKDIR/extract
    tar -f $TARBALL -C $WORKDIR/extract -x

    trace "Checking out Github repo $REPO"

    cd $WORKDIR

    git clone git@github.com:$REPO repo
    cd extract

    set +f
    subdir=$(echo *)
    set -f

    version="$(parse_version $subdir)"
    if [ -z "$version" ]; then
        message "couldn't determine version from $subdir"
        exit 1
    fi

    cd ..

    trace "Committing documentation for version $version"
    mkdir -p repo/guide/
    rm -rf repo/guide/$version
    if [ ! -f extract/$subdir/$DIRECTORY/html/index.html ]; then
        message "no html/index.html documentation in $DIRECTORY"
        exit 1
    fi

    mv -n extract/$subdir/$DIRECTORY/html/ repo/guide/$version
    if [ -f extract/$subdir/$DIRECTORY/index.html ]; then
        mv extract/$subdir/$DIRECTORY/index.html repo/guide/index.html
    fi
    rm -rf repo/guide/latest
    cp -rp repo/guide/$version repo/guide/latest

    # Add frontmatter for Jekyll
    find repo/guide/latest -name '*.html' | xargs sed -i '
1i\
---\
layout: guide\
---'

    git -C repo add guide/

    if git -C repo diff --staged --exit-code > /dev/null; then
        trace "Already uploaded documentation for $version"
        exit 0
    fi

    trace "Committing changes"
    git -C repo commit --message="Update guide to version $version"
    cd ..
}

commit()
(
    trace "Pushing changes to guide"
    git -C $WORKDIR/repo push origin master
    rm -rf $WORKDIR
)

while getopts "f:qvxz" opt; do
    case "$opt" in
    f)
        SOURCE="$OPTARG"
        ;;
    q)
        QUIET=1
        VERBOSE=0
        ;;
    v)
        QUIET=0
        VERBOSE=1
        ;;
    x)
        TRANSACTION=1
        ;;
    z)
        CHECK=1
        ;;
    -)
        break
        ;;
    *)
        usage
        ;;
    esac
done

shift $(expr $OPTIND - 1)

if [ $# -ne 2 ]; then
    usage
fi

if [ $VERBOSE -eq 1 ]; then
    set -x
fi

DIRECTORY="$1"
REPO="$2"

if [ -z "$SOURCE" ]; then
    message "no tarball source specified"
    exit 2
elif [ -d "$SOURCE" ]; then
    TARBALL="$(find $SOURCE -maxdepth 1 -name '*.tar.*' | sort | head -n1)"
elif [ -f "$SOURCE" ]; then
    TARBALL="$SOURCE"
else
    message "tarball source not found: $SOURCE"
    exit 1
fi

if [ $CHECK -eq 1 ]; then
    check
    exit 0
fi

prepare

if [ $TRANSACTION -eq 1 ]; then
    kill -STOP $$
fi

commit
