#!/usr/bin/perl
# Change the password of the wizard.

use vars qw{$sourcedir $dbdir};
$dbdir="./db";

# Source config file.
foreach ('./',$ENV{HOME}.'/.','/usr/local/etc/','/etc/') {
	if (-e "${_}perlmoo.conf") { do "${_}perlmoo.conf" ; last }
}
push @INC,$sourcedir if $sourcedir;

$fn=shift || "$dbdir/db.pl";

require Thing;
require ActiveUser;
require Generics;
require Db;
require Password;

Db::LoadFromFile($fn);

my $wizard=Generics::findgeneric("wizard") || die "No generic wizard?!";
ActiveUser::setactive($wizard);

$wizard->password(Password::encrypt(getpass()));

Db::DumpToFile($fn);
print "Password set, $fn saved.\n";

# Prompt for and get a password, without echoing. Lifted from dupload.
# which is GPL copyrighted by Heiko Schlittermann <heiko@lotte.sax.de>
sub getpass {
	my $okpw=undef;
	do {
		# It's only nice if this stty works, not fatal. Portability.
		system "stty -echo cbreak </dev/tty";
		print "Enter the wizard password to use: ";
		chomp($_ = <STDIN>);
		print "\n";
		system "stty echo -cbreak </dev/tty";

		# Verify the password is ok.
		if (length($_) == 0) {
			print STDERR "Password must be at least one letter long.\n";
		}
		else {
			$okpw=1;
		}
	} until $okpw;

	return $_;
}
