#!/usr/bin/perl
#/* ========================================
# * ███╗   ███╗██████╗ ██╗ █████╗ ██╗
# * ████╗ ████║██╔══██╗██║██╔══██╗██║
# * ██╔████╔██║██║  ██║██║███████║██║
# * ██║╚██╔╝██║██║  ██║██║██╔══██║██║
# * ██║ ╚═╝ ██║██████╔╝██║██║  ██║███████╗
# * ╚═╝     ╚═╝╚═════╝ ╚═╝╚═╝  ╚═╝╚══════╝
# * ========================================
# * mDial - Omni-Channel Contact Centre Suite.
# * Initially Written by Martin McCarthy.
# * Contributions welcome.
# * Active: 2020 - 2023.
# *
# * This software is licensed under AGPLv2.
# * You can find more information here;
# * https://www.gnu.org/licenses/agpl-3.0.en.html
# * A copy of the license is also shipped with this build.
# *
# * Important note: this software is provided to you free of charge.
# * If you paid for this software, you were ripped off.
# *
# * This project is a fork of the awesome FOSS project, ViCiDial.
# * ViCiDial is copyrighted by Matt Florell and the ViCiDial Group
# * under the AGPLv2 license.
# *
# * You can find out more about ViCiDial;
# * Web: https://www.vicidial.com/
# * Email: Matt Florell <vicidial@gmail.com>
# * IRC: Libera.Chat - ##vicidial
# *
# * Bug reports, feature requests and patches welcome!
# * ======================================== */
$V      = 1;    # set to 1 for verbose mode
$CLIOUT = 1;    # set to 1 for printing 1 line to STDERR
$SPEAK  = 1;    # set to 1 for saying invalid digits
( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) =
  localtime(time);
$year = ( $year + 1900 );
$mon++;
if ( $mon < 10 )  { $mon  = "0$mon"; }
if ( $mday < 10 ) { $mday = "0$mday"; }
if ( $hour < 10 ) { $hour = "0$hour"; }
if ( $min < 10 )  { $min  = "0$min"; }
if ( $sec < 10 )  { $sec  = "0$sec"; }
$now_date_epoch = time();
$now_date       = "$year-$mon-$mday $hour:$min:$sec";

if ( length( $ARGV[0] ) > 1 ) {
    if ($CLIOUT) { print STDERR "Perl Environment Dump:\n"; }
    $i = 0;
    while ( $#ARGV >= $i ) {
        $args = "$args $ARGV[$i]";
        if ($CLIOUT) { print STDERR "$i|$ARGV[$i]|\n"; }
        $i++;
    }
    if ( $args =~ /--help/i ) {
        print
"allowed run time options:\n  [-q] = quiet\n  [-t] = test\n  [-debug] = verbose debug messages\n\n";
    }
    else {
        if ( $args =~ /-V/i ) {
            $V = 1;
        }
        if ( $args =~ /-debug/i ) {
            $DG = 1;
        }
        if ( $args =~ /-dbAVS/i ) {
            $DGA = 1;
        }
        if ( $args =~ /-q/i ) {
            $q = 1;
            $Q = 1;
        }
        if ( $args =~ /-t/i ) {
            $TEST = 1;
            $T    = 1;
        }
    }
}
else {
    if ($V) { print "no command line options set\n"; }
}
use Asterisk::AGI;
$AGI = new Asterisk::AGI;
$|   = 1;
while (<STDIN>) {
    chomp;
    last unless length($_);
    if ($V) {
        if (/^agi_(\w+)\:\s+(.*)$/) {
            $AGI{$1} = $2;
        }
    }
    if (/^agi_uniqueid\:\s+(.*)$/)     { $unique_id    = $1; }
    if (/^agi_channel\:\s+(.*)$/)      { $channel      = $1; }
    if (/^agi_extension\:\s+(.*)$/)    { $extension    = $1; }
    if (/^agi_type\:\s+(.*)$/)         { $type         = $1; }
    if (/^agi_callerid\:\s+(.*)$/)     { $callerid     = $1; }
    if (/^agi_calleridname\:\s+(.*)$/) { $calleridname = $1; }
}
if ($V) {
    if ($V) { print STDERR "AGI Environment Dump:\n"; }
    foreach $i ( sort keys %AGI ) {
        if ($V) { print STDERR " -- $i = $AGI{$i}\n"; }
    }
}
if ($CLIOUT) {
    print STDERR
"****** DEBUG: |$unique_id|$channel|$extension|$type|$callerid|$calleridname|\n";
}
if ($SPEAK) {
    $AGI->stream_file('beep');
    sleep(1);
    $AGI->say_digits("$extension");
}
exit;
