#!/bin/bash
#
RMTHOST=192.168.4.12
RMTUSER=mholl
RMTDIR=/home/mholl/smalltpc/lvcard2
CSVDIR=${RMTDIR}/csv
DEVICE=/dev/lvcard2_1
#ssh ${RMTUSER}@${RMTHOST} "stty -F $DEVICE cs8 -cstopb -parenb ixoff -icrnl -opost -onlcr -isig -icanon -echo -echoe"
#ssh ${RMTUSER}@${RMTHOST} "stty -a -F $DEVICE"
#echo ${RMTUSER}@${RMTHOST} "${RMTDIR}/lvcard2  --readadc 0 -M 0"
#ssh ${RMTUSER}@${RMTHOST} "${RMTDIR}/lvcard2  --readadc 0 -M 0 -f ${CSVDIR}/readdac.csv"
echo "Before doing anything, setup power supplies"
echo "...SRU"
echo "...Trigger adapter"
echo "...LV card"
echo "***Make sure cooling is on..."
echo "Next: Check network to ${RMTHOST}"
read -p "Exit[eE], else continue:" myinput
re='^[eE]'
if [[ "$myinput" =~ $re ]] ; then
    echo "EXITING"
    exit 1
fi
# Check network to remote host
echo "Check network to ${RMTHOST} ...."
ping -c 3 ${RMTHOST}
if [ $? -ne 0 ]; then
  echo "Error: Failed to connect to ${RMTHOST}"
  exit 1
fi
#
echo "Init serial line ..."
ssh ${RMTUSER}@${RMTHOST} "stty -F $DEVICE 57600 cs8 -cstopb -parenb ixoff -icrnl -opost -onlcr -isig -icanon -echo -echoe"
echo "Checking speed set"
ssh ${RMTUSER}@${RMTHOST} "stty speed -F $DEVICE"
echo "Next: Init SRU"
read -p "Exit[eE], else continue:" myinput
re='^[eE]'
if [[ "$myinput" =~ $re ]] ; then
    echo "EXITING"
    exit 1
fi
#
echo "Init SRU first time ..."
./srupup --sruif $SRUIF --cfgfile srupup-0-hw-mcmx.cfg
echo "Next: Init LV card ...."
read -p "Exit[eE], else continue:" myinput
re='^[eE]'
if [[ "$myinput" =~ $re ]] ; then
    echo "EXITING"
    exit 1
fi
#
echo "Init all LV cards ...."
ssh ${RMTUSER}@${RMTHOST} "${RMTDIR}/lvcard2 --poweroff 0 --mcmaddress 0 -f ${CSVDIR}/power.csv"
echo "Next: ADC readings"
read -p "Exit[eE], else continue:" myinput
re='^[eE]'
if [[ "$myinput" =~ $re ]] ; then
    echo "EXITING"
    exit 1
fi
#
echo "ADC readings all LVCARDS"
ssh ${RMTUSER}@${RMTHOST} "${RMTDIR}/lvcard2 --readadc 1 --mcmaddress 0 -f ${CSVDIR}/readadc.csv"
echo "Next: Enable LVCARD power"
read -p "Exit[eE], else continue:" myinput
re='^[eE]'
if [[ "$myinput" =~ $re ]] ; then
    echo "EXITING"
    exit 1
fi
#
echo "Enable LVCARD power - press output on"
echo "Next: Power on MCM  and read ADCs"
read -p "Exit[eE], else continue:" myinput
re='^[eE]'
if [[ "$myinput" =~ $re ]] ; then
    echo "EXITING"
    exit 1
fi
#
echo "Power on all MCM"
ssh ${RMTUSER}@${RMTHOST} "${RMTDIR}/lvcard2 --poweron 1 --mcmaddress 0 -f  ${CSVDIR}/power.csv"
echo "Reading ADCs"
ssh ${RMTUSER}@${RMTHOST} "${RMTDIR}/lvcard2 --readadc 2  --mcmaddress 0 -f  ${CSVDIR}/readadc.csv"
echo "Next: reading temperatures"
read -p "Exit[eE], else continue:" myinput
re='^[eE]'
if [[ "$myinput" =~ $re ]] ; then
    ssh ${RMTUSER}@${RMTHOST} "${RMTDIR}/lvcard2 --poweroff 1 --mcmaddress 0 -f  ${CSVDIR}/power.csv"
    echo "EXITING"
    exit 1
