# Routines for actually displaying (parts of) database in CGI output.
#
# Copyright (C) 1999 Ian Jackson <ijackson@chiark.greenend.org.uk>
#
# This 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,
# or (at your option) any later version.
#
# This 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 file; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

sub list_database ($) {
    my ($instyle) = @_;
    my ($t, $v, $k, $x);
print DEBUG "ldb 0 $instyle\n";
    $instyle =~ m/all|area|overlap/ or die "$instyle ?";
    $listing= $&;
    &{"dblist_prep_$listing"}();
    @kl= ();
print DEBUG "ldb 1 $v @kl\n";
    foreach $k (keys %db) {
print DEBUG "ldb q $k\n";
        $ent= $db{$k};
	next unless &{"dblist_cond_$listing"};
	push @kl,$k;
    }
print DEBUG "ldb 2 @kl\n";
    @kl= sort {
	$x= $db{$a}->{'network'} cmp $db{$b}->{'network'}; return $x if $x;
	$x= $db{$a}->{'prefix'} <=> $db{$b}->{'prefix'}; return $x if $x;
	return -1 if $a eq 'picked' && $b ne 'picked';
	return +1 if $b eq 'picked' && $a ne 'picked';;
	$x= $db{$a}->{'name'} cmp $db{$b}->{'name'}; return $x if $x;
	$x= $db{$a}->{'contact'} cmp $db{$b}->{'contact'}; return $x if $x;
	return $a cmp $b;
    } @kl;
print DEBUG "ldb 3 @kl\n";
    $listingnonefound= @kl ? 0 : 1;
print DEBUG "ldb end $listingnonefound\n";
    $v= "listing$listing"; $$v= 1;
    $list= 1;
}

sub dblist_prep_all { }
sub dblist_cond_all { 1; }

sub dblist_prep_area {
    my ($network,$prefix);
    $network= $area_networks[$list_areai];
    $prefix= $area_prefixes[$list_areai];
    $listarea= display_net($network,$prefix);
    $dbl_mask= get_mask($prefix);
    $dbl_value= hex($network);
}
sub dblist_cond_area {
    my ($v, $r);
    $v= hex($ent->{'network'});
    $r= 1 if ($v & $dbl_mask) == $dbl_value;
print DEBUG "dblist_cond_area $k $v $r\n";
    return $r;
}

sub dblist_prep_overlap {
    my ($network,$prefix);
    ($network,$prefix,$dbl_mask,$dbl_value) = parse_netrange($in{'with'});
    $listoverlap= display_net($network,$prefix);
}
sub dblist_cond_overlap {
    my ($v, $m);
    $v= hex($ent->{'network'});
    $m= get_mask($ent->{'prefix'});
    $m &= $dbl_mask;
    return ($v & $m) == ($dbl_value & $m);
}

sub foreach_start_db { $db_i=0; }
sub foreach_cond_db { return $db_i < @kl; }
sub foreach_incr_db { $db_i++; }
sub foreach_setvars_db {
    my ($k, $ent);
    $k= $kl[$db_i];
    $ent= $db{$k};
    $db_picked= $k eq 'picked';
    if ($db_picked) {
	undef $db_id;
    } else {
	$db_id= $k;
    }
    $db_net= display_net($ent->{'network'}, $ent->{'prefix'});
    $db_name= html_sani($ent->{'name'});
    $db_contact= html_sani($ent->{'contact'});
    $db_hiddenemail= $ent->{'hiddenemail'};
    $db_email= $db_hiddenemail ? "" : html_sani($ent->{'email'});
    $db_confirmed= !!$ent->{'changed'};
}

1;
