#!/usr/bin/perl

#   $Header: /cvsroot/systeminstaller/systeminstaller/bin/simigratedb,v 1.4 2003/04/11 20:44:29 mchasal Exp $

#   Copyright (c) 2001 International Business Machines

#   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, or
#   (at your option) any later version.

#   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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

use strict;
use vars qw($config $VERSION);
$VERSION = sprintf("%d.%02d", q$Revision: 1.4 $ =~ /(\d+)\.(\d+)/);
use lib "/usr/lib/systeminstaller";
use SIS::Client;
use SIS::Adapter;
use SIS::Image;
use SIS::DB;
use SystemInstaller::Env;
use SystemInstaller::Log qw(start_verbose stop_verbose verbose logger_file);
use Carp;
use AppConfig qw(:argcount);
use Data::Dumper;
use File::Path;

#Set the path
$ENV{PATH}=$config->binpath .":" . $ENV{PATH};


$config->define(
        Help=>{ ARGCOUNT=> ARGCOUNT_NONE},
        version=>{ARGCOUNT=> ARGCOUNT_NONE},
);

unless ($config->getopt()){
	&usage;
	exit 1;
}

if ($config->version){
        &print_version($0,$VERSION);
        exit 0;
}

if ($config->Help){
	&usage;
	exit 0;
}

unless (-e "/var/lib/clamdr") {
        exit 0;
}

my %CLAMDR;
unless (%CLAMDR=read_old_db() ) {
        print "Couldn't read old SIS database in /var/lib/clamdr.\n";
        print "Unable to migrate to new database, after correcting the\n";
        print "problem, run simigratedb to migrate the data.\n";
        exit 0; # We don't want the %post to fail
}
if (%CLAMDR) {
        make_images(%CLAMDR);
        make_clients(%CLAMDR);
        make_adapters(%CLAMDR);
}

rmtree("/var/lib/clamdr");


exit 0;

sub make_images {
        my %CLAMDR=@_;
        my @OBJS;
        foreach my $image (keys%{$CLAMDR{IMAGES}}) {
                my %O=%{$CLAMDR{IMAGES}{$image}};
                if (exists_image($O{NAME})) {
                        print "Image $O{NAME} already exists.\n";
                        next;
                } else {
                        my $obj = new SIS::Image($O{NAME});
                        $obj->location($O{LOCATION});
                        $obj->arch($O{ARCH});
                        push @OBJS,$obj;
                }

        }
        if (@OBJS) {
                set_image(@OBJS);
        }
} #make_images

sub make_clients {
        my %CLAMDR=@_;
        my @OBJS;
        foreach my $client (keys%{$CLAMDR{CLIENTS}}) {
                my %O=%{$CLAMDR{CLIENTS}{$client}};
                if (exists_client($O{NAME})) {
                        print "Client $O{NAME} already exists.\n";
                        next;
                } else {
                        my $obj = new SIS::Client($O{NAME});
                        $obj->route($O{ROUTE});
                        $obj->hostname($O{HOSTNAME});
                        $obj->domainname($O{DOMAIN});
                        $obj->imagename($O{IMAGE});
                        $obj->proccount($O{NUMPROC});
                        push @OBJS,$obj;
                }

        }
        if (@OBJS) {
                set_client(@OBJS);
        }
} #make_clients

sub make_adapters {
        my %CLAMDR=@_;
        my @OBJS;
        foreach my $client (keys%{$CLAMDR{ADAPTERS}}) {
                foreach my $dev (keys%{$CLAMDR{ADAPTERS}{$client}}) {
                        my %O=%{$CLAMDR{ADAPTERS}{$client}{$dev}};
                        if (exists_adapter($O{DEVICE},$O{CLIENT})) {
                                print "Adapter $O{DEVICE} for client $O{CLIENT} already exists.\n";
                                next;
                        } else {
                                my $obj = new SIS::Adapter($O{DEVICE});
                                $obj->client($O{CLIENT});
                                $obj->mac($O{MAC});
                                $obj->ip($O{IP});
                                $obj->netmask($O{NETMASK});
                                push @OBJS,$obj;
                        }
                }

        }
        if (@OBJS) {
                set_adapter(@OBJS);
        }
} #make_adapters


