BEGIN;
    delete from template_tasks where rule_id IN (
        SELECT id from template_rules where tpl_id IN (1, 2)
    );
    DELETE from template_rules where tpl_id IN (1, 2);


insert into template_rules (tpl_id, rule_order, rule_mode, name, rule_when) values
      (1, 0, 'auto', '{{flow_name}}', 'route_index == 1 and origin.Type == types.FTP and not origin.Type == types.SFTP and origin.IsClient and not origin.IsServer');
insert into template_tasks (rule_id, task_where, task_when, task_order, type, task_delay, task_path, task_condition) values
      (currval('template_rules_id_seq'), 'receiver', 'post', 0, 'FTPCOMMAND', 30000, 'R66PREPARETRANSFER -to {{next.destination.HostIdSsl}} -file #BASEPATH##FILE# -rule #ACCOUNT#_2', NULL);

insert into template_rules (tpl_id, rule_order, rule_mode, name, rule_when) values
      (1, 1, 'auto', '{{flow_name}}', 'route_index == 1');
insert into template_tasks (rule_id, task_where, task_when, task_order, type, task_delay, task_path, task_condition) values
      (currval('template_rules_id_seq'), 'sender', 'post', 0, 'DELETE', 0, NULL, NULL),
      (currval('template_rules_id_seq'), 'receiver', 'post', 0, 'COPYRENAME', 0, '#OUTPATH#/{{next.destination.HostId}}/#ORIGINALFILENAME#', 'next.destination.Type != types.FTP and next.destination.Type != types.SFTP'),
      (currval('template_rules_id_seq'), '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'),
      (currval('template_rules_id_seq'), 'receiver', 'post', 2, 'COPYRENAME', 0, '#INPATH#/#ORIGINALFILENAME#', 'destination.Type == types.R66 and destination_id in final_destinations'),
      (currval('template_rules_id_seq'), 'receiver', 'post', 3, 'DELETE', 0, NULL, NULL);

insert into template_rules (tpl_id, rule_order, rule_mode, name, rule_when) values
      (1, 2, 'auto', '{{flow_name}}_2', 'origin.Type == types.GWFTP or origin.Type == types.GWSFTP and route_index == 2');
insert into template_tasks (rule_id, task_where, task_when, task_order, type, task_delay, task_path, task_condition) values
      (currval('template_rules_id_seq'), 'sender', 'post', 0, 'DELETE', 0, NULL, NULL),
      (currval('template_rules_id_seq'), 'receiver', 'post', 0, 'COPYRENAME', 0, '#OUTPATH#/{{next.destination.HostId}}/#ORIGINALFILENAME#', 'next.destination.Type != types.FTP or next.destination.Type != types.SFTP'),
      (currval('template_rules_id_seq'), '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'),
      (currval('template_rules_id_seq'), 'receiver', 'post', 2, 'COPYRENAME', 0, '#INPATH#/#ORIGINALFILENAME#', 'destination.Type == types.R66 and destination_id in final_destinations'),
      (currval('template_rules_id_seq'), 'receiver', 'post', 3, 'DELETE', 0, NULL, NULL);

insert into template_rules (tpl_id, rule_order, rule_mode, name, rule_when) values
      (1, 3, 'auto', '{{flow_name}}_{{flow_index}}', 'route_index == last_index - 1');
