#!/bin/bash

if [[ $EUID -ne 0 ]]; then
   echo "This script must be run as root" 1>&2
   exit 1
fi

#---------------------------------------------------------------------------------------------------

function wait_for_exit()
{
    PROCESS=$1
    WAIT=30

    PID=`pidof $PROCESS`
    if [[ -n "$PID" ]]; then
        echo -n "Waiting for $PROCESS to exit"
        i=0
        while true; do
            echo -n "."
            sleep 1
            PID=`pidof $PROCESS`
            if [[ -z "$PID" ]]; then
                #exited
                break
            fi
            if ((i > $WAIT)); then
                echo "Failed, force killing it" 1>&2
                kill -KILL $PID
                break
            fi
            i=$((i+1))
        done
        echo ""
    fi
}

#---------------------------------------------------------------------------------------------------


if "/usr/bin/isVaultXL" ; then

    AVR_TEST_UTILITY=AVRTest

    read -p "Reset AVR board $1's configuration on this Vault? [y|n] " answer

    # (2) handle the command line argument we were given
    case $answer in
       [yY]* ) ;;

       [nN]* ) exit;;

       * )     echo "Unrecognised answer - assuming \"no\""; exit ;;
    esac

    echo "********************************************************************"
    echo "*  Stopping drserver                                               *"
    echo "********************************************************************"

    service drserver stop > /dev/null 2>&1
    wait_for_exit drserver

    ## blank the contents of the AVR Configuration area by writing 0xFF to it
    echo
    echo "********************************************************************"
    echo "*  Resetting contents of AVR configuration area                    *"
    echo "********************************************************************"
    echo "c0 ffx" | ${AVR_TEST_UTILITY} v $1 > /dev/null 2>&1
    echo "c1 ffx" | ${AVR_TEST_UTILITY} v $1 > /dev/null 2>&1
    echo "c2 ffx" | ${AVR_TEST_UTILITY} v $1 > /dev/null 2>&1
    echo "c3 ffx" | ${AVR_TEST_UTILITY} v $1 > /dev/null 2>&1
    echo "c4 ffx" | ${AVR_TEST_UTILITY} v $1 > /dev/null 2>&1
    echo "c5 ffx" | ${AVR_TEST_UTILITY} v $1 > /dev/null 2>&1
    echo "c6 ffx" | ${AVR_TEST_UTILITY} v $1 > /dev/null 2>&1
    echo "c7 ffx" | ${AVR_TEST_UTILITY} v $1 > /dev/null 2>&1

    ## See if the Vault's type has been set up in the AVR's configuration area
    ## Read the first byte of the configuration area
    CONFIGBYTE0=`echo "o0x" | ${AVR_TEST_UTILITY} v $1 | grep "Addr 0:" | sed 's/.*Addr 0: //'`
    if [[ "$CONFIGBYTE0" == "ff" ]]; then
        echo "***************************************************************************"
        echo "*  Please select your Vault XL's hardware configuration:                  *"
        echo "*  0 - Transfer Drive Bay hardware is connected to this board             *"
        echo "*  1 - Transfer Drive Bay hardware is NOT connected to this board         *"
        echo "*                                                                         *"
        echo "*  Selecting any other option will default to option 0                    *"
        echo "***************************************************************************"
        read input
        if [[ "$input" == "1" ]]; then
                echo "Configuring Vault XL AVR for no Transfer Drive Bay hardware"
                echo "c0 01x" | ${AVR_TEST_UTILITY} v $1 > /dev/null 2>&1
        else
                echo "Configuring Vault XL AVR for Transfer Drive Bay hardware"
                echo "c0 00x" | ${AVR_TEST_UTILITY} v $1 > /dev/null 2>&1
        fi
        echo "Done"

        ## Success ##
        echo "AVR reset needs to reboot machine to complete"
        echo "Please reboot the system once all AVR boards have been configured"
        echo -n " Press 'Enter' to exit: "
        read input
    else
        echo "AVR data is not in correct state unable to continue"
        echo "Please ensure that:"
        echo "- drserver is not running"
        echo "- AVRTest has been successfully installed"
        echo "- AVRTest can communicate with the selected AVR board"
        echo -n " Press 'Enter' to continue: "
        read -r
    fi

else
    echo "Can only write AVR config for Vault S"

fi
