CONNECT 'localhost:config' USER 'SYSDBA' PASSWORD 'NIAMA';

execute ibeblock
as
begin
  if (not exists (select *
                  from   SYSTEM_CFG c
                  where  c.CFG_TREE = 'POSitive_3'
                         and c.CFG_CLASS = 'global'
                         and c.CFG_KEY = 'init.mod_ALoyalty_exchange'
                         and c.CFG_VALUE = 'global')) then
  begin
    execute statement 'INSERT INTO SYSTEM_CFG (CFG_TREE, CFG_CLASS, CFG_CLASS_CRITERIA, CFG_KEY, CFG_VALUE)
    VALUES (''POSitive_3'', ''global'', '''', ''init.mod_ALoyalty_exchange'',''global'');';

    execute statement 'commit work';
  end
end;

insert into SYSTEM_CFG (CFG_TREE, CFG_CLASS, CFG_CLASS_CRITERIA, CFG_KEY, CFG_VALUE)
select 'POSitive_3', 'global', '', s.CFG_KEY, s.CFG_VALUE
from   SYSTEM_CFG s
where  s.CFG_TREE = 'POSitive_3'
       and s.CFG_KEY like 'init.%'
group by 4, 1, 2, 3, 5
having min(s.CFG_CLASS) = 'station';

commit work;

delete from SYSTEM_CFG s
where  s.CFG_TREE = 'POSitive_3'
       and s.CFG_CLASS = 'station'
       and s.CFG_KEY like 'init.%';

commit work;

insert into SYSTEM_CFG (CFG_TREE, CFG_CLASS, CFG_CLASS_CRITERIA, CFG_KEY, CFG_VALUE)
select 'POSitive_3', 'global', '', 'init.common', 'global'
from   RDB$DATABASE d
where  not exists (select c.CFG_KEY from SYSTEM_CFG c where c.CFG_TREE = 'POSitive_3' and c.CFG_KEY = 'init.common');

insert into SYSTEM_CFG (CFG_TREE, CFG_CLASS, CFG_CLASS_CRITERIA, CFG_KEY, CFG_VALUE)
select 'POSitive_3', 'global', '', 'init.db.system','global'
from   RDB$DATABASE d
where  not exists (select c.CFG_KEY from SYSTEM_CFG c where c.CFG_TREE = 'POSitive_3' and c.CFG_KEY = 'init.db.system');

insert into SYSTEM_CFG (CFG_TREE, CFG_CLASS, CFG_CLASS_CRITERIA, CFG_KEY, CFG_VALUE)
select 'POSitive_3', 'global', '', 'init.system.password','global'
from   RDB$DATABASE d
where  not exists (select c.CFG_KEY from SYSTEM_CFG c where c.CFG_TREE = 'POSitive_3' and c.CFG_KEY = 'init.system.password');

insert into SYSTEM_CFG (CFG_TREE, CFG_CLASS, CFG_CLASS_CRITERIA, CFG_KEY, CFG_VALUE)
select 'POSitive_3', 'global', '', 'init.reports.path', 'global'
from   RDB$DATABASE d
where  not exists (select c.CFG_KEY from SYSTEM_CFG c where c.CFG_TREE = 'POSitive_3' and c.CFG_KEY = 'init.reports.path');

insert into SYSTEM_CFG (CFG_TREE, CFG_CLASS, CFG_CLASS_CRITERIA, CFG_KEY, CFG_VALUE)
select 'POSitive_3', 'global', '', 'init.db_related', 'global'
from   RDB$DATABASE d
where  not exists (select c.CFG_KEY from SYSTEM_CFG c where c.CFG_TREE = 'POSitive_3' and c.CFG_KEY = 'init.db_related');

insert into SYSTEM_CFG (CFG_TREE, CFG_CLASS, CFG_CLASS_CRITERIA, CFG_KEY, CFG_VALUE)
select 'POSitive_3', 'global', '', 'init.oldconfiguration', 'global'
from   RDB$DATABASE d
where  not exists (select c.CFG_KEY from SYSTEM_CFG c where c.CFG_TREE = 'POSitive_3' and c.CFG_KEY = 'init.oldconfiguration');

insert into SYSTEM_CFG (CFG_TREE, CFG_CLASS, CFG_CLASS_CRITERIA, CFG_KEY, CFG_VALUE)
select 'POSitive_3', 'global', '', 'init.local', 'station'
from   RDB$DATABASE d
where  not exists (select c.CFG_KEY from SYSTEM_CFG c where c.CFG_TREE = 'POSitive_3' and c.CFG_KEY = 'init.local');

insert into SYSTEM_CFG (CFG_TREE, CFG_CLASS, CFG_CLASS_CRITERIA, CFG_KEY, CFG_VALUE)
select 'POSitive_3', 'global', '', 'init.alias', 'station'
from   RDB$DATABASE d
where  not exists (select c.CFG_KEY from SYSTEM_CFG c where c.CFG_TREE = 'POSitive_3' and c.CFG_KEY = 'init.alias');

insert into SYSTEM_CFG (CFG_TREE, CFG_CLASS, CFG_CLASS_CRITERIA, CFG_KEY, CFG_VALUE)
select 'POSitive_3', 'global', '', 'init.dbstation_related', 'station'
from   RDB$DATABASE d
where  not exists (select c.CFG_KEY from SYSTEM_CFG c where c.CFG_TREE = 'POSitive_3' and c.CFG_KEY = 'init.dbstation_related');

commit work;

