#!/usr/bin/perl -w

use strict;
use warnings;

my $file = $ARGV[0];
my $out1 = "egw_cal.sql";
my $out2 = "egw_cal_dates.sql";

print "Migration file $file...\n";

open F, $file;
open G, ">$out1";
open H, ">$out2";

my $line;
my $line2;

while ($line=<F>) {

	$line2=$line;
	$line =~ s/^(\d+)\t([^\t]+)\t(\d+)\t([^\t]*)\t([^\t]+)\t(\d+)\t(\d+)\t(\d+)\t(\d+)\t([^\t]+)\t(\d+)\t([^\t]+)\t([^\t]*)\t([^\t]*)\t(\d+)$/$1\t$2\t$3\t$4\t$7\t$9\t$11\t$12\t$13\t$14\t$15\t$3\t0/;
	$line2 =~ s/^(\d+)\t([^\t]+)\t(\d+)\t([^\t]*)\t([^\t]+)\t(\d+)\t(\d+)\t(\d+)\t(\d+)\t([^\t]+)\t(\d+)\t([^\t]+)\t([^\t]*)\t([^\t]*)\t(\d+)$/$1\t$6\t$8/;
;
	print G $line;
	print H $line2;
}

close F;
close G;
close H;


# Migration de la table phpgw_cal vers egw_cal et egw_cal_dates
#
# Il faut faire un dump PostgreSQL d'eGW 1.0
# puis separer dans un fichier phpgw_cal.sql
# les lignes correspondant à :
# 
# COPY phpgw_cal [...]
# [....]
# [....]
# \.
#
#
# On peut ensuite lancer le script :
# ./perl-egw-cal.pl  phpgw_cal.sql
# qui va générer deux fichiers SQL
# à injecter dans la base PostgreSQL vierge d'eGW 1.2
#
# Remplacer la 1ere ligne avec :
# COPY egw_cal (cal_id, cal_uid, cal_owner, cal_category, cal_modified, cal_priority, cal_public, cal_title, cal_description, cal_location, cal_reference, cal_modifier, cal_non_blocking) FROM stdin;
#
# Et pour le 2e fichier :
#
# COPY egw_cal_dates (cal_id, cal_start, cal_end) FROM stdin;
#
#
# Lors de l'insertion d'egw_cal, en cas d'erreur du type :
# ERREUR:  Séquence d'octets invalide pour le codage "UNICODE" : 0xe96d61
# Ajouter :
# SET client_encoding to 'LATIN1';
#
#
# Il faut aussi reinitialiser la sequence :
# => select max(cal_id) from egw_cal;
#
# incrementer le resultat X puis :
# egroupware3=> ALTER SEQUENCE egw_cal_cal_id_seq RESTART WITH X;
#
# Remarques sur les changements dans la base :
#
# phpgw_cal => egw_cal :
# ----------------------
#
# cal_id => cal_id
# uid => cal_uid
# owner => cal_owner
# category => cal_category
# groups => XXXX
# datetime => XXXX
# mdatetime => cal_modified
# edatetime => XXXX
# priority => cal_priority
# cal_type => XXX
# is_public => cal_public
# title => cal_title
# description => cal_description
# location => cal_location
# reference => cal_reference
# XXXX => cal_modifier =uid
# XXXX => cal_non_blocking =0
#
# phpgw_cal => egw_cal_dates :
# ----------------------------
#
# cal_id => cal_id
# uid => XXXX
# owner => XXXX
# category => XXXX
# groups => XXXX
# datetime => cal_start
# mdatetime => XXXX
# edatetime => cal_end
# ... => XXX
#
#
#
