#!/usr/bin/ruby
#

usage = <<US
copy-trace <trace name> <new trace name>
Example: copy-trace ABCD_20090201 ABCD_copy_20090201
Will create ABCD_copy_20090201_Flows from ABCD_20090201_Flows, its aggregate
tables and host names table.
US

trace = ARGV[0] || abort('Expecting trace table name: ' + usage)
newtrace = ARGV[1] || abort("Expecting new trace table name: " + usage);

sql = <<SQL
SELECT COUNT(*) FROM `#{trace}_Flows`;

CREATE TABLE `#{newtrace}_Flows` LIKE `#{trace}_Flows`;
INSERT INTO `#{newtrace}_Flows` SELECT * FROM `#{trace}_Flows`;

CREATE TABLE `#{newtrace}_FlowsAggByDstIp` LIKE `#{trace}_FlowsAggByDstIp`;
INSERT INTO `#{newtrace}_FlowsAggByDstIp` SELECT * FROM `#{trace}_FlowsAggByDstIp`;

CREATE TABLE `#{newtrace}_FlowsAggByDstIpPort` LIKE `#{trace}_FlowsAggByDstIpPort`;
INSERT INTO `#{newtrace}_FlowsAggByDstIpPort` SELECT * FROM `#{trace}_FlowsAggByDstIpPort`;

CREATE TABLE `#{newtrace}_HostNames` LIKE `#{trace}_HostNames`;
INSERT INTO `#{newtrace}_HostNames` SELECT * FROM `#{trace}_HostNames`;

INSERT INTO `Traces` (`Name`, `Description`, `StartDate`, `EndDate`, `DataPath`)
SELECT '#{newtrace}', CONCAT('Copy of ', Name),
StartDate, EndDate, DataPath FROM Traces
WHERE Name = '#{trace}';

ALTER TABLE `Traces` ORDER BY `Name`;
SQL

puts sql
