#! /home/steger/.local/bin/gdsh

# capture data
OK=0
for idx in `seq 0 4`; do
 DEV=/dev/ttyACM$idx
 if [ -c $DEV ] ; then
   gds-open $DEV
   if [ $? -eq 0 ] ; then
     OK=1
     break
   fi
 fi
done

if [ $OK -ne 1 ] ; then
  echo "Cannot open device" >&2
  exit 1
fi


echo "Using device $DEV" >&2

echo -n "Channels enabled: " >&2
gds-channels -a >&2

if [ $# = 0 ] ; then
 channels=$(gds-channels -a)
else
 channels="$@"
fi

echo "Capturing channels: $channels" >&2
TMP=$(mktemp -u)

FILES=
:stop
for ch in $channels ; do
 SCAL=`:chan${ch}:scal_`
 :acq${ch}:mem_ -0 -h -V $SCAL > ${TMP}-${ch}
 FILES="$FILES ${TMP}-${ch}"
done
:run
gds-close

paste $FILES | \
 awk '{ 
  l = $1
  for (i = 2; i <= NF; i += 2)
    l = l" "$i
  print l
 }'

echo "OK" >&2
