#!/bin/sh -x

# Deleting old records may be done with:
# DELETE FROM log WHERE datetime<SUBDATE(now(),INTERVAL 90 DAY);

DBUSER=${DBUSER:=root}
DBNAME=${DBNAME:=sagator}
ARGS=${ARGS:="-p"}
NOW="`date +%s`"

mysql -u $DBUSER $ARGS << EOF
CREATE DATABASE $DBNAME;
USE $DBNAME;

# update sagator's password and uncomment this line:
#GRANT INSERT,SELECT,UPDATE,DELETE ON sagator.* TO 'sagator'@'127.0.0.1' IDENTIFIED BY 'your_secret';

CREATE TABLE webaccess (
  email varchar(40) NOT NULL,
  pass varchar(40) NOT NULL,
  perms varchar(20) default '' NOT NULL,
  datetime datetime default NULL,
  lang varchar(20) default '' NOT NULL,
  showrows int(11) default 50 NOT NULL
);

CREATE TABLE log (
  id int unsigned NOT NULL auto_increment,
  datetime timestamp,
  level float,
  virname varchar(80),
  status varchar(20),
  qname varchar(80),
  sender text,
  recipient text,
  size int,
  ip varchar(40),
  PRIMARY KEY (id)
);

CREATE TABLE signatures (
  prio int,
  email text,
  ckey varchar(16)
);

CREATE TABLE greylist (
  flags varchar(2),
  timestamp int DEFAULT $NOW NOT NULL,
  expire int DEFAULT -1 NOT NULL,
  ip varchar(16) DEFAULT '' NOT NULL,
  sender varchar(128) DEFAULT '' NOT NULL,
  recipient varchar(128) DEFAULT '' NOT NULL,
  repeats int DEFAULT 0 NOT NULL,
  block_count int DEFAULT 0 NOT NULL,
  pass_count int DEFAULT 0 NOT NULL,
  created int DEFAULT $NOW NOT NULL,
  last_update int DEFAULT $NOW NOT NULL,
  PRIMARY KEY ( flags, ip, sender, recipient )
);

CREATE INDEX greylist_idx ON greylist (ip,sender,recipient);

INSERT INTO greylist (flags,ip) VALUES ('WA','127.%');
INSERT INTO greylist (flags,ip) VALUES ('WA','10.%');
INSERT INTO greylist (flags,ip) VALUES ('WA','172.16.%');
INSERT INTO greylist (flags,ip) VALUES ('WA','192.168.%');

EOF
