#!/bin/bash
################################################################################
#                                  deploy                                      #
################################################################################
#                                                                              #
#  DESCRIPTION:  Quick script to consistently and reliably upload releases     #
#                to our website.                                               #
#                                                                              #
#  AUTHOR:       Mark Zieg <mzieg@wasatchphotonics.com>                        #
#                                                                              #
################################################################################

function upload()
{
    local LOCAL_NAME="$1"

    if ! [ -f $LOCAL_NAME ]
    then
        echo "ERROR: can't find $LOCAL_NAME"
        exit 1
    fi

    REMOTE_PATH="binaries/apps/enlighten/$LOCAL_NAME"
    if $IS_BETA
    then
        REMOTE_PATH="binaries/apps/enlighten/beta/$LOCAL_NAME"
    fi
    FULL_REMOTE_PATH="www/$REMOTE_PATH"

    if ssh $USERNAME@wasatchphotonics.com ls $FULL_REMOTE_PATH 1>/dev/null 2>&1
    then
        echo "ERROR: $FULL_REMOTE_PATH already found on remote host (manually remove/rename then retry)"
        if $FORCE
        then
            echo "--force configured, allowing overwrite"
        else
            exit 1
        fi
    else
        echo "$FULL_REMOTE_PATH does not currently exist on remote host"
    fi

    scp $LOCAL_NAME $USERNAME@wasatchphotonics.com:$FULL_REMOTE_PATH

    mv -f $LOCAL_NAME releases

    echo "File posted to https://wasatchphotonics.com/$REMOTE_PATH"
}

################################################################################
# read parameters
################################################################################

read -p "Username:   " USERNAME
read -p "Version:    " VERSION 
read -p "Beta:       " BETA

export VERSION
export RUST_BACKTRACE=1

if echo $BETA | grep -q y
then
    IS_BETA=true
else
    IS_BETA=false
fi

################################################################################
# check for keys
################################################################################

if ! [ -f src/wp_enlighten/network/keys.py ]
then
    echo "WARNING: keys.py not found"
fi

################################################################################
# validate release requirements
################################################################################

# verify version number in enlighten.common
if egrep -q "\"$VERSION\"" src/wp_enlighten/common.py
then
    echo "version found in common.py"
else
    echo "version missing from common.py"
    exit 1
fi

# verify version number in pyproject.toml
if egrep -q "^version = \"$VERSION\"" pyproject.toml
then
    echo "version found in pyproject.toml"
else
    echo "version missing from pyproject.toml"
    exit 1
fi

# verify version number is tagged
if git tag | egrep -q $VERSION
then
    echo "Git tag found (ENLIGHTEN)"
else
    echo "Git tag not found in ENLIGHTEN: $VERSION"
    exit 1
fi

# verify Wasatch.PY is tagged
DRIVER_TAG="enlighten-$VERSION"
if ( cd ../Wasatch.PY && git tag -l ) | egrep -q "^${DRIVER_TAG}\$"
then
    echo "Git tag found (Wasatch.PY)"
else
    echo "Git tag not found in Wasatch.PY: $DRIVER_TAG"
    exit 1
fi

# verify Wasatch.PY version is deployed to PyPi
if [ -d ../Wasatch.PY ]
then
    WASATCH_PY_VERSION=`grep ^__version__ ../Wasatch.PY/wasatch/__init__.py | awk '{print $3}' | tr -d '"'`
    if pip index versions wasatch 2>&1 | grep -q $WASATCH_PY_VERSION
    then
        echo "Wasatch.PY $WASATCH_PY_VERSION found in PyPi"
    else
        echo "Wasatch.PY $WASATCH_PY_VERSION not found in PyPi"
        $IS_BETA || exit 1
    fi
else
    echo "Apparently using Wasatch.PY from PyPi"
fi

################################################################################
# render documentation 
################################################################################

echo "make docs..."
make docs
echo

echo "rsync..."
rsync --archive --delete --src docs/doxygen/html/ --dest $USERNAME@wasatchphotonics.com:www/api/ENLIGHTEN/
echo

echo "scp..."
scp docs/ENLIGHTEN_CHANGELOG.html $USERNAME@wasatchphotonics.com:www/binaries/apps/enlighten/README.html
echo

################################################################################
# upload binaries
################################################################################

mkdir -p releases
upload "Enlighten-Setup64-${VERSION}.exe"

################################################################################
# capture build artifacts
################################################################################

if [ -r build.out ]
then
    BUILD_LOG=releases/build-${VERSION}.log
    echo "stdout:"   > $BUILD_LOG
    cat   build.out >> $BUILD_LOG

    echo "stderr:"  >> $BUILD_LOG
    cat   build.err >> $BUILD_LOG

    gzip $BUILD_LOG
fi

################################################################################
# code quality
################################################################################

echo "Recommend periodically checking code quality with:"
echo
echo "    $ make flake8"
echo "    $ make pylint"
echo
