#!/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"
}

################################################################################
# process cmd-line args
################################################################################

POST_MAC=false
POST_WIN=false
POST_LINUX=false
DOCS_ONLY=false
FORCE=false

while [ "$1" != "" ]; do
    case $1 in
          --mac) POST_MAC=true   ;;
          --win) POST_WIN=true   ;;
        --linux) POST_LINUX=true ;;
        --force) FORCE=true      ;;
    esac
    shift
done

if ! ( $POST_WIN || $POST_MAC || $POST_LINUX )
then
    echo "Updating documentation only."
    DOCS_ONLY=true
fi

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

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

export VERSION

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

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

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

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

# verify version number in code
if egrep -q "\"$VERSION\"" enlighten/common.py
then
    echo "version found in common.py"
else
    echo "version missing from common.py"
    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 
################################################################################

# commented-out until we fix the symlink issue on Windows
if false
then
    make doc
    rsync --archive --delete docs/doxygen/html/ $USERNAME@wasatchphotonics.com:www/api/ENLIGHTEN/
    scp docs/ENLIGHTEN_CHANGELOG.html $USERNAME@wasatchphotonics.com:www/binaries/apps/enlighten/README.html
fi

if $DOCS_ONLY
then
    exit 0
fi
echo

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

mkdir -p releases

if $POST_WIN
then
    upload "Enlighten-Setup64-${VERSION}.exe"
else
    echo "skipping Windows"
fi

if $POST_MAC
then
    upload "ENLIGHTEN-MacOS-${VERSION}.zip"
else
    echo "skipping Mac"
fi

if $POST_LINUX
then
    TARBALL=`ls -t *.tgz | head -1`
    upload $TARBALL
else
    echo "skipping Linux"
fi

scp scripts/bootstrap/ubuntu/enlighten_ubuntu_launcher.sh $USERNAME@wasatchphotonics.com:www/binaries/apps/enlighten

################################################################################
# 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
