#@IgnoreInspection BashAddShebang
# This file must be used with "source bin/activate" *from bash or zsh*
# you cannot run it directly

# the need for a seperate sync up and down stems from the filters
# on sync up we need to use the `:-` filter syntax
# on sync down we need to use the `.-` filter syntax
# also on sync down we don't want to use --delete and we add a third parameter that is an additional ignore file

GREEN='\033[0;32m'
RED='\033[0;31m'
NO_COLOR='\033[0m'

function do-sync-up() {
    echo "Syncing $1 to remote dock ($2)"
    if [ -e .dockerignore ]; then
        local HAS_DOCKERIGNORE=1
    fi

    local RESPONSE
    local DESTRUCTIVE=""
#    echo ""
#    echo -e "${GREEN}Use 'delete' option with rsync (destructive)? ${RED}enter${GREEN} to bypass, ${RED}y${GREEN} to use 'delete': ${NO_COLOR}"
#    read RESPONSE
#    if [ "$RESPONSE" = "y" ]; then DESTRUCTIVE="--delete"; fi

    echo rsync -azq ${DESTRUCTIVE} ${HAS_DOCKERIGNORE:+--filter=":- .dockerignore"} -e "ssh -o LogLevel=error -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" $1 $2
    rsync -azq ${DESTRUCTIVE} \
        ${HAS_DOCKERIGNORE:+--filter=":- .dockerignore"} \
        -e "ssh -o LogLevel=error -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" $1 $2
    echo "Sync complete"
}

function do-sync-down() {
    echo "Syncing from remote dock ($1)"
    if [ -e .dockerignore ]; then
        local HAS_DOCKERIGNORE=1
    fi

    local RESPONSE
    local DESTRUCTIVE=""
#    echo ""
#    echo -e "${GREEN}Use 'delete' option with rsync (destructive)? ${RED}enter${GREEN} to bypass, ${RED}y${GREEN} to use 'delete': ${NO_COLOR}"
#    read RESPONSE
#    if [ "$RESPONSE" = "y" ]; then DESTRUCTIVE="--delete"; fi

    echo rsync -azq ${DESTRUCTIVE} ${HAS_DOCKERIGNORE:+--filter=".- .dockerignore"}  ${3:+--exclude=$3} -e "ssh -o LogLevel=error -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" $1 $2
    rsync -azq ${DESTRUCTIVE} \
        ${HAS_DOCKERIGNORE:+--filter=".- .dockerignore"} \
        ${3:+--exclude=$3} \
        -e "ssh -o LogLevel=error -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" $1 $2
    echo "Sync complete"
}

function trim() {
    local var="$*"
    # remove leading whitespace characters
    var="${var#"${var%%[![:space:]]*}"}"
    # remove trailing whitespace characters
    var="${var%"${var##*[![:space:]]}"}"
    echo -n "$var"
}

function get_version_file() {
    # first get the directory name for the repo
    local CODE_DIR=${PWD##*/}

    # now find out where the version file is
    local LINE=$(grep versionfile_source $PWD/setup.cfg)
    local FILE=$(trim ${LINE#*=})

    if [[ -f "$PWD/_version.py.bld" && -f "$PWD/$FILE" ]]; then
        echo -n $CODE_DIR/$FILE
    fi
}

function sync-up() {
    local user=$(whoami)
    ssh ubuntu@$DOCKER_IP 'mkdir -p /data/workspaces/'$USER'/code'
    do-sync-up $PWD ubuntu@$DOCKER_IP:/data/workspaces/$USER/code

    # generate the version file and sync it over the <repo>/_version.py file
    # This utilizes the same versioning mechanism that versioneer has in place for exporting tars from
    # a git repo, and allows us to have version info without requiring a sync of the git repo
    if [ -f "$PWD/setup.cfg" ]; then
        # generate the _version.py.bld
        genversion

        # get the version file
        if [ -n "$PWD/setup.cfg" ]; then
            local VER_FILE=$(get_version_file)

            if [ -n "$VER_FILE" ]; then
                # finally sync _version.py.bld into the version file
                do-sync-up $PWD/_version.py.bld ubuntu@$DOCKER_IP:/data/workspaces/$USER/code/$VER_FILE
            fi
        fi
    fi
    return 0
}

function sync-down() {
    # if we have a proxy version file, ignore it on the sync down
    local VER_FILE=
    if [ -n "$PWD/setup.cfg" ]; then
        VER_FILE=$(get_version_file)
        VER_FILE=${VER_FILE#*/}
    fi
    do-sync-down "ubuntu@$DOCKER_IP:/data/workspaces/$(whoami)/code/${PWD##*/}/*" . ${VER_FILE:+$VER_FILE}
    return 0
}
