#!/bin/sh -x

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

createdb -U $DBUSER $DBNAME
psql -U $DBUSER $DBNAME << EOF

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 (
  datetime timestamp,
  level float,
  virname varchar(80),
  status varchar(20),
  qname varchar(80),
  sender text,
  recipient text,
  size int,
  ip varchar(40)
);

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(255) DEFAULT '' NOT NULL,
  recipient varchar(255) 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);
EOF
