.bail on
BEGIN;

-- Tables
create table "config"("key" text primary key, "value") without rowid;
create table "newsgroups"("name" text primary key, "server" text, "last" int) without rowid;
create table "xpost"("art" int, "group" text, primary key("group", "art")) without rowid;
create table "art"("id" integer primary key, "data" blob, "subject" text, "messageid" text unique, "parent" text, "time" int, "posted" int, "date" int, "read" int);
create table "servers"("name" text primary key, "port" int, "last" int, "wait" int, "date" text) without rowid;

-- Views (write-only)
create view "incoming" as select '' as "id", '' as "data", '' as "server" where 0;
create view "edited" as select 0 as "id", '' as "data", 0 as "posted" where 0;

-- Indexes
create index "art_1" on "art"("date" desc);

-- Triggers
create trigger incoming_1 instead of insert on incoming begin
  insert or fail into art(data,subject,messageid,parent,time,posted,date) values(new.data,header(new.data,'subject'),coalesce(header(new.data,'message-id'),new.id),right_msgid(header(new.data,'references')),now(),-1,parse_date(header(new.data,'date')));
  insert or fail into xpost(art,"group") select last_insert_rowid(),name from newsgroups where instr(' '||replace(header(new.data,'newsgroups'),',',' ')||' ',' '||name||' ');
end;
create trigger edited_0 instead of insert on edited when new.posted=0 begin
  update art set data=new.data, subject=header(new.data,'subject'), time=now(), posted=0, date=now() where id=new.id;
  insert or ignore into xpost("art","group") values(new.id,'!draft');
end;
create trigger edited_1 instead of insert on edited when new.posted=1 begin
  update or rollback art set data=new.data, subject=header(new.data,'subject'), messageid=header(new.data,'message-id'), parent=right_msgid(header(new.data,'references')), time=now(), posted=1, date=now() where id=new.id and posted=0;
  insert or rollback into xpost(art,"group") select new.id,name from newsgroups where instr(' '||replace(header(new.data,'newsgroups'),',',' ')||' ',' '||name||' ');
  update xpost set "group"='!posted' where "group"='!draft' and "art"=new.id;
end;
create trigger art_0 after delete on art begin
  delete from xpost where art = old.id;
end;

-- Config
INSERT INTO config VALUES('/AltHeadersQuery','with z(id,level,msgid,subj) as (select id,1,messageid,subject from art where case when length($ARG) then (cast($ARG as int)=id or $ARG=messageid) else parent is null end union all select a.id,z.level+1,a.messageid,a.subject from art a,z on a.parent=z.msgid order by 2 desc) select id,format_row(''[33]8,__2>.__[1]98,'',id,level,subj) from z;');
INSERT INTO config VALUES('/Color',1);
INSERT INTO config VALUES('/CountDownloads',1);
INSERT INTO config VALUES('/Editor','vi ~/.bystand/article');
INSERT INTO config VALUES('/HeadersQuery','select distinct a.id,format_row(''[33]8,_[1;31]&_[1]102,'',a.id,case a.posted when -1 then '' '' when 0 then ''*'' when 1 then ''P'' end,a.subject) from art a, xpost x where x."group" glob $GROUP and a.id=x.art order by a.date desc limit 60 offset $MORE*60;');
INSERT INTO config VALUES('/InternetVersion',4);
INSERT INTO config VALUES('/NoQuoteSignature',1);
INSERT INTO config VALUES('/Pager','less -Rf');
INSERT INTO config VALUES('/PostTo','select distinct x."server",119 from "newsgroups" x, "art" y where length($A)=0 and y."id" = $N and replace(header(y."data",''newsgroups'')||'' '','','','' '') glob (x."name" || '' *'') union all select $A,119 where length($A);');
INSERT INTO config VALUES('/PrependHeaders','select ''Date: ''||format_date(now());');
INSERT INTO config VALUES('/Progress',1);
INSERT INTO config VALUES('/ViewColors',1);

-- Macros
INSERT INTO config VALUES(',g','select format_row(''!_[1;32]80,_[1;35]8,__[1;36]&'',"group",count(),format_date(max("date"))),max("date") from "art","xpost" on "art"="id" group by "group" order by 2 desc;');
INSERT INTO config VALUES(',h','select format_row(''![33]8,__[1]102,'',a.id,a.subject) from art a,art b where b.id=$N and b.messageid=a.parent order by a.date desc;');
INSERT INTO config VALUES(',u','select a.id from art a,art b where b.id=$N and a.messageid=b.parent union all select ''echo Not a follow-up message.'' from art where id=$N and parent is null;');

COMMIT;
