#!/bin/bash

#######################################################################
####################### Make sure that run as root ####################
#######################################################################

if [ "$EUID" -ne 0 ]
  then echo "Please run as root"
  exit
fi


#######################################################################
###################### Setting project paths ##########################
#######################################################################

topdirname="fmcmasterfip"
projectpath="."
LOGDIR="${HOME}/Desktop/pts_log/fmcmasterfip"

#######################################################################
############# Getting absolute path for project #######################
#######################################################################

if [ -n "$1" ]
then
    top="$1"
else
    prg=$0

    if [ ! -e "$prg" ]; then
      case $prg in
        (*/*) exit 1;;
        (*) prg=$(command -v -- "$prg") || exit;;
      esac
    fi
    dir=$(
      cd -P -- "$(dirname -- "$prg")" && pwd -P
    ) || exit
    prg=$dir/$(basename -- "$prg") || exit 
    
    top=$prg
    while true
    do
        if [ `basename "$top"` == "$topdirname" ]
        then
            break;
        else
            top=`dirname "$top"`
        fi
    done
fi


# path verification

if [ ! -d "${top}/${projectpath}/software" ]; then
    echo "Couldn't find project path!"
    echo "Please make sure that top directory of PtsFmcMastrefip is called \"${topdirname}\""
    exit 1
fi

echo "Identified $top as FMC masterFip top directory"

#######################################################################
####################### Drivers management ############################
#######################################################################


# remove drivers
# in order to make sure that tests will be run
# with correct driver versions

rmmod fmc_adc_100m14b   2> /dev/null
rmmod zio               2> /dev/null
rmmod rawrabbit         2> /dev/null
rmmod spec              2> /dev/null
rmmod fmc               2> /dev/null
rmmod cp210x            2> /dev/null

sleep 1

# load precompiled drivers with known verson

insmod "${top}/${projectpath}/software/fmc-bus/kernel/fmc.ko" 
insmod "${top}/${projectpath}/software/spec-sw/kernel/spec.ko" "fw_name=../../${top}/${projectpath}/gateware/spec-init.bin-2012-12-14"
insmod "${top}/${projectpath}/software/zio/zio.ko"
insmod "${top}/${projectpath}/software/fmc-adc-100m14b4cha-sw/kernel/fmc-adc-100m14b.ko" "gateware=../../${top}/${projectpath}/gateware/syn/spec/spec_top_fmc_masterfip.bin"
modprobe usbserial
insmod "${top}/${projectpath}/software/cp210x/cp210x.ko"


#######################################################################
############################## Logging ################################
#######################################################################


# create log directories
mkdir -p "$LOGDIR"
mkdir -p "$LOGDIR/eeprom"
mkdir -p "$LOGDIR/adcdata"


# get board serial
serial=0000
extra_serial=0000

serial=$1
if [ x$1 = x"" ]; then
	echo -n "Please scan CERN serial number bar-code, then press [ENTER]: "
        read serial
fi

if [ x$serial = x"" ]; then
	serial=0000
fi

extra_serial=$2
if [ x$2 = x"" ]; then
	echo -n "If needed input extra serial number and press [ENTER] OR just press [ENTER]: "
        read extra_serial
fi

if [ x$extra_serial = x"" ]; then
	extra_serial=0000
fi

echo " "

nb_test_limit=2
nb_test=1


# leftovers from previous tests
rm -f "/tmp/mfdata_*"


#######################################################################
############################## Testing  ###############################
#######################################################################


while [ "$nb_test" -le "$nb_test_limit" ]
do
    echo "--------------------------------------------------------------"
    echo "Test series run $nb_test out of $nb_test_limit"
    echo " "

    # run tests
    sudo ${top}/pts/ptsFip.py -b FmcMasterFip -s $serial -e $extra_serial -t../python -l $LOGDIR 00 01 02 03 04 05 06


    # backup eeprom image
    if [ -f /tmp/eeprom ]
    then
        cp "/tmp/eeprom" "${LOGDIR}/eeprom/${serial}_${extra_serial}_eeprom"
    else
        echo "Did not find eeprom image."
    fi

    # backup adc samples contents
    if [ `ls /tmp/mfdata_* 2>/dev/null | wc -l` -ne 0 ]
    then
        for f in `ls /tmp/mfdata_*`
        do
            topic=`sed 's/\/tmp\/mfdata_//' <<< $f`
            newname="${serial}_${extra_serial}_${topic}"
            cp $f "${LOGDIR}/adcdata/${newname}" 

        done
        echo "ADC samples backed up in ${LOGDIR}/adcdata"
    else
        echo "Did not find any ADC samples to backup"
    fi

    # repeat test?
    if [ "$nb_test" != "$nb_test_limit" ]
    then
        echo " "
        echo -n "Do you want to run the test series again [y,n]? "
        read reply
        if [ "$reply" != "y" ]
        then
            
            break
        fi
    fi
    nb_test=$(($nb_test+1))
done

echo "--------------------------------------------------------------"
echo " "
echo -n "End of the test, do you want to switch the computer OFF? [y,n]"
read reply
if [ "$reply" = "y" ]
then
    sudo poweroff
fi

