#!/usr/bin/perl 

# fetch the scope configuration and store to a file
# the GenericIO headers/etc that get added to STDOUT
# makes it problematic to use STDOUT for this, because
# the extra text messes up the config. So just require
# an output file


use Lab::Instrument::TDS2024B;
use Getopt::Long;


my $filename;
my $force = 0;
my $tmc_address;
my $visa_name;
my $usb_serial;
my $help = 0;

    
Getopt::Long::GetOptions(
           "force|f" => \$force,
           "tmc_address|t" => \$tmc_address,
           "visa_name|v" => \$visa_name,
           "usb_serial|u" => \$usb_serial,
           "h|?|help" => \$help,
    );

if ($help) {
    print "usage: $0 [options] OUTFILE\n";
    print "\t-f  --force                              force output file overwrite\n";
    print "\t-tX --tmc_address=X                      use /dev/usbtmcX device\n";
    print "\t-vUSB... --visa_name=USB:0xAAAA::0xBBBB::0xCCCC  use visa-style address\n";
    print "\t-uSN     --usb_serial=SerialNumber       select device by serial number\n";
    print "\n";
    print "If device is not specified, first TDS2024B found is used\n";
    exit(0);
}

my $args = {};
$args->{tmc_address} = $tmc_address if defined $tmc_address;
$args->{visa_name} = $visa_name if defined $visa_name;
$args->{usb_serial} = $usb_serial if defined $usb_serial;
$args->{debug} = 1;

$filename = shift;
die("output file must be specified") unless defined($filename);

if (! -e $filename || $force) {
    open(OUT,">$filename") || die("unable to open '$filename' for writing");
} else {
    die("use --force to force overwriting of existing output file");
}

my $s = new Lab::Instrument::TDS2024B($args);

my $config = $s->get_setup();

print OUT $config;



