#!/bin/sh

# fhide - add/remove a entry in the $REMOTE_REPOS_DIR/.hidden file

FLAG_UNHIDE=
FLAG_SHOW=
FLAG_NV=
REPOS_LIST_CLI=
DO_NOTHING=0
REPOS_LIST_SERVER=

# Usage
usage()
{
cat << EOF    

Usage:
  $0 [-nv|--no-verify] [-u|--unhide] <respository-name> [<respository-name2>] ...
Show hidden repos (flag is optional):
  $0 [-s|--show]

Help:
  $0 [--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=`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 for command-line options
for arg in $@; do
    case "$arg" in
        -u|--unhide)
            FLAG_UNHIDE="$arg"
        ;;
        -s|--show)
            FLAG_SHOW="$arg"
        ;;
        -nv|--no-verify)
            FLAG_NV="$arg"
        ;;
        -h|--help)
            usage
        ;;
        -*)
            printf "\nERROR: Unknown option '$arg'.\n"; usage
        ;;
        *)
            REPOS_LIST_CLI="${REPOS_LIST_CLI} $1"
            ;;
    esac
    shift
done
set -- ${REPOS_LIST_CLI}

REMOTE_SERVER=redantig@kuu.se
REMOTE_PORT=7822
REMOTE_REPOS_DIR=/home/redantig/fossil/repos
REMOTE_HIDDEN_FILE="${REMOTE_REPOS_DIR}"/.hidden
LOCAL_REPOS_DIR=`dirname \`readlink -f $0\``
LOCAL_HIDDEN_FILE="${LOCAL_REPOS_DIR}"/.hidden
SSH_COMMAND=ssh
SCP_COMMAND="scp -p"
if [ ${REMOTE_PORT} -ne 22 ];then
    SSH_COMMAND="ssh -p ${REMOTE_PORT}"
    SCP_COMMAND="scp -p -P ${REMOTE_PORT}"
fi

# If no args, set 'show' flag
if [ $# -lt 1 ]; then
    FLAG_SHOW=-s
fi

# Skip if show flag
if [ -z "${FLAG_SHOW}" ]; then

    # If args were given but without repos names, exit with error
    if [ -z "${REPOS_LIST_CLI}" ]; then
        printf "\nERROR: Missing repository argument.\n"; usage
    fi
    verify
fi

# Start with a empty local copy of .hidden
# If this file is empty at the end, don't upload it to the server
echo > ${LOCAL_HIDDEN_FILE}

# Download remote .hidden if it exists
${SCP_COMMAND} ${REMOTE_SERVER}:${REMOTE_HIDDEN_FILE} ${LOCAL_HIDDEN_FILE} > /dev/null 2>&1
# Remove empty lines from .hidden
sed -i /^$/d ${LOCAL_HIDDEN_FILE}

# If show options, show .hidden and exit
if [ -n "${FLAG_SHOW}" ]; then
    cat ${LOCAL_HIDDEN_FILE}
    exit 0
fi

# Loop through repos
for r in ${REPOS_LIST_CLI}; do
    if [ -n "${FLAG_UNHIDE}" ];then
        # Remove entry from .hidden
        cp ${LOCAL_HIDDEN_FILE} ${LOCAL_HIDDEN_FILE}.BUP
        sed -i /^$r$/d ${LOCAL_HIDDEN_FILE}
        # If entry does not exist in .hidden file, do nothing
        diff -q ${LOCAL_HIDDEN_FILE} ${LOCAL_HIDDEN_FILE}.BUP > /dev/null 2>&1
        if [ $? -eq 0 ];then
            printf "DO NOTHING\n"
        fi
        rm -f ${LOCAL_HIDDEN_FILE}.BUP
    else
        # Add entry to .hidden
        grep -E -q "^$r$" ${LOCAL_HIDDEN_FILE}
        if [ $? -eq 0 ];then
            printf "ALREADY HIDDEN $r\n"
            DO_NOTHING=1
        else
            echo "${r}" >> ${LOCAL_HIDDEN_FILE}
            cat ${LOCAL_HIDDEN_FILE} | sort -u > ${LOCAL_HIDDEN_FILE}.BUP
            mv ${LOCAL_HIDDEN_FILE}.BUP ${LOCAL_HIDDEN_FILE}
        fi
    fi
done

# Update .hidden

# Only upload .hidden to server if was modified
if [ ${DO_NOTHING} -eq 0 ];then
    ${SCP_COMMAND} ${LOCAL_HIDDEN_FILE} ${REMOTE_SERVER}:${REMOTE_HIDDEN_FILE} > /dev/null 2>&1
fi
