#!/bin/bash
#
# Collects test files for use on drive module tester and sends them over to the tester PC
#
# @copyright Highly Confidential and Proprietary Zoox Information. Copyright (c) Zoox
#
# Author: trawat
# Date Created: April 28, 2020
#
# Note: this must be run on a Zoox Ubuntu desktop by a Zoox employee who has access to the
#       Zoox codebase

# Common variables used in this file
echo "Usage: ./configure_dm.sh <tester_pc_IP> [all]"
echo "Usage: add 'all' at the end to configure both env and scripts"
tester_pc="$1" # the first arg to this script must be the hostname or IP address of the tester PC
all="$2" # optional 2nd arg to configure both env and test scripts when specified
zooxuser=svc-teststation

if [ "$tester_pc" == "" ]; then
    echo "Usage: ./configure_dm.sh <tester_pc_IP>"
    exit 1
fi

# Get the driving repo
if [ ! -d "driving" ]; then
    git clone --depth 1 --no-single-branch git@git.zooxlabs.com:zooxco/driving.git
fi

# Checkout the branch with the DM code changes
cd driving
git checkout hwsi/dm_tester
git pull origin hwsi/dm_tester
cd ..

# Package env files if 'all' parameter is specified
if [ -n "$all" ]; then
    # Get zoox internal packages
    working_dir=`pwd`
    projects=("llama" "testrail_lib" "zooxpath")
    for project in ${projects[@]}; do
        mkdir -p pkg
        inspect_path=driving/firmware/validation/py_packages/$project
        # Build zoox packages from source
        cd $inspect_path
        rm -rf dist 2> /dev/null
        python setup.py sdist
        cp dist/* ${working_dir}/pkg
        rm -rf dist 2> /dev/null
        cd $working_dir
    done
    # Get canassist 0.0.38
    pip download canassist==0.0.38 --index-url https://pypi.zooxlabs.com/simple -d ${working_dir}/pkg

    # Remove python-can (canassist will install this for us later) and wheels
    rm -f pkg/python-can-3.3.3.tar.gz 2> /dev/null
    rm -f pkg/*.whl 2> /dev/null

    # Copy chaski source dist individually
    cp chaski-1.0.1.tar.gz pkg

    # Zip the files we need for installation
    zip -r py_packages.zip pkg/*
fi

# Zip the files we require for the test
zip -r test_directories.zip \
    driving/firmware/validation/dm_tester \
    driving/firmware/validation/ecu_components \
    driving/firmware/validation/hil_class \
    driving/firmware/validation/tools

# Tell the user what to do next
if [ "$debug" != "" ]; then
    echo "===== Next Steps ====="
    echo "ssh ${zooxuser}@$tester_pc 'mkdir -p dropzone'"
    echo "ssh ${zooxuser}@$tester_pc 'rm -f dropzone/*'"
    echo "scp py_packages.zip ${zooxuser}@$tester_pc:/home/${zooxuser}/dropzone/"
    echo "scp test_directories.zip ${zooxuser}@$tester_pc:/home/${zooxuser}/dropzone/"
    echo "scp deploy_dm.sh ${zooxuser}@$tester_pc:/home/${zooxuser}/dropzone/"
fi
ssh ${zooxuser}@$tester_pc 'mkdir -p dropzone'
ssh ${zooxuser}@$tester_pc 'rm -f dropzone/*'

if [ -n "$all" ]; then
    scp py_packages.zip ${zooxuser}@$tester_pc:/home/${zooxuser}/dropzone/
fi
scp test_directories.zip ${zooxuser}@$tester_pc:/home/${zooxuser}/dropzone/
scp deploy_dm.sh ${zooxuser}@$tester_pc:/home/${zooxuser}/

if [ -n "$all" ]; then
    ssh ${zooxuser}@$tester_pc './deploy_dm.sh all'
else
    ssh ${zooxuser}@$tester_pc './deploy_dm.sh'
fi

echo "Cleaning up"
ssh ${zooxuser}@$tester_pc 'rm -f deploy_dm.sh'

rm -f py_packages.zip test_directories.zip pkg/* 2> /dev/null
rmdir pkg 2> /dev/null
echo "Done"
