#!/usr/bin/perl -w
#---------------------------------------------------------------------
# gsgc
# Alamin GSM SMS Gateway mysql reader
#---------------------------------------------------------------------
# (C) Andrs Seco Hernndez, April 2000-Oct 2004
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
#---------------------------------------------------------------------

=pod

=head1 NAME

gsgdb2sms - Alamin GSM SMS Gateway mysql reader

=head1 SYNOPSIS

gsgdb2sms [-c config_file_name]

=head1 DESCRIPTION

gsgdb2sms is the component that reads a mysql database table and calls
gsgc (the client) to send messages. Database connect data, table name and
fields can be configured using its config file. It updates a field each time
a message is correctly sent.

=head1 OPTIONS

=over

=item -c <file_name>

(default: /etc/alamin/gsgdb2sms.conf) Sets the config file to be used. It is a perl formated file, be careful with the perl syntax.

=back

=head1 FILES

/etc/alamin/gsgdb2sms.conf

=head1 SEE ALSO

See also gsgc(1), gsgcmd(8), gsgmdd(8), gsgsmppin(8), gsgsmppout(8) and
gsgdb2sms(8).

=head1 BUGS

Send bugs to the author, please. I would like to keep the program without
bugs.

=head1 LICENSE

Alamin GSM SMS Gateway
Copyright (C) Andres Seco Hernandez and others.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

=head1 AUTHOR

Andres Seco Hernandez <AndresSH@alamin.org>.

=cut

use strict;

my $timetoterminate = 0;
$SIG{TERM} = \&TERMINATE;

use DBI();
use Sys::Syslog qw(:DEFAULT setlogsock);
use Getopt::Std;

my $program_name = "gsgdb2sms";
my $program_version = "v0.3.7 - Oct 25, 2004";

my $waittime = 1;
my $verbose = 1;
my $debug = 1;
my $dbname = "dbname";
my $dbhost = "dbhost";
my $dbuser = "dbuser";
my $dbpass = "dbpass";
my $sqltable = "SMSOUT";
my $sqlfieldkey = "id";
my $sqlfieldphone = "phone";
my $sqlfieldtext = "text";
my $sqlwhere = "status = 0";
my $sqlupdate = "status = 1";
my $gsgcpath = "/usr/bin/gsgc";
my $gsgcoptions = "--host localhost";
my $usesyslog = 1;
my $syslogfacility = "local0";
my $pidfile = "/var/run/alamin/gsgdb2sms.pid";
my $runasdaemon = 1;
my %opt;
getopts ("c:", \%opt);
my $configfile = $opt{"c"} || "/etc/alamin/gsgdb2sms.conf";
eval `cat $configfile`;

if ($runasdaemon) {
  use Proc::Daemon;
  Proc::Daemon::Init;
}

setlogsock "unix";
openlog($program_name,"pid",$syslogfacility);
umask 007;
open (PIDFILE,">".$pidfile);
print PIDFILE "$$";
close (PIDFILE);
logit("info","Starting Alamin GSM SMS Gateway - $program_name - $program_version") if ($verbose);

while(!$timetoterminate) {
  my $dbh = DBI->connect("DBI:mysql:database=".$dbname.";host=".$dbhost, $dbuser, $dbpass, {'RaiseError' => 1});
# selecciona registros pendientes
  my $sth = $dbh->prepare("SELECT ".$sqlfieldkey.",".$sqlfieldphone.",".$sqlfieldtext." FROM ".$sqltable." WHERE ".$sqlwhere);
  $sth->execute();
# recorre los pendientes actualmente
  my $result;
  while ($result = $sth->fetchrow_hashref()) {
    my $commandtorun = $gsgcpath." ".$gsgcoptions." --send ".$result->{$sqlfieldphone}." \"".$result->{$sqlfieldtext}."\"";
    my $answer = system($commandtorun);
    if (!$answer) {
# marca como enviado si el envo ha sido bueno
      logit("info","Updating SMS_OUTPUT id=".$result->{$sqlfieldkey}.".") if ($verbose);
      my $sthupdate = $dbh->prepare("UPDATE ".$sqltable." SET ".$sqlupdate." WHERE ".$sqlfieldkey."='".$result->{$sqlfieldkey}."'");
      $sthupdate->execute();
      $sthupdate->finish();
    } else {
      logit("info","ERROR sending id=".$result->{$sqlfieldkey}.", exitcode=$answer.");
    }
  }
  $sth->finish();
  $dbh->disconnect();
  logit("debug","Waiting $waittime seconds.") if ($debug);
  sleep $waittime;
}

logit("info","Exiting.") if ($verbose);
unlink $pidfile;
closelog;

sub TERMINATE {
  $timetoterminate = 1;
  logit("debug","SIGTERM received, ending tasks...") if ($debug);
}

sub logit {
  my ($log_type, $log_message) = @_;
  if ($usesyslog) {
    syslog($log_type,$log_message);
  }
}
