BEGIN;

DO $$
    BEGIN
        BEGIN
            ALTER TABLE partners ADD COLUMN sftp_gateway varchar(255);
        EXCEPTION
            WHEN duplicate_column THEN
            --do nothing
        END;
    END;
$$;

DO $$
    BEGIN
        BEGIN
            ALTER TABLE partners ADD COLUMN sftp_username varchar(255);
        EXCEPTION
            WHEN duplicate_column THEN
            --do nothing
        END;
    END;
$$;

DO $$
    BEGIN
        BEGIN
            ALTER TABLE partners ADD COLUMN sftp_password varchar(255);
        EXCEPTION
            WHEN duplicate_column THEN
            --do nothing
        END;
    END;
$$;

DO $$
    BEGIN
        BEGIN
            ALTER TABLE partners ADD COLUMN sftp_authent varchar(30);
        EXCEPTION
            WHEN duplicate_column THEN
            --do nothing
        END;
    END;
$$;

DO $$
    BEGIN
        BEGIN
            ALTER TABLE partners ADD COLUMN sftp_public_key text;
        EXCEPTION
            WHEN duplicate_column THEN
            --do nothing
        END;
    END;
$$;

DO $$
    BEGIN
        BEGIN
            ALTER TABLE partners ADD COLUMN sftp_server_port integer DEFAULT 22;
        EXCEPTION
            WHEN duplicate_column THEN
            --do nothing
        END;
    END;
$$;

DO $$
    BEGIN
        BEGIN
            ALTER TABLE partners ADD COLUMN gwsftp_public_key text;
        EXCEPTION
            WHEN duplicate_column THEN
            --do nothing
        END;
    END;
$$;

DO $$
    BEGIN
        BEGIN
            ALTER TABLE partners ADD COLUMN gwsftp_private_key text;
        EXCEPTION
            WHEN duplicate_column THEN
            --do nothing
        END;
    END;
$$;

DO $$
    BEGIN
        BEGIN
            ALTER TABLE partners ADD COLUMN gwsftp_port integer default 6622;
        EXCEPTION
            WHEN duplicate_column THEN
            --do nothing
        END;
    END;
$$;

DELETE FROM template_tasks WHERE rule_id IN (
    SELECT id FROM template_rules WHERE tpl_id IN (
        SELECT id FROM templates WHERE name IN ('default', 'déploiement_config')
    )
);

DELETE FROM template_rules WHERE tpl_id IN (
    SELECT id FROM templates WHERE name IN ('default', 'déploiement_config')
);

DELETE FROM templates WHERE name IN ('default', 'déploiement_config');


INSERT INTO templates (id, name) VALUES
  (1, 'default'),
	(2, 'déploiement_config');


insert into template_rules (id, tpl_id, rule_order, rule_mode, name, rule_when) values
      (1, 1, 0, 'recv', '{{flow_name}}', 'route_index == 1 and origin.Type == types.FTP and not origin.Type == types.SFTP and origin.IsClient and not origin.IsServer'),
      (2, 1, 1, 'send', '{{flow_name}}', 'route_index == 1'),
      (3, 1, 2, 'send', '{{flow_name}}_2', 'origin.Type == types.GWFTP or origin.Type == types.GWSFTP and route_index == 2'),
      (4, 1, 3, 'send', '{{flow_name}}_{{flow_index}}', 'route_index == last_index - 1'),
      (5, 1, 4, 'recv', '{{flow_name}}_FINAL', 'route_index == last_index and destination.Type != types.FTP and destination.Type != types.SFTP and not destination.IsServer'),
      (6, 1, 5, 'send', '{{flow_name}}_FINAL', 'route_index == last_index and destination.Type != types.FTP and destination.Type != types.SFTP and destination.IsServer'),
      (7, 1, 6, 'send', '{{flow_name}}', 'destination.Type == types.FTP or destination.Type == types.SFTP and route_index == last_index and destination.IsClient and not destination.IsServer'),
      (8, 1, 7, 'send', '{{flow_name}}_{{flow_index}}', 'default'),

      (9, 2, 0, 'send', '{{flow_name}}', 'route_index == 1'),
      (10, 2, 1, 'send', '{{flow_name}}_{{flow_index}}', 'route_index == last_index'),
      (11, 2, 2, 'send', '{{flow_name}}_{{flow_index}}', 'default');


