#! /labor/scope/bin/gdsh

DEV=$1
CHANS=$(echo -n $2 | sed s/,/\ /g)

function usage {
  ec=$1
  shift
  echo "Usage: $0 <device> <chans>" >&2
  echo "OOPS: $@" >&2
  exit $ec
}

[ -e "$DEV" ] || usage 1 "Device $DEV not found"
[ -z "$CHANS" ] && usage 2 "Provide at least one channels"

gds-open $DEV
[ $? -eq 0 ] || usage 1 "Cannot open device $DEV"

echo "Capturing channels $CHANS @ $DEV" >&2

TMP=$(mktemp -u)

FILES=
:stop
for ch in $CHANS ; 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
