#!/bin/bash

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

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

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/isVaultS" ; then

    AVR_TEST_UTILITY=AVRTest

    read -p "Reset AVR 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} > /dev/null 2>&1
    echo "c1 ffx" | ${AVR_TEST_UTILITY} > /dev/null 2>&1
    echo "c2 ffx" | ${AVR_TEST_UTILITY} > /dev/null 2>&1
    echo "c3 ffx" | ${AVR_TEST_UTILITY} > /dev/null 2>&1
    echo "c4 ffx" | ${AVR_TEST_UTILITY} > /dev/null 2>&1
    echo "c5 ffx" | ${AVR_TEST_UTILITY} > /dev/null 2>&1
    echo "c6 ffx" | ${AVR_TEST_UTILITY} > /dev/null 2>&1
    echo "c7 ffx" | ${AVR_TEST_UTILITY} > /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} | grep "Addr 0:" | sed 's/.*Addr 0: //')
    if [[ "$CONFIGBYTE0" == "ff" ]]; then
        echo "***************************************************************************"
        echo "*  Please select your Vault's hardware configuration:                     *"
        echo "*  1 - One onboard/CD2 bay and four Codex Capture Drive bays              *"
        echo "*  2 - Two Sony AXSM bays, two Codex Capture Drive bays and two RED bays  *"
        echo "*  3 - One onboard/CD2 bay, two Codex Capture Drive bays and two RED bays *"
        echo "*  4 - Two Sony AXSM bays and four Codex Capture Drive bays               *"
        echo "*  5 - Four Codex Capture Drive bays                                      *"
        echo "*  6 - Two Codex Capture Drive bays and two RED bays                      *"
        echo "*                                                                         *"
        echo "*  Selecting any other option will default to option 1                    *"
        echo "***************************************************************************"
        read input
        if   [[ "$input" == "6" ]]; then
                echo "Configuring Vault for 2 Codex Capture Drive bays and 2 RED bays"
                echo "c0 00x" | ${AVR_TEST_UTILITY} > /dev/null 2>&1
                echo "c1 A5x" | ${AVR_TEST_UTILITY} > /dev/null 2>&1
        elif [[ "$input" == "5" ]]; then
                echo "Configuring Vault for 4 Codex Capture Drive bays"
                echo "c0 00x" | ${AVR_TEST_UTILITY} > /dev/null 2>&1
                echo "c1 55x" | ${AVR_TEST_UTILITY} > /dev/null 2>&1
        elif [[ "$input" == "4" ]]; then
                echo "Configuring Vault for 2 Sony AXSM bays and 4 Codex Capture Drive bays"
                echo "c0 20x" | ${AVR_TEST_UTILITY} > /dev/null 2>&1
                echo "c1 55x" | ${AVR_TEST_UTILITY} > /dev/null 2>&1
        elif [[ "$input" == "3" ]]; then
                echo "Configuring Vault for 1 onboard bay, 2 Codex Capture Drive bays and 2 RED bays"
                echo "c0 10x" | ${AVR_TEST_UTILITY} > /dev/null 2>&1
                echo "c1 A5x" | ${AVR_TEST_UTILITY} > /dev/null 2>&1
        elif [[ "$input" == "2" ]]; then
                echo "Configuring Vault for 2 Sony AXSM bays, 2 Codex Capture Drive bays and 2 RED bays"
                echo "c0 20x" | ${AVR_TEST_UTILITY} > /dev/null 2>&1
                echo "c1 A5x" | ${AVR_TEST_UTILITY} > /dev/null 2>&1
        else
                echo "Configuring Vault for 1 onboard bay and 4 Codex Capture Drive bays"
                echo "c0 10x" | ${AVR_TEST_UTILITY} > /dev/null 2>&1
                echo "c1 55x" | ${AVR_TEST_UTILITY} > /dev/null 2>&1
        fi
    fi

    echo "Done"

    ## Success ##
    echo "AVR reset needs to reboot machine to complete"
    echo -n " Press 'Enter' to continue: "
    read -r
    reboot

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

fi