sub read_old_db {
        my %CLAMDR;
        my $file="/var/lib/clamdr/Image";
        if (-e $file) {
                open(FILE,"<$file") or return;
                while (<FILE>) {
                        chomp;
                        my ($name,$server,$loc,$arch)=split(/:/,$_);
                        $CLAMDR{IMAGES}{$name}{NAME}=$name;
                        $CLAMDR{IMAGES}{$name}{LOCATION}=$loc;
                        $CLAMDR{IMAGES}{$name}{ARCH}=$arch;
                        $CLAMDR{IMAGES}{$name}{SERVER}=$server;
                }
                close(FILE);
         }
        my $file="/var/lib/clamdr/Client";
        if (-e $file) {
                open(FILE,"<$file") or return;
                while (<FILE>) {
                        chomp;
                        my ($name,$hostname,$domain,$route,$mode,$bootdev,$image,$numproc)=split(/:/,$_);
                        $CLAMDR{CLIENTS}{$name}{NAME}=$name;
                        $CLAMDR{CLIENTS}{$name}{HOSTNAME}=$hostname;
                        $CLAMDR{CLIENTS}{$name}{DOMAIN}=$domain;
                        $CLAMDR{CLIENTS}{$name}{ROUTE}=$route;
                        $CLAMDR{CLIENTS}{$name}{MODE}=$mode;
                        $CLAMDR{CLIENTS}{$name}{BOOTDEV}=$bootdev;
                        $CLAMDR{CLIENTS}{$name}{IMAGE}=$image;
                        $CLAMDR{CLIENTS}{$name}{NUMPROC}=$numproc;
                }
                close(FILE);
         }
        my $file="/var/lib/clamdr/Adapter";
        if (-e $file) {
                open(FILE,"<$file") or return;
                while (<FILE>) {
                        chomp;
                        my ($client,$devname,$MAC,$ip,$netmask,$method,$rest);
                        if (/\"/){
                                ($client,$devname,$rest)=split(/:/,$_,3);
                                $rest=~s/^"//;
                                ($MAC,$rest)=split(/"/,$rest,2);
                                $rest=~s/^://;
                                ($ip,$netmask,$method)=split(/:/,$rest);
                        } else {
                                ($client,$devname,$MAC,$ip,$netmask,$method)=split(/:/,$_);
                        }
                        $CLAMDR{ADAPTERS}{$client}{$devname}{DEVICE}=$devname;
                        $CLAMDR{ADAPTERS}{$client}{$devname}{CLIENT}=$client;
                        $CLAMDR{ADAPTERS}{$client}{$devname}{MAC}=$MAC;
                        $CLAMDR{ADAPTERS}{$client}{$devname}{IP}=$ip;
                        $CLAMDR{ADAPTERS}{$client}{$devname}{NETMASK}=$netmask;
                        $CLAMDR{ADAPTERS}{$client}{$devname}{METHOD}=$method;
                }
                close(FILE);
         }
         return %CLAMDR;
} #read_old_db


sub usage {
    my $progname = $0;
    if ($progname =~ m/(.+\/)(\w+)/) {
	$progname = $2;
    }
    print <<USAGE;
usage: $progname <options>
  options
        --Help     Print help message.
        --version  Print file version info.
USAGE
}

__END__

=head1 NAME

simigratedb - command shell migrate the CLAMDR database to the MLDBM format

=head1 SYNOPSIS

  simigratedb 

=head1 DESCRIPTION

The simigratedb command is used to move the contents of the old SIS database
(CLAMDR) to the new one.

=head2 Syntax

simigratedb [ I<options> ]

=head2 Options

Recognized options include:

=over 4

=item --version

Prints version information

=item --help

Prints usage information

=head1 AUTHOR

Michael Chase-Salerno, mchasal@users.sf.net

=head1 SEE ALSO

perl(1), L<SIS::DB>

=cut