insert into template_tasks (rule_id, task_where, task_when, task_order, type, task_delay, task_path, task_condition) values
      (currval('template_rules_id_seq'), 'sender', 'post', 0, 'DELETE', 0, NULL, NULL),
      (currval('template_rules_id_seq'), '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'),
      (currval('template_rules_id_seq'), 'receiver', 'post', 1, 'COPYRENAME', 0, '#HOMEPATH#/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'),
      (currval('template_rules_id_seq'), '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 == types.GWSFTP'),
      (currval('template_rules_id_seq'), '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'),
	  (currval('template_rules_id_seq'), 'receiver', 'post', 3, 'EXEC', 600000, './bin/sftpc.sh #HOSTID# --put --host {{next.destination.Ip}} --port {{next.destination.SftpServerPort}} --username {{next.destination.SftpUsername}} --password {{next.destination.SftpPassword}} #OUTPATH#/{{next.destination.HostId}}/#ORIGINALFILENAME#', 'next.destination.Type == types.SFTP and next.destination.IsServer and next.destination.SftpAuthent == 1'),
	  (currval('template_rules_id_seq'), 'receiver', 'post', 3, 'EXEC', 600000, './bin/sftpc.sh #HOSTID# --put --host {{next.destination.Ip}} --port {{next.destination.SftpServerPort}} --username {{next.destination.SftpUsername}} #OUTPATH#/{{next.destination.HostId}}/#ORIGINALFILENAME#', 'next.destination.Type == types.SFTP and next.destination.IsServer and next.destination.SftpAuthent == 2'),
	  (currval('template_rules_id_seq'), 'receiver', 'post', 3, 'EXEC', 600000, './bin/sftpc.sh #HOSTID# --put --host {{next.destination.Ip}} --port {{next.destination.SftpServerPort}} --username {{next.destination.SftpUsername}} --password {{next.destination.SftpPassword}} #OUTPATH#/{{next.destination.HostId}}/#ORIGINALFILENAME#', 'next.destination.Type == types.SFTP and next.destination.IsServer and next.destination.SftpAuthent == 3'),
      (currval('template_rules_id_seq'), 'receiver', 'post', 4, 'COPYRENAME', 0, '#INPATH#/#ORIGINALFILENAME#', 'destination.Type == types.R66 and destination_id in final_destinations'),
      (currval('template_rules_id_seq'), 'receiver', 'post', 5, 'DELETE', 0, NULL, NULL),
      (currval('template_rules_id_seq'), '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');

insert into template_rules (tpl_id, rule_order, rule_mode, name, rule_when) values
      (1, 4, 'auto', '{{flow_name}}_FINAL', 'route_index == last_index and destination.Type != types.FTP and destination.Type != types.SFTP and not destination.IsServer');
insert into template_tasks (rule_id, task_where, task_when, task_order, type, task_delay, task_path, task_condition) values
      (currval('template_rules_id_seq'), 'sender', 'post', 0, 'DELETE', 0, NULL, NULL),
      (currval('template_rules_id_seq'), 'receiver', 'post', 0, 'MOVERENAME', 0, '#INPATH#/#ORIGINALFILENAME#', 'destination.Type == types.R66 and destination_id in final_destinations');

insert into template_rules (tpl_id, rule_order, rule_mode, name, rule_when) values
      (1, 5, 'auto', '{{flow_name}}_FINAL', 'route_index == last_index and destination.Type != types.FTP and destination.Type != types.SFTP and destination.IsServer');
insert into template_tasks (rule_id, task_where, task_when, task_order, type, task_delay, task_path, task_condition) values
      (currval('template_rules_id_seq'), 'sender', 'post', 0, 'DELETE', 0, NULL, NULL),
      (currval('template_rules_id_seq'), 'receiver', 'post', 0, 'MOVERENAME', 0, '#INPATH#/#ORIGINALFILENAME#', 'destination.Type == types.R66 and destination_id in final_destinations');

insert into template_rules (tpl_id, rule_order, rule_mode, name, rule_when) values
      (1, 6, 'auto', '{{flow_name}}', 'destination.Type == types.FTP or destination.Type == types.SFTP and route_index == last_index and destination.IsClient and not destination.IsServer');
insert into template_tasks (rule_id, task_where, task_when, task_order, type, task_delay, task_path, task_condition) values
      (currval('template_rules_id_seq'), 'sender', 'post', 0, 'FTPCOMMAND', 0, 'NONE', NULL);

insert into template_rules (tpl_id, rule_order, rule_mode, name, rule_when) values
      (1, 7, 'auto', '{{flow_name}}_{{flow_index}}', 'default');
insert into template_tasks (rule_id, task_where, task_when, task_order, type, task_delay, task_path, task_condition) values
      (currval('template_rules_id_seq'), 'sender', 'post', 0, 'DELETE', 0, NULL, NULL),
      (currval('template_rules_id_seq'), 'receiver', 'post', 0, 'COPYRENAME', 0, '#OUTPATH#/{{next.destination.HostId}}/#ORIGINALFILENAME#', 'next.destination.Type != types.FTP and next.destination.Type != types.SFTP'),
      (currval('template_rules_id_seq'), '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'),
      (currval('template_rules_id_seq'), 'receiver', 'post', 2, 'COPYRENAME', 0, '#INPATH#/#ORIGINALFILENAME#', 'destination.Type == types.R66 and destination_id in final_destinations'),
      (currval('template_rules_id_seq'), 'receiver', 'post', 3, 'DELETE', 0, NULL, NULL);


