#!/bin/bash
(

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=`echo "$prg" | sed 's/fmcdac600m12b1chadds.*/fmcdac600m12b1chadds\//'`
# echo $top
export PATH="\
$top/software/fmc-adc-100m14b4cha-sw/tools:\
$top/software/fmc-adc-100m14b4cha-sw/libtools:\
$PATH"

# echo $PATH

rm /tmp/data.bin
# echo "1"
tmp=`printf "%02x00" $3`
adcpath="/sys/bus/zio/devices/adc-100m14b-$tmp/"
echo 1 > "${adcpath}/cset0/trigger/sw-trg-enable"
# echo 1 > /sys/bus/zio/devices/adc-100m14b-0200/cset0/trigger/sw-trg-enable
# echo "2"
sleep 1
# echo "3"
fald-acq -b 0 -a $1 -r $2 -B /tmp/data.bin "0x${tmp}" &
# echo "4"
sleep 2
# echo "5"
echo 1 > "${adcpath}/cset0/trigger/sw-trg-fire"
# echo 1 > /sys/bus/zio/devices/adc-100m14b-0200/cset0/trigger/sw-trg-fire
# echo "6"
wait
# echo "7"
)


