#!/usr/bin/perl

use strict;
use warnings;
use LATMOS::Accounts;
use Getopt::Long;
use Pod::Usage;

GetOptions(
    'c|config=s' => \my $config,
    'b|base=s'   => \my $base,
) or pod2usage();

my $otype ||= 'user';

my $LA = LATMOS::Accounts->new($config);
my $labase = $base ? $LA->base($base) : $LA->default_base;
$labase->{wexported} = 1;
$labase && $labase->load or die "Cannot load base";

while (my $line = <STDIN>) {

    chomp($line);

    my @passwd = split("\t", $line);
    warn (join(' - ', map { $_ => $passwd[$_] } 0 ..$#passwd));
    my @epo;

    my $login = {
        clerbaux => 'ccl',
        cgranier => 'clg',
        vernier => 'jeanpaul82',
    }->{$passwd[22]} || $passwd[22];
    my $obj = $labase->get_object($otype, $login) or do {
        warn "Cannot get $login user";
        warn $line . "\n";
        next;
    };
    my %changes;

    { # le nom/ prénom
        $changes{sn} = $passwd[0];
        $changes{sn} =~ s/_/ /g;
        $changes{givenName} = $passwd[1];
        $changes{givenName} =~ s/_/ /g;
        if ($passwd[10] =~ /^NICK(.*)$/) {
            if ($1 eq 'zzz') {
            $changes{initials} = undef;
            } else { $changes{initials} = $1 }
        }
        $changes{mail} = $passwd[9];
        $changes{mail} =~ s/cetp.ipsl.fr/latmos.ipsl.fr/;
        if ($passwd[13] !~ /1900\d\d\d\d/) {
            $changes{birthDate} = $passwd[13];
            $changes{birthDate} =~ s/^N//;
            $changes{birthDate} .= '120000Z';
        }
    }

    { # address, a bit more complex
        # trash old
        foreach ($obj->get_attributes('otheraddress')) {
            $labase->delete_object('address', $_);
        }

        foreach my $col (3, 5) {
            if($passwd[$col] ne 'AJETER') {
                my %param = (user => $obj->id,);
                $passwd[$col] =~ /^(SAV|SAJ|vel|StM)-(.*)/i;
                my $eposite = $1;
                $param{physicalDeliveryOfficeName} = $2;
                $param{telephoneNumber} = '+33 1 ' . $passwd[$col + 3];

                for ($eposite) {
                    /^SAV$/i and do {
                        $param{site} = 'Verrières';
                        $passwd[14] =~ /aerov/ and $param{isMainAddress} = 1;
                        last;
                    };
                    /^Vel$/i and do {
                        $param{site} = 'Vélizy';
                        $passwd[14] =~ /cetp/ and $param{isMainAddress} = 1;
                        last;
                    };
                    /^Stm$/i and do {
                        $param{site} = 'St Maur';
                        last;
                    };
                    /^SAJ$/ and do {
                        $param{site} = 'Jussieu';
                        $passwd[14] =~ /aero$/ and $param{isMainAddress} = 1;
                        last;
                    };
                    warn "unmanaged $eposite";
                }
                $labase->create_object(
                    'address',
                    $obj->id . '-' . $eposite,
                    %param
                );
            }
        }
        $passwd[12] =~ /^T(\d\d\d\d)(\d\d)(\d\d)$/ and do {
            if($1 < 2100) {
                $changes{expire} = "$3/$2/$1 23:59:59";
            } else {
                $changes{expire} = undef;
            }
        };
        $passwd[17] =~ /^GRADE(.*)/ and do {
            $changes{grade} = $1;
        };
    }

    {
        my @group = @{ $obj->get_c_field('memberOf') || []};
        foreach ($passwd[15] =~ /^DOM(.*)/g) {
            push @group, $1;
            next;
        }
        $changes{'memberOf'} = \@group;
    }

    keys %changes or next;
    $obj->set_c_fields(%changes);
}
$labase->commit;
