GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO homer_user;

CREATE TABLE IF NOT EXISTS alias (
  id SERIAL NOT NULL,
  gid INTEGER NOT NULL DEFAULT 0,
  ip varchar(80) NOT NULL DEFAULT '',
  port INTEGER NOT NULL DEFAULT '0', 
  capture_id varchar(100) NOT NULL DEFAULT '',
  alias varchar(100) NOT NULL DEFAULT '',
  status smallint NOT NULL DEFAULT 0,  
  created timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (id)
);  

CREATE UNIQUE INDEX alias_id ON alias (id);
CREATE UNIQUE INDEX alias_id_port_captid ON alias (id,port,capture_id);
CREATE INDEX alias_ip_idx ON alias (ip);


INSERT INTO alias (id, gid, ip, port, capture_id, alias, status, created) VALUES
(1, 10, '192.168.0.30', 0, 'homer01', 'proxy01', 1, '2014-06-12 20:36:50'),
(2, 10, '192.168.0.4', 0, 'homer01', 'acme-234', 1, '2014-06-12 20:37:01'),
(22, 10, '127.0.0.1:5060', 0, 'homer01', 'sip.local.net', 1, '2014-06-12 20:37:01');


CREATE TABLE IF NOT EXISTS "group" (
  gid INTEGER NOT NULL DEFAULT 0,
  name varchar(100) NOT NULL DEFAULT ''
);

CREATE UNIQUE INDEX group_gid ON "group" (gid);
INSERT INTO "group" (gid, name) VALUES (10, 'Administrator');


CREATE TABLE IF NOT EXISTS link_share (
  id SERIAL NOT NULL,
  uid INTEGER NOT NULL DEFAULT 0,
  uuid varchar(120) NOT NULL DEFAULT '',
  data text NOT NULL,
  expire timestamp NOT NULL DEFAULT '2032-12-31 00:00:00',
  active smallint NOT NULL DEFAULT '1',
  PRIMARY KEY (id)
)  ;


CREATE TABLE IF NOT EXISTS setting (
  id SERIAL NOT NULL,
  uid INTEGER NOT NULL DEFAULT '0',
  param_name varchar(120) NOT NULL DEFAULT '',
  param_value text NOT NULL,
  valid_param_from timestamp NOT NULL DEFAULT '2012-01-01 00:00:00',
  valid_param_to timestamp NOT NULL DEFAULT '2032-12-01 00:00:00',
  param_prio integer NOT NULL DEFAULT '10',
  active INTEGER NOT NULL DEFAULT '1',
  PRIMARY KEY (id)
);

CREATE UNIQUE INDEX setting_id ON setting (uid,param_name);
CREATE INDEX setting_param_name ON setting (param_name);
CREATE INDEX setting_uid ON setting (uid);

INSERT INTO setting (id, uid, param_name, param_value, valid_param_from, valid_param_to, param_prio, active) VALUES
(1, 1, 'timerange', '{"from":"2015-05-26T18:34:42.654Z","to":"2015-05-26T18:44:42.654Z"}', '2012-01-01 00:00:00', '2032-12-01 00:00:00', 10, 1);


CREATE TABLE IF NOT EXISTS user_menu (
  id varchar(125) NOT NULL DEFAULT '',
  name varchar(100) NOT NULL DEFAULT '',
  alias varchar(200) NOT NULL DEFAULT '',
  icon varchar(100) NOT NULL DEFAULT '',
  weight INTEGER NOT NULL DEFAULT '10',
  active INTEGER NOT NULL DEFAULT '1'
);

CREATE UNIQUE INDEX user_menu_id ON "user_menu" (id);

INSERT INTO user_menu (id, name, alias, icon, weight, active) VALUES
('_1426001444630', 'SIP Search', 'search', 'fa-search', 10, 1),
('_1427728371642', 'Home', 'home', 'fa-home', 1, 1),
('_1431721484444', 'Alarms', 'alarms', 'fa-warning', 20, 1);
