use utf8;
use warnings; no warnings "redefine";
use strict;
use Loader qw(line_in select_descr log_update valid_handle 
	domain_defaulthandles line_add);
use Dbase::Help qw(quote DoFn Do);
use Fehler qw(problem);

sub set_handle_handle($$;$) {
	my($pers,$nic,$kn) = @_; 

	return unless valid_handle($pers,$nic,1,$kn);

	my $dhand = domain_defaulthandles($pers,$nic);
	line_add($dhand) if defined $dhand;

	my($ohand,$stat) = DoFn("select handle,status from nic where person=$pers and nic=$nic");
	line_add($ohand) if defined $ohand and defined $dhand and $ohand ne $dhand;

	my $hand = line_in "NIC-Handle : ";
	return undef if not defined $hand or $hand eq "";

	if($hand eq "-") {
		$hand = "''";
	} else {
		$hand = "'${\quote $hand}'";

		my $op = DoFn("select person from nic where handle = $hand and person != $pers");
		return problem "Den Handle '$hand' hat bereits #$op:".mpersinfo($op).".\n"
			if $op;

		my $re = DoFn("select handle_re from nicdata where nic=$nic");

		return problem "Handle $hand passt nicht zum NIC $nic"
			if $re and ($hand !~ /\A$re\z/);
	}

	if($stat < 4) {
		$stat = 0;
	} else {
		$stat = 10;
	}

	log_update("nic","person",$pers,"nic",$nic,undef,"handle",undef,$ohand);

	Do("update nic set handle=$hand, status=$stat where person=$pers and nic=$nic");
	1;
}
1;
