#!/usr/bin/perl
#
# wnam-gammu-smsd-bridge.pl
# http://www.netams.com/
# see http://wammu.eu/docs/manual/smsd/run.html
#

use constant WNAM_HOST => "127.0.0.1";

use IO::Socket::INET;

$| = 1; 

my $sock = new IO::Socket::INET(PeerAddr => WNAM_HOST, PeerPort => 20001, Proto => 'tcp', Timeout => 0.5) or undef $sock;

if (defined $sock) {
        my $msgs=$ENV{"SMS_MESSAGES"};
        if (defined $msgs) {
	        for (my $i=1; $i<=$msgs; $i++) {
	                my $tel = $ENV{"SMS_".$i."_NUMBER"};
	                my $code = $ENV{"SMS_".$i."_TEXT"};
	            print $sock "SMSRECV PHONE=$tel TEXT=$code\n";
	        }
        }
        $sock->shutdown(2);
        $sock->close;
	}

