#!/usr/bin/perl

use DBI;
use POSIX qw(strftime);

$db_user="aktuell";
$db_password="aktuell";
$dbi_string="DBI:mysql:hostname=192.168.4.8;database=nadiraktuell";

die unless ($dbh=DBI->connect($dbi_string,$db_user,$db_password));


### prepare
$mo_insert_q="insert into content_objekt (titel, urheber, freigeschaltet, datum, ishtml, storage_path, thema_id, contentdata, created, lastchange, mail_sent) values (?,?,0,?,0,?,0,?,NOW(),NOW(),0)";
$mo_insert_sth=$dbh->prepare($mo_insert_q);

# setze datum

$datum = &POSIX::strftime( '%Y%m%d', localtime time );
$storage_path=&POSIX::strftime( '%Y/%m/%d', localtime time );  


$headermode=1;
$titel="";$from="";$contentdata="";
while (<>) {
	if($headermode==1) {
		if (/^Subject: (.*)/) {
			$titel=$1;
	  		if (/^Subject: <nadir-aktuell> (.*)/) {
	    		$titel=$1;
			}
		}
		if (/^From: (.*)/) {
			$from = $1;
		}
		if (/^$/) {
	 	 print "Switched to Body\n";
	  	$headermode=0;
		}	      
    }
	else {
	  $contentdata = $contentdata . $_;
	}
}
    print "$datum $storage_path $from $titel \n";
    $mo_insert_sth->execute($titel, $from, $datum, $storage_path, $contentdata);


1;