insert into template_rules (tpl_id, rule_order, rule_mode, name, rule_when) values
      (2, 0, 'auto', '{{flow_name}}', 'route_index == 1');
insert into template_tasks (rule_id, task_where, task_when, task_order, type, task_delay, task_path, task_condition) values
      (currval('template_rules_id_seq'), 'sender', 'post', 0, 'DELETE', 0, NULL, NULL),
      (currval('template_rules_id_seq'), 'receiver', 'post', 0, 'COPYRENAME', 0, '#OUTPATH#/{{next.destination.HostId}}/#ORIGINALFILENAME#', 'route_index != last_index'),
      (currval('template_rules_id_seq'), 'receiver', 'post', 1, 'DELETE', 0, NULL, 'route_index != last_index'),
      (currval('template_rules_id_seq'), 'receiver', 'post', 2, 'TRANSFER', 0, '-file #OUTPATH#/{{next.destination.HostId}}/#ORIGINALFILENAME# -to {{next.destination.HostIdSsl}} -rule {{next.name}} -copyinfo', 'route_index != last_index'),
      (currval('template_rules_id_seq'), 'receiver', 'post', 3, 'EXECJAVA', 1, 'org.waarp.manager.tasks.UpdateConfExtended #HOSTID#', 'route_index == last_index and destination.Type != types.GWSFTP'),
      (currval('template_rules_id_seq'), 'receiver', 'post', 3, 'EXECJAVA', 1, 'org.waarp.manager.tasks.UpdateConfExtendedSFTP #HOSTID#', 'route_index == last_index and destination.Type == types.GWSFTP'),
      (currval('template_rules_id_seq'), 'receiver', 'post', 4, 'DELETE', 0, NULL, 'route_index == last_index');

insert into template_rules (tpl_id, rule_order, rule_mode, name, rule_when) values
      (2, 1, 'auto', '{{flow_name}}_{{flow_index}}', 'route_index == last_index');
insert into template_tasks (rule_id, task_where, task_when, task_order, type, task_delay, task_path, task_condition) values
      (currval('template_rules_id_seq'), 'sender', 'post', 0, 'DELETE', 0, NULL, NULL),
      (currval('template_rules_id_seq'), 'receiver', 'post', 0, 'EXECJAVA', 1, 'org.waarp.manager.tasks.UpdateConfExtended #HOSTID#', 'destination.Type != types.GWSFTP'),
      (currval('template_rules_id_seq'), 'receiver', 'post', 0, 'EXECJAVA', 1, 'org.waarp.manager.tasks.UpdateConfExtendedSFTP #HOSTID#', 'destination.Type == types.GWSFTP'),
      (currval('template_rules_id_seq'), 'receiver', 'post', 1, 'DELETE',  0, NULL, NULL);

insert into template_rules (tpl_id, rule_order, rule_mode, name, rule_when) values
      (2, 2, 'auto', '{{flow_name}}_{{flow_index}}', 'default');
insert into template_tasks (rule_id, task_where, task_when, task_order, type, task_delay, task_path, task_condition) values
      (currval('template_rules_id_seq'), 'sender', 'post', 0, 'DELETE', 0, NULL, NULL),
      (currval('template_rules_id_seq'), 'receiver', 'post', 0, 'COPYRENAME', 0, '#OUTPATH#/{{next.destination.HostId}}/#ORIGINALFILENAME#', NULL),
      (currval('template_rules_id_seq'), 'receiver', 'post', 1, 'DELETE', 0, NULL, NULL),
      (currval('template_rules_id_seq'), 'receiver', 'post', 2, 'TRANSFER', 0, '-file #OUTPATH#/{{next.destination.HostId}}/#ORIGINALFILENAME# -to {{next.destination.HostIdSsl}} -rule {{next.name}} -copyinfo', NULL);

COMMIT;
