#!/usr/bin/perl

# sample usage:
# ./ng-client offer --trust-address --from-tag=sgadhdagf --call-id=sfghjfsh --sdp=$'v=0\no=moo 1 2 IN IP4 192.168.1.90\ns=-\nc=IN IP4 192.168.1.90\nt=0 0\nm=audio 3456 RTP/AVP 0\n'

use warnings;
use strict;
use Getopt::Long;
use Data::Dumper;
use NGCP::Rtpengine;

my %options = ('proxy-address' => 'localhost', 'proxy-port' => 2223);

GetOptions(
	'proxy-address=s'		=> \$options{'proxy-address'},
	'proxy-port=s'			=> \$options{'proxy-port'},
	'from-tag=s'			=> \$options{'from-tag'},
	'to-tag=s'			=> \$options{'to-tag'},
	'call-id=s'			=> \$options{'call-id'},
	'via-branch=s'			=> \$options{'via-branch'},
	'protocol=s'			=> \$options{'transport protocol'},
	'trust-address'			=> \$options{'trust address'},
	'sip-source-address'		=> \$options{'sip source address'},
	'symmetric'			=> \$options{'symmetric'},
	'asymmetric'			=> \$options{'asymmetric'},
	'replace-origin'		=> \$options{'replace-origin'},
	'replace-session-connection'	=> \$options{'replace-session connection'},
	'client-address=s'		=> \$options{'client-address'},
	'sdp=s'				=> \$options{'sdp'},
	'sdp-file=s'			=> \$options{'sdp-file'},
	'ICE=s'				=> \$options{'ICE'},
	'DTLS=s'			=> \$options{'DTLS'},
	'SDES=s@'			=> \$options{'SDES'},
	'rtcp-mux=s@'			=> \$options{'rtcp-mux'},
	'address-family=s'		=> \$options{'address family'},
	'direction=s'			=> \$options{'direction'},
	'force'				=> \$options{'force'},
	'v|verbose'			=> \$options{'verbose'},
	'strict-source'			=> \$options{'strict source'},
	'media-handover'		=> \$options{'media handover'},
	'TOS=i'				=> \$options{'TOS'},
	'delete-delay=i'		=> \$options{'delete-delay'},
	'reset'				=> \$options{'reset'},
	'port-latching'			=> \$options{'port latching'},
	'media-address=s'		=> \$options{'media address'},
) or die;

my $cmd = shift(@ARGV) or die;

my %packet = (command => $cmd);

for my $x (split(/,/, 'from-tag,to-tag,call-id,transport protocol,media address,ICE,address family,DTLS,via-branch,media address')) {
	defined($options{$x}) and $packet{$x} = \$options{$x};
}
for my $x (split(/,/, 'TOS,delete-delay')) {
	defined($options{$x}) and $packet{$x} = $options{$x};
}
for my $x (split(/,/, 'trust address,symmetric,asymmetric,force,strict source,media handover,sip source address,reset,port latching')) {
	defined($options{$x}) and push(@{$packet{flags}}, $x);
}
for my $x (split(/,/, 'origin,session connection')) {
	defined($options{'replace-' . $x}) and push(@{$packet{replace}}, $x);
}
for my $x (split(/,/, 'rtcp-mux,SDES')) {
	$packet{$x} = $options{$x}
		if defined($options{$x}) && ref($options{$x}) eq 'ARRAY';
}
if (defined($options{direction})) {
	$options{direction} =~ /(.*),(.*)/ or die;
	$packet{direction} = [$1,$2];
}

if (defined($options{sdp})) {
	$packet{sdp} = $options{sdp};
}
elsif (defined($options{'sdp-file'})) {
	open(my $fh, '<', $options{'sdp-file'}) or die $!;
	my @sdp = <$fh> or die $!;
	close($fh);
	$packet{sdp} = join('', @sdp);
}
#elsif (@ARGV && $ARGV[0] eq 'sdp') {
#	shift(@ARGV);
#	$options{'client-address'} or die;
#	my ($ca, $cp);
#	if ($ca = inet_pton(AF_INET, $options{'client-address'})) {
#		$ca = inet_ntop(AF_INET, $ca);
#		$cp = "IP4";
#	}
#	elsif ($ca = inet_pton(AF_INET6, $options{'client-address'})) {
#		$ca = inet_ntop(AF_INET6, $ca);
#		$cp = "IP6";
#	}
#	$ca or die;
#	my $sdp = "v=0\r\no=- 12345 67890 IN $cp $ca\r\ns=session\r\nc=IN $cp $ca\r\nt=0 0\r\n";
#
#	$packet{sdp} = $sdp;
#}

$options{verbose} and print Dumper \%packet;

my $engine = NGCP::Rtpengine->new($options{'proxy-address'}, $options{'proxy-port'});
my $resp = $engine->req(\%packet);

#print Dumper $resp;
#exit;

if (defined($$resp{sdp})) {
	print("New SDP:\n-----8<-----8<-----8<-----8<-----8<-----\n$$resp{sdp}\n".
		"----->8----->8----->8----->8----->8-----\n");
}
else {
	local $Data::Dumper::Indent = 1;
	local $Data::Dumper::Terse = 1;
	local $Data::Dumper::Quotekeys = 0;
	print("Result dictionary:\n-----8<-----8<-----8<-----8<-----8<-----\n"
		. Dumper($resp)
		. "----->8----->8----->8----->8----->8-----\n");
}
