#!/usr/bin/perl -w

############################################################################
#                                                                          #
#  mysql2pg.pl - Helps migration of RT database from MySQL to PostgreSQL   #
#  Copyright (C) 2007 - Gilmar Santos Jr                                   #
#                                                                          #
#  This program is free software; you can redistribute it and/or modify    #
#  it under the terms of the GNU General Public License as published by    #
#  the Free Software Foundation; either version 2 of the License.          #
#                                                                          #
#  This program is distributed in the hope that it will be useful,         #
#  but WITHOUT ANY WARRANTY; without even the implied warranty of          #
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           #
#  GNU General Public License for more details.                            #
#                                                                          #
#  You should have received a copy of the GNU General Public License       #
#  along with this program; if not, write to the Free Software             #
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,              #
#  MA  02110-1301, USA                                                     #
#                                                                          #
#  Gilmar Santos Jr - gilmarsantosjr@safernet.org.br                       #
#                                                                          #
############################################################################

use strict;
use DBI;
use Data::Dumper;

use lib ("/etc/request-tracker3.6", "/usr/share/request-tracker3.6/lib", "/usr/local/share/request-tracker3.6/lib");

use RT::Interface::CLI qw(CleanEnv GetCurrentUser);
use RT::Attachment;

CleanEnv();

RT::LoadConfig();


RT::Init();
$SIG{__DIE__} = "";

############# Adjust usernames and passwords!!!!!!! #############
my $mysql = DBI->connect("DBI:mysql:database=rt3:host=chas", "root", "mv920-h")
	or die $DBI::errstr;

my @tables = (
	'Attachments',
);

$| = 1;
foreach my $table (@tables) {
	print "\rConverting $table...             ";
	my $nr_regs = $mysql->selectall_arrayref(
		          	qq|SELECT count(*) FROM $table;|)->[0][0];
	print "($nr_regs lines)\n";
	my $data    = $mysql->selectall_arrayref(
		          	qq|SELECT * FROM $table ORDER BY ID DESC LIMIT 10000 OFFSET 0|, 
					{ Slice => {} }   ) or die $mysql->errstr;
	my @keys = keys %{$data->[0]};
	foreach my $reg (0 .. ($nr_regs - 1 > 9999 ? 9999 : $nr_regs - 1) ) {
		my $Attachment = RT::Attachment->new($RT::SystemUser);
		$Attachment->Load( $data->[$reg]->{'id'});
		next if ( lc($data->[$reg]->{'ContentType'}) eq "text/plain");
		my ($ContentEncoding, $Body) = $Attachment->_EncodeLOB( $data->[$reg]->{'Content'},  $data->[$reg]->{'ContentType'});
		my ($val, $msg) = $Attachment->SetContent( $Body );
		print "\n *ERROR* $msg \n" unless ($val || $msg =~ /That is already the current value/);
		($val, $msg) = $Attachment->SetContentEncoding( $ContentEncoding );
		print "\n *ERROR* $msg \n" unless ($val || $msg =~ /That is already the current value/);
		print "\r (W) id " . $data->[$reg]->{'id'} . "                    ";
	}
	for (my $i = 10000; $i < $nr_regs; $i += 10000) {
		print "\r (R)", sprintf('%10.2f', $i * 100 / $nr_regs), "\% done ($i rows)";
		$data = $mysql->selectall_arrayref(
		        	qq|SELECT * FROM $table ORDER BY ID DESC LIMIT 10000 OFFSET $i|, 
					{ Slice => {} } ) or die $mysql->errstr;
		print "\r (W)", sprintf('%10.2f', $i * 100 / $nr_regs), "\% done ($i rows)";
		foreach my $reg (0 .. ($nr_regs - $i > 9999 ? 9999 : $nr_regs - $i - 1) ) {
			my $Attachment = RT::Attachment->new($RT::SystemUser);
			$Attachment->Load( $data->[$reg]->{'id'});
			next if ( lc($data->[$reg]->{'ContentType'}) eq "text/plain");
			my ($ContentEncoding, $Body) = $Attachment->_EncodeLOB( $data->[$reg]->{'Content'},  $data->[$reg]->{'ContentType'});
			my ($val, $msg) = $Attachment->SetContent( $Body );
			print "\n *ERROR* $msg \n" unless ($val || $msg =~ /That is already the current value/);
			($val, $msg) = $Attachment->SetContentEncoding( $ContentEncoding );
			print "\n *ERROR* $msg \n" unless ($val || $msg =~ /That is already the current value/);
			print "\r (W) id " . $data->[$reg]->{'id'} . "                    " ;
		}
	}
}

$mysql->disconnect;
