#!/usr/bin/perl
# $Id: convertXml,v 1.7 2002/03/12 19:04:18 jeffb Exp $

use lib "/usr/cmu/perl";

use strict;
use TreeXml;
use Archive;
use Data::Dumper;
require MIME::Base64;
import MIME::Base64 qw(decode_base64);


my ($dir, $file);
if(@ARGV && -d $ARGV[0]) { $dir = $ARGV[0]; } 
else {
    print "Cannot find directory: ", $ARGV[0], "\n";
    print "usage: $0 [DIR]\n";
	exit 1;
}

if(-f $dir.'/cmu.xml') { $file = $dir.'/cmu.xml'; } 
else {
    print "Cannot find cmu.xml in directory: ", $dir, "\n";
	exit 1;
}

my $newTree = convertXml($dir, $file);

my $migrate = {};
qx/cp $file $file.bak/;
$newTree->{cmuVersion} = '3.31';
TreeXml::addNode('migrate', $newTree, $migrate);
TreeXml::writeXml($migrate, $file);

exit 0;


sub convertXml
# Arguments: old xml tree data structure
# Retrun: the newly format data structure
# side effect:  kicking punk ass
{
	my $dir = shift || return;
	my $file = shift || return;

	chdir $dir || die "Could not change dir to: $dir\n";
	
	# initialize the data structure
	my $tree = {};
	$tree = TreeXml::readXml($file, 0);

	my $varch = Archive->new(type => 'groups', destDir => $dir);
	my $uarch = Archive->new(type => 'users', destDir => $dir);

	my (@keys, $pub, $priv, $xml, $arch, $newXml, $tarName);
	@keys = keys %{ $tree->{vsite} };
	foreach my $site (@keys) {
		next if(defined $tree->{vsite}->{$site}->{archive});
		$xml = $dir.'/groups-'.$tree->{vsite}->{$site}->{fqdn}.'.xml';
		warn "reading: ", $xml, "\n";
		$arch = $varch->readXml($xml); 
		$arch = decodeFileNamesOld($arch);
		foreach my $type (qw(public private)) {
			next if(!defined $arch->{$type}); 
			$newXml = 'groups-'.$tree->{vsite}->{$site}->{fqdn}.'-'.$type.'.xml';
			$varch->setXmlName($newXml);
			$tarName = 'groups-'.$tree->{vsite}->{$site}->{fqdn}.'-'.$type.'.tar.gz';
			$arch->{$type}->{tarFile} = $tarName; 
			$varch->writeXml($arch->{$type});
			if(-f $newXml) { 
				push @{ $tree->{vsite}->{$site}->{archives}->{$type} }, $newXml;
			}
		}
		
	}
    @keys = keys %{ $tree->{user} };
	foreach my $user (@keys) {
		next if(defined $tree->{user}->{$user}->{archive});
		$xml = $dir.'/users-'.$tree->{user}->{$user}->{name}.'.xml';
		$arch = $varch->readXml($xml); 
		$arch = decodeFileNamesOld($arch);
		foreach my $type (qw(public private)) {
			next if(!defined $arch->{$type}); 
			$newXml = 'users-'.$tree->{user}->{$user}->{name}.'-'.$type.'.xml';
			$varch->setXmlName($newXml);
			$tarName = 'users-'.$tree->{user}->{$user}->{name}.'-'.$type.'.tar.gz';
			$arch->{$type}->{tarFile} = $tarName; 
			if(-f $tarName) { $varch->writeXml($arch->{$type}); }
			else { warn "Empty archive: $xml: $tarName\n" }
			if(-f $newXml) { 
				push @{ $tree->{user}->{$user}->{archives}->{$type} }, $newXml;
			}
		}
		
	}

	return($tree);
}

sub decodeFileNamesOld
# argument: file data structure
# returns: file data structure
# side effect: decode_base64 each of the file names
{
    my $hash = shift;

	foreach my $type (qw(public private)) {
		next if(!defined $hash->{$type}->{file});
		if(ref $hash->{$type}->{file} eq 'HASH') {
			my $tmp = $hash->{$type}->{file};
			delete $hash->{$type}->{file}; 
			push @{ $hash->{$type}->{file} }, $tmp;
		}
		foreach my $f (@{ $hash->{$type}->{file} }) {
			next if(!defined $f->{name});
			$f->{name} = decode_base64($f->{name});
		}
	}
    return $hash;
}

