#!/usr/bin/perl

use v5.10;
use strict;
use DBI;
use File::Copy;

my $MYSQL_FILE = "/etc/mysql/conf.d/kamailio.cnf";
my $database = 'ivozprovider';
my $data_source = "dbi:mysql:$database" .
    ";mysql_read_default_file=$MYSQL_FILE" .
    ";mysql_read_default_group=kamailio";

my $PROXY;
my $LISTENERS_FILE;
my $PORTS_FILE;

sub printListeners() {
    my %listeners;

    my $mysql_table = $PROXY eq "users" ? "ProxyUsers" : "ProxyTrunks";

    my $dbh = DBI->connect($data_source, undef, undef) or die "$!";
    my $sql = "SELECT id, name, ip, advertisedIp FROM $mysql_table ORDER BY id ASC";
    my $sth = $dbh->prepare($sql);
    $sth->execute();

    unlink $LISTENERS_FILE;

    while (my @socket = $sth->fetchrow_array) {
        my $id = $socket[0];
        my $name = $socket[1];
        my $ip = $socket[2];
        my $advertised = $socket[3];

        if ($advertised && $id != 1) {
            &printPublicListener($id, $name, $ip, $advertised);
        } else {
            &printListener($id, $name, $ip);
        }

    }

    $sth->finish();
    $dbh->disconnect();

    return \%listeners;
}

sub printPorts() {
    unlink $PORTS_FILE;

    open my $FILE, ">>", $PORTS_FILE;

    say $FILE '#!define TRUNKS_DMQ_SERVER "sip:trunks.ivozprovider.local:5060"';
    say $FILE '#!define USERS_DMQ_SERVER "sip:users.ivozprovider.local:5060"';
    if ($PROXY eq "users") {
        say $FILE '#!define TRUNKS_SIP_PORT 5060';
    }

    say $FILE "";
    say $FILE "#!define SIP_PORT 5060";
    say $FILE "#!define SIPS_PORT 5061";

    if ($PROXY eq "users") {
        say $FILE "#!define RPC_PORT 8000";
        say $FILE "#!define WS_PORT 10080";
        say $FILE "#!define WSS_PORT 10081";
    } else {
        say $FILE "#!define RPC_PORT 8001";
        say $FILE "#!define XMLRPC_PORT 8002";
    }

    say $FILE "";
    close $FILE;
}

sub printListener() {
    my ($id, $name, $ip) = @_;

    $ip = $PROXY if $id == 1;

    open my $FILE, ">>", $LISTENERS_FILE;

    say $FILE "# $name (id: $id)";
    say $FILE "listen=udp:$ip:SIP_PORT";
    say $FILE "listen=tcp:$ip:SIP_PORT";
    say $FILE "listen=tls:$ip:SIPS_PORT";

    if ($PROXY eq "users") {
        say $FILE "listen=tcp:$ip:WS_PORT";
        say $FILE "listen=tls:$ip:WSS_PORT";
    } else {
        say $FILE "listen=tcp:$ip:XMLRPC_PORT" if $id == 1;
    }

    say $FILE "listen=tcp:$ip:RPC_PORT" if $id == 1;
    say $FILE "";

    close $FILE;
}

sub printPublicListener() {
    my ($id, $name, $ip, $advertised) = @_;

    open my $FILE, ">>", $LISTENERS_FILE;

    say $FILE "# $name (id: $id)";
    say $FILE "listen=udp:$ip:SIP_PORT advertise $advertised:SIP_PORT";
    say $FILE "listen=tcp:$ip:SIP_PORT advertise $advertised:SIP_PORT";
    say $FILE "listen=tls:$ip:SIPS_PORT advertise $advertised:SIPS_PORT";

    if ($PROXY eq "users") {
        say $FILE "listen=tcp:$ip:WS_PORT advertise $advertised:WS_PORT";
        say $FILE "listen=tls:$ip:WSS_PORT advertise $advertised:WSS_PORT";
    }

    say $FILE "";
    close $FILE;
}

if (@ARGV != 1 || ($ARGV[0] ne "users" && $ARGV[0] ne "trunks")) {
    die "One parameter needed: users/trunks\n";
    exit(-1);
}

$PROXY = shift @ARGV;
$PORTS_FILE = "/etc/kamailio/proxy$PROXY/ports.cfg";
$LISTENERS_FILE = "/etc/kamailio/proxy$PROXY/listeners.cfg";

if (-f "$PORTS_FILE.dev") {
    copy "$PORTS_FILE.dev", $PORTS_FILE;
} else {
    &printPorts();
}

&printListeners();

exit(0);
