# Stubs that fake LDAP accesses in a local directory.

package LDAPcontext;

my $ldir = "/tmp/LDAP";	# Where to keep the data.
my $hdir;				# Host dir under $ldir.
my $ndir;				# Name dir under $hdir.

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# Instantiate an LDAP object for a specific name and host.  We ignore
# the port at the moment. The password is currently just stored, as a
# record of the last request; we might check its value instead.

sub new {
	my $class = shift;
	my $name = shift;
	my $pswd = shift;
	my $host = shift;
	my $port = shift;
#
	&V::hsend("$V::P/new: Called.") if $V::V>0;
	$self = {};
	bless $self, $class;
	&V::hsend("$V::P/new: Created and blessed self=$self.") if $V::V>0;
#
	$name = 'test' unless $name; &V::hsend("$V::P/new: name=\"$name\"") if $V::V>0;
	$pswd = 'test' unless $pswd; &V::hsend("$V::P/new: pswd=\"$pswd\"") if $V::V>0;
	$host = 'test' unless $host; &V::hsend("$V::P/new: host=\"$host\"") if $V::V>0;
	$port =  389   unless $port; &V::hsend("$V::P/new: port=\"$port\"") if $V::V>0;
	$hdir = "$ldir/$host";       &V::hsend("$V::P/new: hdir=\"$hdir\"") if $V::V>0;
	$ndir = "$hdir/$name";       &V::hsend("$V::P/new: ndir=\"$ndir\"") if $V::V>0;
#
	$self->{'name'} = $name;
	$self->{'pswd'} = $pswd;
	$self->{'host'} = $host;
	$self->{'port'} = $port;
	$self->{'hdir'} = $hdir;
	$self->{'ndir'} = $ndir;
#
	mkdir($ldir,0777) unless -d $ldir;
	mkdir($hdir,0777) unless -d $hdir;
	mkdir($ndir,0777) unless -d $ndir;
	unless (-d $ndir) {
		print STDERR "LDAPcontext: Can't mkdir $ndir ($!)\n";
		return undef;
	}
	if ($pw = &get($self,'pswd',undef)) {
		&V::hsend("$V::P/new: Got pw=\"$pw\"") if $V::V>0;
		if ($pw ne $pswd) {
			&V::hsend("$V::P/new: pw=\"$pw\" not \"$pswd\"") if $V::V>0;
			&V::hsend("<b>Password wrong for \"$name\"</b>.");
			return undef;
		}
		&V::hsend("$V::P/new: Passwords OK.") if $V::V>0;
	} else {
		&V::hsend("$V::P/new: No password.") if $V::V>0;
		&V::hsend("$V::P/new: Before put($self,'pswd',\"$pswd\")") if $V::V>0;
		&put($self,'pswd',$pswd);
		&V::hsend("$V::P/new: After  put($self,'pswd',\"$pswd\")") if $V::V>0;
	}
#
	return $self;
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
sub SettingInterface {
	my $self = shift;	# LDAP context.
	return &new($self->{'name'}, $self->{'pswd'}, $self->{'host'}, $self->{'port'});
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# Store a value for a symbol.

sub put {
	my $self = shift;	# LDAP context.
	my $sym  = shift;	# File name for symbol.
	my $val  = shift;	# String to write.
	my $pth  = "$ndir/$sym";
	my $fil  = 'FIL';
	&V::hsend("$V::P/put: pth=\"$pth\"") if $V::V>1;
	if (open($fil,">$pth")) {
		&V::hsend("$V::P/put: fil=\"$fil\" opened.") if $V::V>1;
		print $fil "$val\n";
		&V::hsend("$V::P/put: val=\"$val\" written.") if $V::V>1;
		close $fil;
		&V::hsend("$V::P/put: fil=\"$fil\" closed.") if $V::V>1;
		return $val;
	} else {
		&V::hsend("$V::P/put: Can't write \"$pth\" ($!)") if $V::V>1;
		return undef;
	}
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# Fetch a value for a symbol.

sub get {
	my $self = shift;	# LDAP context.
	my $sym  = shift;	# File name for symbol.
	my $dfl  = shift;	# Default value if file missing.
	my $val;
	my $pth  = "$ndir/$sym";
	my $fil  = 'FIL';
	if (open($fil,$pth)) {
		$val = <$fil>;
		close $fil;
		chomp $val;
		return $val;
	} else {
		&V::hsend("$V::P/put: Can't read \"$pth\" ($!)") if $V::V>1;
		return $dfl;
	}
}

1;
