#
# Functions to access the NUUG member register dump
#

# The SAGE member list is updated on the wiki until the register dump
# include the information needed.
my $sagelistfile = "http://wiki.nuug.no/medlemsregister/sagemedlemmer?action=raw";
my $ikkeinfolistfile = "http://wiki.nuug.no/medlemsregister/ikkeinfo?action=raw";

sub firmmembers_included {
    return 3;
}

sub current_membership_period {
    return "2026-12-31";
}

sub load_memberslist_new {
    my ($filename, $processfunc, $ref) = @_;

    open(FILE, "<", "$filename") || die "Unable to read from $filename";
    my $csv = Text::CSV_XS->new({'binary' => 1, sep_char => "\t"});
    my $line = <FILE>;
    my $status = $csv->parse($line);
    die unless $status;

    my @field_names = $csv->fields();
    die "Got $#field_names fields, expected 21" unless $#field_names == 21;

    while (<FILE>) {
        chomp;
        s/\r//;
        if ($status = $csv->parse($_) ) {
            my @field_values = $csv->fields();
            next unless $field_values[19]; # ErArbeidsgiver

            my %values;
            my $i = 0;
            for my $field_name (@field_names) {
                $values{$field_name} = $field_values[$i] eq '' ? undef : $field_values[$i];
                $i++;
            }
            if (add_member($dbh, %values)) {
                $added++;
            } else {
                $dropped++;
                last;
            }
        } else {
            print "Unable to parse $_\n";
            last;
        }
    }
    close FILE;
}

sub load_memberslist {
    my ($filename, $processfunc, $ref) = @_;
    open LIST, "<", "$filename" or die "Unable to read from $filename";
    my $line = <LIST>;
    $line =~ s/\s+$//g;
    my @fields = split(/\t/, $line);

    # Sanity check
    if ($fields[15] eq "PostCode") { # New format including SAGE
    } elsif ($fields[14] eq "PostCode") { # Traditional format from DND
    } elsif ($fields[13] eq "PostCode") { # Old format
    } else {
        print STDERR "Input file '$filename' have incorrect format.\n";
        exit 1;
    }

#    my %sagemember = load_wiki_idlist($sagelistfile);
#    my %ikkeinfo = load_wiki_idlist($ikkeinfolistfile);

#    my %paid = load_csv_idlist("usenixdump-2007-jan.csv");

    while (<LIST>) {
        s/\s+$//g;
        my @values = split(/\t/);
        my %memberinfo;
        my $fieldcount = 0;
        for my $field (@fields) {
            $memberinfo{$field} = $values[$fieldcount];
            $fieldcount++;
        }
        my $customerno = $memberinfo{'CustomerNo'};

        # Hack to work around missing/incorrect info in the export
        if (0) {
        if (exists $sagemember{$customerno}) {
            $memberinfo{'SAGE'} = 1;
        } else {
            $memberinfo{'SAGE'} = 0;
        }
        if (exists $ikkeinfo{$customerno}) {
            $memberinfo{'Informasjon'} = 0;
        } else {
            $memberinfo{'Informasjon'} = 1;
        }
        if (exists $paid{$customerno}) {
            $memberinfo{'ZBetaltTil'} = "2007-12-31";
        } else {
            $memberinfo{'ZBetaltTil'} = "";
        }
        }
        $processfunc->(\%memberinfo, $ref);
    }
    close(LIST);
}

sub load_wiki_idlist {
    my $filename = shift;
    my %idlist;
    my $orig_rs = $/;
    if ($filename =~ m/^http:/) {
        $/ = "\r\n"; # The wiki uses DOS-type line ending
        # Quick and dirty HTTP extraction.  Should probably use perl
        # libraries instead.
        open(SAGELIST, "GET '$filename' | grep '^[0-9]' |")
            || die "Unable to read URL $filename";
    } else {
        open(SAGELIST, "<$filename") || die "Unable to read $filename";
    }
    while (<SAGELIST>) {
        chomp;
        $idlist{$_} = 1;
    }
    close SAGELIST;
    $/ = $orig_rs;
    return %idlist;
}

sub load_csv_idlist {
    my $filename = shift;
    my %idlist;
#    print "Loading $filename\n";
    open(DATALIST, "<", $filename) || die "Unable to read $filename";
    my $line = <DATALIST>;  # Remove header
#    print "A: @f\n";
    while (<DATALIST>) {
        chomp;
        my @f = split(/\t/);
        $idlist{$f[0]} = 1;
#        print "A: @f\n";
    }
    close DATALIST;
#    print "Done loading $filename\n";
    return %idlist;
}

1;
