#!/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, noacl => 1);
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 = $passwd[27]; 
    warn $login . ' ' . scalar(@passwd);
    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;
        if ($passwd[1] ne 'Compte-de-service') {
            $changes{givenName} = $passwd[1];
            $changes{givenName} =~ s/_/ /g;
        }
        if ($passwd[10] =~ /^NICK(.*)$/) {
            if ($1 eq 'zzz') {
            $changes{nickname} = undef;
            } else { $changes{nickname} = $1 }
        }

        if ($passwd[1] ne 'Compte-de-service') {
            $changes{mail} = $passwd[13];
            $changes{mail} =~ s/cetp.ipsl.fr/latmos.ipsl.fr/;
        }
    }

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


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

                for ($eposite) {
                    /^SAV$/i and do {
                        $param{site} = 'Verrières';
                        $passwd[18] =~ /aerov/ and $param{isMainAddress} = 1;
                        last;
                    };
                    /^Vel$/i and do {
                        $param{site} = 'Vélizy';
                        $passwd[18] =~ /cetp/ and $param{isMainAddress} = 1;
                        last;
                    };
                    /^Stm$/i and do {
                        $param{site} = 'St Maur';
                        last;
                    };
                    /^SAJ$/i and do {
                        $param{site} = 'Jussieu';
                        $passwd[18] =~ /aero$/ and $param{isMainAddress} = 1;
                        last;
                    };
                    /^GY$/i and do {
                        $param{site} = 'Guyancourt';
                        # is not main address at time
                        last;
                    };
                    warn "unmanaged $eposite";
                }
                $labase->create_object(
                    'address',
                    $obj->id . '-' . $eposite,
                    %param
                );
            }
        }
        $passwd[16] =~ /^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[22] =~ /^GRADE(.*)/ and do {
            $changes{grade} = $1;
        };
        } # Not compte-de-service
        else {
            $changes{grade} = undef,
            $changes{expire} = undef;
            $changes{description} = "$passwd[1] $passwd[0]";
        }
    }

    {
        my @group = @{ $obj->get_c_field('memberOf') || []};
        foreach (split('DOM', $passwd[19])) {
            $_ or next;
            $changes{department} ||= $_;
            push @group, $_;
            next;
        }
        foreach (split('CEL', $passwd[20])) {
            $_ or next;
            push @group, $_;
        }
        $changes{'memberOf'} = \@group;
    }

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