#!/bin/sh
#
# 2011 Nico Schottelius (nico-cdist at schottelius.org)
#
# This file is part of cdist.
#
# cdist is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# cdist is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
#
#
# Push a directory to a target, both sides have the same name (i.e. explorers)
# or
# Pull a directory from a target, both sides have the same name (i.e. explorers)
#

# exit on any error
#set -e

basedir=${0%/*}
version=$(git describe)

# Manpage and HTML
A2XM="a2x -f manpage --no-xmllint -a encoding=UTF-8"
A2XH="a2x -f xhtml --no-xmllint -a encoding=UTF-8"

# Developer webbase
WEBDIR=$HOME/niconetz
WEBBASE=$WEBDIR/software/cdist
WEBMAN=$WEBBASE/man/$version
WEBPAGE=${WEBBASE}.mdwn

# Documentation
MANDIR=docs/man
MAN1DSTDIR=${MANDIR}/man1
MAN7DSTDIR=${MANDIR}/man7
SPEECHESDIR=docs/speeches

# Change to checkout directory
cd "$basedir"

case "$1" in
    man)
        set -e
        "$0" mangen
        "$0" mantype
        "$0" manbuild
    ;;

    manbuild)
        trap abort INT
        abort() {
            kill 0
        }
        for section in 1 7; do
            for src in ${MANDIR}/man${section}/*.text; do
                manpage="${src%.text}.$section"
                if [ ! -f "$manpage" -o "$manpage" -ot "$src" ]; then
                    echo "Compiling man page for $src"
                    $A2XM "$src"
                fi
                htmlpage="${src%.text}.html"
                if [ ! -f "$htmlpage" -o "$htmlpage" -ot "$src" ]; then
                    echo "Compiling html page for $src"
                    $A2XH "$src"
                fi
            done
        done
    ;;

    mantype)
        for mansrc in conf/type/*/man.text; do
            dst="$(echo $mansrc | sed -e 's;conf/;cdist-;'  -e 's;/;;' -e 's;/man;;' -e 's;^;docs/man/man7/;')"
            ln -sf "../../../$mansrc" "$dst"
        done
    ;;

    mangen)
        ${MANDIR}/cdist-reference.text.sh
    ;;

    dist)
        set -e
        # Do the checks
        $0 dist-check

        # Git changes - everything depends on this
        $0 dist-tag
        $0 dist-branch-merge

        # Pypi first - is the base for others
        $0 dist-pypi
        
        # Archlinux depends on successful pypi ;-)
        $0 dist-archlinux

        # Update website (includes documentation)
        $0 web

        $0 pub

        $0 dist-post
    ;;

    changelog-version)
        # get version from changelog and ensure it's not already present
        grep '^[[:digit:]]' "$basedir/docs/changelog" | head -n1 | sed 's/:.*//'
    ;;

    dist-check)
        set -e
        echo "Verifying documentation building works ..."
        $0 clean 
        $0 man

        changelog_version=$($0 changelog-version)
        echo "Target version from changelog: $changelog_version"

        if git show --quiet $changelog_version >/dev/null 2>&1; then
            echo "Version $changelog_version already exists, aborting."
            exit 1
        fi

        # verify date in changelog
        date_today="$(date +%Y-%m-%d)"
        date_changelog=$(grep '^[[:digit:]]' "$basedir/docs/changelog" | head -n1 | sed 's/.*: //')

        if [ "$date_today" != "$date_changelog" ]; then
            echo "Date in changelog is not today"
            echo "Changelog: $date_changelog"
            exit 1
        fi

    ;;

    dist-post)
        cat << notes

    To be done manually...
    
        - freecode release
        - blog entry
        - linkedin entry
        - mailinglist update
notes

    ;;

    dist-tag)
        version=$($0 changelog-version)
        # add tag
        printf "Enter tag description for %s> " "$version"
        read tagmessage
        git tag "$version" -m "$tagmessage"
    ;;

    dist-branch-merge)
        version=$($0 changelog-version)
        target_branch=${version%\.*}
        current_branch=$(git rev-parse --abbrev-ref HEAD)

        printf "Press enter to git merge $current_branch into \"$target_branch\" > "
        read prompt
        git checkout "$target_branch"
        git merge "$current_branch"
        git checkout "$current_branch"
    ;;

    dist-archlinux)
        ./PKGBUILD.in
        makepkg -c
        makepkg -c --source
    ;;
    dist-pypi)
        $0 man
        $0 version
        python3 setup.py sdist upload
    ;;

    speeches)
        cd "$SPEECHESDIR"
        for speech in *tex; do
            pdflatex "$speech"
            pdflatex "$speech"
            pdflatex "$speech"
        done
    ;;
        
    webmain)
        cp README ${WEBPAGE}
        cd ${WEBDIR} && git commit -m "cdist main update" ${WEBPAGE}
        cd ${WEBDIR} && make pub
    ;;

    web)
        cp README ${WEBPAGE}
        rm -rf ${WEBMAN}
        mkdir -p ${WEBMAN}/man1 ${WEBMAN}/man7

        cp ${MAN1DSTDIR}/*.html ${MAN1DSTDIR}/*.css ${WEBMAN}/man1 
        cp ${MAN7DSTDIR}/*.html ${MAN7DSTDIR}/*.css ${WEBMAN}/man7
        
        cd ${WEBDIR} && git add ${WEBBASE}
        cd ${WEBDIR} && git commit -m "cdist update" ${WEBBASE} ${WEBPAGE}
        cd ${WEBDIR} && make pub

        # Fix ikiwiki, which does not like symlinks for pseudo security
        ssh tee.schottelius.org \
          "cd /home/services/www/nico/www.nico.schottelius.org/www/software/cdist/man &&
                rm -f latest && ln -sf "$version" latest"
    ;;

     p|pu|pub)
          for remote in "" github sf ethz; do
                echo "Pushing to $remote"
                git push --mirror $remote
          done
    ;;

    clean)
        rm -f ${MAN7DSTDIR}/cdist-reference.text

        find "${MANDIR}" -mindepth 2 -type l \
            -o -name "*.1" \
            -o -name "*.7" \
            -o -name "*.html" \
            -o -name "*.xml" \
        | xargs rm -f

        find * -name __pycache__  | xargs rm -rf
    ;;
    clean-dist)
        rm -f cdist/version.py MANIFEST PKGBUILD
        rm -rf cache/ dist/

        # Archlinux
        rm -f cdist-*.pkg.tar.xz cdist-*.tar.gz
        rm -rf pkg/ src/
    ;;

    very-clean)
        $0 clean
        $0 clean-dist
    ;;

    test)
        shift # skip t

        if [ $# -lt 1 ]; then
            python3 -m cdist.test
        else
            python3 -m unittest "$@"
        fi
    ;;

    version)
        echo "VERSION=\"$version\"" > cdist/version.py
    ;;

    *)
        echo ''
        echo 'Welcome to cdist!'
        echo ''
        echo 'Here are the possible targets:'
        echo ''
        echo '    clean: Remove build stuff'
        echo '    man: Build manpages (requires Asciidoc)'
        echo '    test: Run tests'
        echo ''
        echo ''
        echo "Unknown target, \"$1\"" >&2
        exit 1
    ;;

esac