#!/usr/bin/perl

use strict;

use gateway_lib;

use IO::File;

use Getopt::Long;

my $ssh_pubkey;
my $ssh_privkey;
my $tunnelip;
my $peer;
my $fedkit_dir= "/usr/local/federation";
my $perl = "/usr/bin/perl";
my $iface_file;
my $ssh = "/usr/bin/ssh";
my @ports;

exit(20) unless GetOptions(
    'ssh_pubkey=s' => \$ssh_pubkey,
    'ssh_privkey=s' => \$ssh_privkey,
    'tunnelip' => \$tunnelip,
    'peer=s' => \$peer,
    'fedkit=s' => \$fedkit_dir,
    'perl=s' => \$perl,
    'interfaces=s' => \$iface_file,
    'ssh=s' => \$ssh,
    'port=s', \@ports,
);


my $tunnelparam = $tunnelip ? '--tunnelip' : '';
my $portparam = join(" --port=", @ports);

# join doesn't prefix the first one
$portparam="--port=$portparam" if $portparam;

system("$perl -I$fedkit_dir/lib $fedkit_dir/bin/active_config.pl " .
    "--peer=$peer --ssh_pubkey=$ssh_pubkey --ssh_privkey=$ssh_privkey " .
    " $tunnelparam --fedkit=$fedkit_dir --perl=$perl " .
    "--ssh=$ssh --interfaces=$iface_file");
exit(20) if $?;

system("$perl -I$fedkit_dir/lib $fedkit_dir/bin/port_forward.pl " .
    "--peer=$peer --ssh_pubkey=$ssh_pubkey --ssh_privkey=$ssh_privkey " .
    " $tunnelparam --fedkit=$fedkit_dir --perl=$perl " .
    "--ssh=$ssh $portparam");
exit(20) if $?;
