#!/bin/sh

# fcgi - a script to generate a Fossil CGI script (but not related at all to FastCGI)

# Dir where this script is running
SCRIPT_NAME=`readlink -f "$0"`
SCRIPT_DIR=`dirname "${SCRIPT_NAME}"`

FLAG_DELETE=
FLAG_NV=
REPOS_LIST_CLI=
EXIT_STATUS=-1

# Usage
usage()
{
cat << EOF

Usage:
  $0 [-nv|--no-verify] [-d|--delete] <respository-name> [<respository-name2>] ...

Help:
  $0 [-h|--help]
EOF
exit 1
}

# Returns an list of any existing repos, empty list if no repos exist.
check_for_non_existing_repos()
{
    MISSING_REPOS=
    for r_cli in $@; do
        printf "Verify: Check for existing repos '${r_cli}'...\t"
        echo "${REPOS_LIST_SERVER}" | grep -q "${r_cli}"
        if [ $? -eq 1 ];then
            MISSING_REPOS="${MISSING_REPOS} ${r_cli}"
            printf "ERROR\n"
        else
            printf "OK\n"
        fi
    done
    if [ -z "${MISSING_REPOS}" ];then
        return 0
    else
        printf "\nERROR: Missing repo(s): %s\n" "${MISSING_REPOS}"
        return 1
    fi
}

verify()
{
    # Verify that repos exists by default (skip using -nv)
    if [ -z "$FLAG_NV" ];then
        printf "Verify repos:\n"
        printf "=============\n"
        printf "Verify: Fetch repos list from server...\t\t"
        REPOS_LIST_SERVER=`"${SCRIPT_DIR}/flist"`
        if [ -z "$REPOS_LIST_SERVER" ];then
            printf "\nERROR:\n"
            printf "Could not fetch repos list from server.\n"
            printf "Internet or server connection problem?\n"
            exit 1
        fi
        printf "OK\n"
        check_for_non_existing_repos ${REPOS_LIST_CLI}
        if [ $? -eq 1 ];then
            printf "\nTIP:\n"
            printf "Run 'flist' to see all existing repositories.\n"
            usage;exit 1
        fi
    fi
}

# Check args
if [ $# -lt 1 ]; then
    usage
fi

# Check for command-line options
for arg in $@; do
    case "$arg" in
        -d|--delete)
            FLAG_DELETE="$arg"
        ;;
        -h|--help)
            usage
        ;;
        -nv|--no-verify)
            FLAG_NV="$arg"
        ;;
        -*)
            printf "\nERROR: Unknown option '$arg'.\n"; usage
        ;;
        *)
            REPOS_LIST_CLI="${REPOS_LIST_CLI} $1"
            ;;
    esac
    shift
done

if [ -z "${REPOS_LIST_CLI}" ]; then
    printf "\nERROR: Missing repository argument.\n"; usage
fi

REMOTE_SERVER=redantig@kuu.se
REMOTE_PORT=7822
REMOTE_REPOS_DIR=/home/redantig/fossil/repos
REMOTE_CGI_DIR=/home/redantig/www/kuu.se/fossil
SSH_COMMAND=ssh
if [ ${REMOTE_PORT} -ne 22 ];then
    SSH_COMMAND="ssh -p ${REMOTE_PORT}"
fi

verify

for r in ${REPOS_LIST_CLI}; do
    if [ -n "${FLAG_DELETE}" ];then
        # Delete any existing CGI script(s) for this repos
        ${SSH_COMMAND} ${REMOTE_SERVER} "if [ -f '${REMOTE_CGI_DIR}/${r}.cgi' ];then rm -f '${REMOTE_CGI_DIR}/${r}.cgi'; exit 0; else exit 1; fi"
        EXIT_STATUS=$?
    else
        # Create CGI script for web access to remote repos, and make it/them executable (755)
        ${SSH_COMMAND} ${REMOTE_SERVER} "if [ ! -f '${REMOTE_CGI_DIR}/${r}.cgi' ];then echo '#!/home/redantig/bin/fossil' > '${REMOTE_CGI_DIR}/${r}.cgi'; echo 'repository: ${REMOTE_REPOS_DIR}/${r}.fossil' >> '${REMOTE_CGI_DIR}/${r}.cgi'; chmod 755 '${REMOTE_CGI_DIR}/${r}.cgi'; exit 0; else exit 1; fi"
        EXIT_STATUS=$?
    fi
done
exit ${EXIT_STATUS}
