#!/usr/bin/perl

use strict;
use warnings;

use Getopt::Long;

my $opt_help;
my $opt_device;
my $opt_baudrate;

GetOptions (
	    'h|help'        => \$opt_help,
	    'd|device=s'    => \$opt_device,
	    'b|baudrate=i'  => \$opt_baudrate,
);



if ($opt_help || !defined $opt_device || defined $ARGV[0]) {
  print "Error: No device argument given!\n" if (!defined $opt_device);
  print "Error: more than device as an argument. What should this mean?\n" if (defined $ARGV[0]);

  print <<EOF;
usage: hadcon [-h] --device=<device>|-d <device> [--baudrate=<baudrate>|-b <baudrate>]

hadcon connects to a device and opens a tmux session with separate "windows" for input and output.
It supports a command history and command line editing.
--device <device>: is expected in the format: /dev/xxx, so normally /dev/ttyUSB0 or /dev/hadcon.
--baudrate <baudrate>: 115200 is default
To exit, you can press ctrl-c two times.
Or you can detach from the tmux session with: ctrl-b + d
Later you can attach again with the command: tmux attach
EOF

exit;

}

$opt_baudrate=115200 if(!defined $opt_baudrate);

my $c = qq|tmux new-session -n hadcon  "slurp_serial --device=$opt_device --baudrate=$opt_baudrate"  \\\; split-window -t hadcon -l3 "PERL_RL=' o=0' hadcon_command_only.pl --device=$opt_device" \\\; attach|;


#print "command: $c\n";
qx($c);