insert into template_tasks (id, rule_id, task_where, task_when, task_order, type, task_delay, task_path, task_condition) values
      (1, 1, 'receiver', 'post', 0, 'FTPCOMMAND', 30000, 'R66PREPARETRANSFER -to {{next.destination.HostIdSsl}} -file #BASEPATH##FILE# -rule #ACCOUNT#_2', NULL),
      (2, 2, 'sender', 'post', 0, 'DELETE', 0, NULL, NULL),
      (3, 2, 'receiver', 'post', 0, 'COPYRENAME', 0, '#OUTPATH#/{{next.destination.HostId}}/#ORIGINALFILENAME#', 'next.destination.Type != types.FTP and next.destination.Type != types.SFTP'),
      (4, 2, 'receiver', 'post', 1, 'TRANSFER', 0, '-file #OUTPATH#/{{next.destination.HostId}}/#ORIGINALFILENAME# -to {{next.destination.HostIdSsl}} -rule {{next.name}} -copyinfo', 'next.destination.Type == types.R66 and next.destination.IsServer or next.destination.Type == types.GWFTP or next.destination.Type == types.GWSFTP'),
      (5, 2, 'receiver', 'post', 2, 'COPYRENAME', 0, '#INPATH#/#ORIGINALFILENAME#', 'destination.Type == types.R66 and destination_id in final_destinations'),
      (6, 2, 'receiver', 'post', 3, 'DELETE', 0, NULL, NULL),
      (7, 3, 'sender', 'post', 0, 'DELETE', 0, NULL, NULL),
      (8, 3, 'receiver', 'post', 0, 'COPYRENAME', 0, '#OUTPATH#/{{next.destination.HostId}}/#ORIGINALFILENAME#', 'next.destination.Type != types.FTP or next.destination.Type != type.SFTP'),
      (9, 3, 'receiver', 'post', 1, 'TRANSFER', 0, '-file #OUTPATH#/{{next.destination.HostId}}/#ORIGINALFILENAME# -to {{next.destination.HostIdSsl}} -rule {{next.name}} -copyinfo', 'next.destination.Type == types.R66 and next.destination.IsServer or next.destination.Type == types.GWFTP or next.destination.Type == types.GWSFTP'),
      (10, 3, 'receiver', 'post', 2, 'COPYRENAME', 0, '#INPATH#/#ORIGINALFILENAME#', 'destination.Type == types.R66 and destination_id in final_destinations'),
      (11, 3, 'receiver', 'post', 3, 'DELETE', 0, NULL, NULL),
      (12, 4, 'sender', 'post', 0, 'DELETE', 0, NULL, NULL),
      (13, 4, 'receiver', 'post', 0, 'COPYRENAME', 0, '#OUTPATH#/{{next.destination.HostId}}/#ORIGINALFILENAME#', 'next.destination.Type != types.FTP and next.destination.Type != types.SFTP or next.destination.IsServer'),
      (14, 4, 'receiver', 'post', 1, 'COPYRENAME', 0, '/var/lib/waarp/{{destination.HostId}}/ftp/{{next.destination.FtpClientUsername}}/{{flow_name}}/#ORIGINALFILENAME#', 'next.destination.Type == types.FTP or next.destination.Type == types.SFTP and next.destination.IsClient and not next.destination.IsServer'),
      (15, 4, 'receiver', 'post', 2, 'TRANSFER', 0, '-file #OUTPATH#/{{next.destination.HostId}}/#ORIGINALFILENAME# -to {{next.destination.HostIdSsl}} -rule {{next.name}} -copyinfo', 'next.destination.Type == types.R66 and next.destination.IsServer or next.destination.Type == types.GWFTP or next.destination.Type == type.GWSFTP'),
      (16, 4, 'receiver', 'post', 3, 'FTP', 30000, '-to {{next.destination.Ip}} -port {{next.destination.FtpServerPort}} -user {{next.destination.FtpServerUsername}} -pwd {{next.destination.FtpServerPassword}} -ssl {{next.destination.FtpServerSecure}} -file #OUTPATH#/{{next.destination.HostId}}/#ORIGINALFILENAME# -cwd {{next.destination.FtpServerDirectory}} -command put', 'next.destination.Type == types.FTP and next.destination.IsServer'),
	  (17, 4, 'receiver', 'post', 3, 'EXEC', 600000, './bin/sftpc.sh -put -host {{next.destination.IP}} -port {{next.destination.SftpServerPort}} -user [[next.destination.SftpUsername}} -pwd {{next.destination.SftpPassword}} -file #OUTPATH#{{next.destination.HostId}}/#ORIGINALFILENAME#', 'next.destination.Type == "types.SFTP and next.destination.IsServer'),
      (18, 4, 'receiver', 'post', 4, 'COPYRENAME', 0, '#INPATH#/#ORIGINALFILENAME#', 'destination.Type == types.R66 and destination_id in final_destinations'),
      (19, 4, 'receiver', 'post', 5, 'DELETE', 0, NULL, NULL),
      (20, 4, 'receiver', 'post', 6, 'EXECOUTPUT', 30000, 'rm #OUTPATH#/{{next.destination.HostId}}/#ORIGINALFILENAME#', 'next.destination.Type == types.FTP or next.destination.Type == types.SFTP and next.destination.IsServer'),
      (21, 5, 'sender', 'post', 0, 'DELETE', 0, NULL, NULL),
      (22, 5, 'receiver', 'post', 0, 'MOVERENAME', 0, '#INPATH#/#ORIGINALFILENAME#', 'destination.Type == types.R66 and destination_id in final_destinations'),
      (23, 6, 'sender', 'post', 0, 'DELETE', 0, NULL, NULL),
      (24, 6, 'receiver', 'post', 0, 'MOVERENAME', 0, '#INPATH#/#ORIGINALFILENAME#', 'destination.Type == types.R66 and destination_id in final_destinations'),
      (25, 7, 'sender', 'post', 0, 'FTPCOMMAND', 0, 'NONE', NULL),
      (26, 8, 'sender', 'post', 0, 'DELETE', 0, NULL, NULL),
      (27, 8, 'receiver', 'post', 0, 'COPYRENAME', 0, '#OUTPATH#/{{next.destination.HostId}}/#ORIGINALFILENAME#', 'next.destination.Type != types.FTP and next.destination.Type != types.SFTP'),
      (28, 8, 'receiver', 'post', 1, 'TRANSFER', 0, '-file #OUTPATH#/{{next.destination.HostId}}/#ORIGINALFILENAME# -to {{next.destination.HostIdSsl}} -rule {{next.name}} -copyinfo', 'next.destination.Type == types.R66 and next.destination.IsServer or next.destination.Type == types.GWFTP or next.destination.Type == types.GWSFTP'),
      (29, 8, 'receiver', 'post', 2, 'COPYRENAME', 0, '#INPATH#/#ORIGINALFILENAME#', 'destination.Type == types.R66 and destination_id in final_destinations'),
      (30, 8, 'receiver', 'post', 3, 'DELETE', 0, NULL, NULL),

      (31, 9, 'sender', 'post', 0, 'DELETE', 0, NULL, NULL),
      (32, 9, 'receiver', 'post', 0, 'COPYRENAME', 0, '#OUTPATH#/{{next.destination.HostId}}/#ORIGINALFILENAME#', 'route_index != last_index'),
      (33, 9, 'receiver', 'post', 1, 'DELETE', 0, NULL, 'route_index != last_index'),
      (34, 9, 'receiver', 'post', 2, 'TRANSFER', 0, '-file #OUTPATH#/{{next.destination.HostId}}/#ORIGINALFILENAME# -to {{next.destination.HostIdSsl}} -rule {{next.name}} -copyinfo', 'route_index != last_index'),
      (35, 9, 'receiver', 'post', 3, 'EXECJAVA', 1, 'org.waarp.manager.tasks.UpdateConfExtended #HOSTID#', 'route_index == last_index and destination.Type != types.GWSFTP'),
      (36, 9, 'receiver', 'post', 3, 'EXECJAVA', 1, 'org.waarp.manager.tasks.UpdateConfExtendedSFTP #HOSTID#', 'route_index == last_index and destination.Type == types.GWSFTP'),
      (37, 9, 'receiver', 'post', 4, 'DELETE', 0, NULL, 'route_index == last_index'),
      (38, 10, 'sender', 'post', 0, 'DELETE', 0, NULL, NULL),
      (39, 10, 'receiver', 'post', 0, 'EXECJAVA', 1, 'org.waarp.manager.tasks.UpdateConfExtended #HOSTID#', 'destination.Type != types.GWSFTP'),
      (40, 10, 'receiver', 'post', 0, 'EXECJAVA', 1, 'org.waarp.manager.tasks.UpdateConfExtendedSFTP #HOSTID#', 'destination.Type == types.GWSFTP'),
      (41, 10, 'receiver', 'post', 1, 'DELETE',  0, NULL, NULL),
      (42, 11, 'sender', 'post', 0, 'DELETE', 0, NULL, NULL),
      (43, 11, 'receiver', 'post', 0, 'COPYRENAME', 0, '#OUTPATH#/{{next.destination.HostId}}/#ORIGINALFILENAME#', NULL),
      (44, 11, 'receiver', 'post', 1, 'DELETE', 0, NULL, NULL),
      (45, 11, 'receiver', 'post', 2, 'TRANSFER', 0, '-file #OUTPATH#/{{next.destination.HostId}}/#ORIGINALFILENAME# -to {{next.destination.HostIdSsl}} -rule {{next.name}} -copyinfo', NULL);

alter sequence templates_id_seq restart with 3;
alter sequence template_rules_id_seq restart with 12;
alter sequence template_tasks_id_seq restart with 46;

COMMIT;
