#!/usr/bin/perl -w
#---------------------------------------------------------------------------
# eisfax-db-blob.pl - MySQL blob
#
# Creation:     2011-05-01 hb
# Last Update:  $Id$
#
# Copyright (c) 2011-@@YEAR@@ Holger Bruenjes, holgerbruenjes(at)gmx(dot)net
#
# 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.
#----------------------------------------------------------------------------

use strict;
use DBI;
use Getopt::Long;
$Getopt::Long::ignorecase = 0;
$Getopt::Long::bundling = 1;
use Fcntl;  # for O_RDONLY, O_WRONLY, O_CREAT
use FileHandle;

sub usage;

my ($db_host, $db_name, $db_user, $db_passwd, $db_table,
    $db_file, $db_type, $db_commit, $db_server, $db_help,
    $db_version);

# set default db server
$db_server = 'mysql';

# get cmd arguments
if (@ARGV == 0) {
    &usage();
}

# get cmd arguments
if (@ARGV == 1) {

GetOptions (

"help"          => \$db_help,
"version|v"     => \$db_version
) or &usage();
}

# extract comandline
GetOptions (

"host|h=s"      => \$db_host,
"db|D=s"        => \$db_name,
"user|U=s"      => \$db_user,
"paswd|P=s"     => \$db_passwd,
"file|f=s"      => \$db_file,
"table|t=s"     => \$db_table,
"type|T=s"      => \$db_type,
"commit|c=s"    => \$db_commit,
"server|s=s"    => \$db_server

) or exit (1);

# print version information
if ($db_version) {
    print "EisFAX_my_blob.pl\n";
    print "version 0.0.1\n";
    exit (1);
}


if ($db_help) {
    &usage();
}


# print usage
sub usage {
    print " Usage: $0 \n\n";

    print "    -f, --file=filename     file to read from\n";
    print "    -D, --db=database       database to connect to\n";
    print "    -h, --host=hostname     machine running the database server\n";
    print "    -t, --table             table name to insert to\n";
    print "    -T, --type              like  SEND or RECV\n";
    print "    -c, --commit            commit Id from HylaFAX\n";
    print "    -s, --server            database server name, default 'mysql'\n";
    print "    -U, --user=login        database user for login\n";
    print "    -P, --passwd=password   users password\n";
    print "    -v, --version           show program version\n";
    print "        --help              show this help\n\n";

    exit (1);
}

# read fax file and store into $db_data
my $fh = new FileHandle;
my ($size, $db_data);

sysopen ($fh, $db_file, O_RDONLY)
        or die "Cannot read $db_file: $!\n";
binmode ($fh);   # helpfull for binary data
$size = (stat ($fh))[7];
sysread ($fh, $db_data, $size) == $size
        or die "Failed to write entire file $db_file: $!\n";
$fh->close ();

# set database connections
my $dsn = "DBI:$db_server:database=$db_name;host=$db_host";
my $dbh = DBI->connect ($dsn, "$db_user", "$db_passwd")
        or die "Cannot connect the Server\n";

# Bilddatei in Datenbank-Tabelle speichern,
if ($db_server eq 'mysql') {
    $dbh->do ("INSERT INTO $db_table (type, commid ,data) VALUES(?,?,?)",
                undef,
                "$db_type", "$db_commit", $db_data);
}

$dbh->disconnect ();

exit (0);
