#!/usr/bin/perl
use File::Copy;
require "prontolib.pl";
&read_prefs();
$conn=&open_db_conn;
&add_trueregex;

sub add_trueregex
{
	if ($prefs{'DatabaseDriver'} eq "mysql") {
		$sql = "alter table filters add trueregex int";
	} elsif ($prefs{'DatabaseDriver'} eq "Pg") {
		$sql = "alter table filters add column trueregex int";
	} elsif ($prefs{'DatabaseDriver'} eq "CSV") {
		&add_csv_column("$prefs{'MailDir'}/filters","trueregex");
		return 1;
	}	
	$query = $conn->prepare($sql);
	$query->execute();
}

sub add_csv_column
{
	my ($filename, $column) = @_;
	open (FILE, $filename);
	@file = <FILE>;
	close (FILE);
	$columnlist = $file[0];
	if ($columnlist =~ m/\b$column\b/) { print "has been added already\n"; return 1; }
	if ($column eq "ref") {
		$columnlist=~ s/,score/,score,ref/i;
	} elsif ($column eq "count") {
		$columnlist=~ s/,name/,name,count/i;
	} elsif ($column eq "trueregex") {
		$columnlist=~ s/,count/,count,trueregex/i;
	}	
	$file[0] = $columnlist;
	copy($filename,$filename."_bak");
	open(FILE,">$filename");
	foreach(@file) { print FILE $_  }
	close(FILE);
}
