#! /bin/sh

set -e

usage()
{
    printf "\n$(basename $0) usage:\n\n"
    printf "Takes one optional switch: -y | --yes to skip dry-run confirmations.\n"
    printf "Takes the name of the library: '--libcore' or '--libgui'\n"
    printf "\nAttention:\n"
    printf "Note that a VPN connection to UPSAY must be opened (use open-upsay.sh)\n\n"
}

if [ "$#" =  "0" ] || [ "x$1" = "x-h" ]  || [ "x$1" = "x--help" ]; then
    usage
    exit 0
fi

always_yes="false"

if [ "x$1" = "x-y" ] || [ "x$1" = "x--yes" ]; then
    always_yes="true"
    shift
fi

echo "always_yes: ${always_yes}"

libcore="false"
libgui="false"

if [ "x$1" = "x--libcore" ]; then
    libcore="true"
    shift
fi


if [ "x$1" = "x--libgui" ]; then
    libgui="true"
    shift
fi

echo "libcore: ${libcore}"
echo "libgui: ${libgui}"

if [ "${libcore}" =  "false" ] && [ "${libgui}" = "false" ]; then
    usage
    exit 0
fi

# Go to the top source directory of the website:
build_dir="$HOME/devel/xpertmass/build-area/unix"

cd ${build_dir}

# First open a virtual private network with
# open-upsay.sh

if [ "${libcore}" = "true" ]; then

    if [ ! -d code_coverage ];then
        printf "\nError: directory core_coverage does not exist.\n"
        exit 1
    fi

    #This is the dry-run command:
    dry_run_command="rsync -avpnP --no-o --no-g --delete code_coverage/ $(cat $HOME/devel/varia/upsay-credentials)/code_coverage/libXpertMassCore"

    # This is the effective command:
    real_command="rsync -avpP --no-o --no-g --delete code_coverage/ $(cat $HOME/devel/varia/upsay-credentials)/code_coverage/libXpertMassCore"

    if [ "${always_yes}" != "true" ]; then

        printf "\nGoing to run dry-run command:\n\n${dry_run_command}\n\nContinue? <ENTER> | Ctrl-C\n"

        read answer
        eval ${dry_run_command}

        printf "\nShould now run the REAL command:\n\n${real_command}\n\nContinue? <ENTER> | Ctrl-C\n"

        read answer
    fi

    eval ${real_command}

    exit $?
fi
