#!/usr/bin/perl

## $Id: obec.change.text.pl,v 1.1 2002/02/26 10:17:20 lord Exp $
##-----------------------------------------------------
## Change body of given article
##
## http://obec.sourceforge.net/
## Vadim Zaliva <lord@crocodile.org>
##-----------------------------------------------------

use ConfigReader::Simple  ;
use DBI                   ;
use Mail::Header          ;
use Date::Parse           ;
use POSIX qw(strftime)    ;

# ---------------- Constants --------------------------

$DBCHARSET = 'KOI8';

# ---------------------- Main -------------------------

# Check args

if($#ARGV != 1)
  {
    print "Usage: obec.change.text.pl <id> <filename>\n";
    exit(1);
  }

# Load CFG.
$config    = load_cfg("obec.fetcher.cfg");

# Connect to DB
($dbh, $drh) = db_connect($config);

# Do the work

$id    = $ARGV[0];
$fname = $ARGV[1];

open(IN,"< $fname") or die "Couldn't open file for reading $!\n";

while(<IN>)
  {
    $text = $text . $_;
  }
close IN;

$stmt->execute($text, $id);
$stmt->finish();

# Disconnect from the database.
db_disconnect();

print "Done.\n";

exit(0);

# ---------------------- Subroutines -------------------------

sub trim($)
  {
    my @out = @_;
    for(@out) {
      s/^\s+//;
      s/\s+$//;
    }
    return wantarray ? @out:$out[0];
  }
  
sub load_cfg($)
  {
    my $config = ConfigReader::Simple->new($_[0], [qw(Server DSN DBUser DBPassword ArticlesGroup)]);
    $config->parse();
    $config->_validate_keys();  
    return $config;
  };
             
           
sub db_disconnect()
  {
    $dbh->disconnect();
  }

sub db_connect($)
{
  my($config)=@_;

  my $dsn         = $config->get("DSN");
  my $db_user     = $config->get("DBUser");
  my $db_password = $config->get("DBPassword");
  
  print "Connecting to: $dsn as $db_user\n";
  $dbh = DBI->connect($dsn, $db_user, $db_password);
  $drh = DBI->install_driver("mysql");

  $stmt  = $dbh->prepare("UPDATE Texts SET text=? WHERE Id=?");

  return ($dbh, $drh);
}