fi
#
echo "reading LVCARD temperatures"
ssh ${RMTUSER}@${RMTHOST} "${RMTDIR}/lvcard2 --lvtemp 2 --mcmaddress 0 -f  ${CSVDIR}/lvtemp.csv"
echo "reading MCM temperatures"
ssh ${RMTUSER}@${RMTHOST} "${RMTDIR}/lvcard2 --mcmtemp 2 --mcmaddress 0 -f ${CSVDIR}/mcmtemp.csv"
echo "Next: MCM init - write DAC settings"
read -p "Exit[eE] and shutdown, else continue:" myinput
re='^[eE]'
if [[ "$myinput" =~ $re ]] ; then
    ssh ${RMTUSER}@${RMTHOST} "${RMTDIR}/lvcard2 --poweroff 2 --mcmaddress 0 -f  ${CSVDIR}/power.csv"
    echo "EXITING"
    exit 1
fi
#
echo " MCM init - write/read DAC settings"
ssh ${RMTUSER}@${RMTHOST} "${RMTDIR}/lvcard2 --mcmini --mcmaddress 0 -f  ${CSVDIR}/mcmini.csv"
ssh ${RMTUSER}@${RMTHOST} "${RMTDIR}/lvcard2 --readdac 2 --mcmaddress 0 -f  ${CSVDIR}/readdac.csv"
# HERE: Decode DAC settings
echo "Next: SRU init"
read -p "Exit[eE] and shutdown, else continue:" myinput
re='^[eE]'
if [[ "$myinput" =~ $re ]] ; then
    ssh ${RMTUSER}@${RMTHOST} "${RMTDIR}/lvcard2 --poweroff 3 --mcmaddress 0 -f ${CSVDIR}/power.csv"
    echo "EXITING"
    exit 1
fi
#
echo "SRU init"
./srupup --sruif $SRUIF --cfgfile srupup-0-hw-mcmx.cfg
./mcmdumpregs --fec 0
./mcmdumpregs --fec 1
./mcmdumpregs --fec 2
echo "Next: switch on SALTRO"
read -p "Exit[eE] and shutdown, else continue:" myinput
re='^[eE]'
if [[ "$myinput" =~ $re ]] ; then
    ssh ${RMTUSER}@${RMTHOST} "${RMTDIR}/lvcard2 --poweroff 4 --mcmaddress 0 -f ${CSVDIR}/power.csv"
    echo "EXITING"
    exit 1
fi
# saltro on - read ADC and temp
./biasctrltpc-mcmx.sh 0 1
echo "Reading ADCs"
ssh ${RMTUSER}@${RMTHOST} "${RMTDIR}/lvcard2 --readadc 3  --mcmaddress 0 -f ${CSVDIR}/readadc.csv"
echo "Next: read temperatures"
read -p "Exit[eE] and shutdown, else continue:" myinput
re='^[eE]'
if [[ "$myinput" =~ $re ]] ; then
    ./biasctrltpc-mcmx.sh 1 0
    ssh ${RMTUSER}@${RMTHOST} "${RMTDIR}/lvcard2 --poweroff 5 --mcmaddress 0 -f ${CSVDIR}/power.csv"
    echo "EXITING"
    exit 1
fi
echo "reading LVCARD temperatures"
ssh ${RMTUSER}@${RMTHOST} "${RMTDIR}/lvcard2 --lvtemp 3 --mcmaddress 0 -f ${CSVDIR}/lvtemp.csv"
echo "reading MCM temperatures"
ssh ${RMTUSER}@${RMTHOST} "${RMTDIR}/lvcard2 --mcmtemp 3 --mcmaddress 0 -f ${CSVDIR}/mcmtemp.csv"

read -p "Exit[eE] and shutdown, else DONE:" myinput
re='^[eE]'
if [[ "$myinput" =~ $re ]] ; then
    ./biasctrltpc-mcmx.sh 1 0
    ssh ${RMTUSER}@${RMTHOST} "${RMTDIR}/lvcard2 --poweroff 6 --mcmaddress 0 -f ${CSVDIR}/power.csv"
    echo "EXITING"
    exit 1
fi
# uncomment if you do not want the power on SALTRO
#./biasctrltpc-mcmx.sh 1 0

echo "************"
echo "DONE"
echo "************"
echo "Example of running:"
echo "fec 2, gain=0 shaper=0 samples=1008 channels=bit 0 and 15 on else off"
echo "./smalltpc-setup-mcmx.sh 2 0 0 1008 0 7FFE"
echo "fecs \"0 1\", gain=0 shaper=0 samples=1008 channels=0(all) events=500 runnb=1"
echo "./smalltpc-test-hw-lvcard2-ssh-mcmx.sh \"0 1\" 0 0 1008 0 500 1"
exit
