#!/usr/bin/perl -w

use strict;

use diagnostics;

use CDB_File 'create';

# Version 0.1 got its information from subdirectories of $home (default /home),
# if you prefer it, you can get the old version.
my $VERSION="0.2";

# which file should be put into the database?
# support for pgp-keys will be added soon.
my $file = ".plan";

# in which file the database is stored?
#my $dbfile = "/tmp/finger.cdb";
#my $dbtmp = "/tmp/db.tmp";

# only open .plan if uid is greater than or equal to $minuid
my $minuid = 500;

# which passwdfile dffingerd should use?
my $passwd = "/etc/passwd";

# if you don't want to use /etc/passw, you can also rearange the position of the
# single entries.
my $username = 0;
my $uid = 2;
my $home = 5;

sub check_uid
{
	return $>;
}

sub get_user
{
	my %user;
	open (PASSWD,"<$passwd") || die "cannot open file $passwd: $!";
	while (<PASSWD>)
	{
		my @ary = split(/:/);
		$ary[$home] .= "/" if (!/\/$/);  # couldn't create a regexp.
		$user{$ary[$username]} = $ary[$home] if ($ary[$uid] >= $minuid)
	}
	return \%user;
}

sub read_file()
{
	my $home = shift;
	my $result = '';
	if ( -e $home . $file && -f $home . $file)
	{
		open (FILE,"<$home/$file") || die "Could not open $home$file: $!\n";
		while (<FILE>)
		{
			$result .= $_
		}
		close (FILE)
	}
	return $result
}


sub create_database
{
	my $result = CDB_File::create (%dbinput,$dbfile,$dbtmp);
	if (!$result)
	{
		print "Error while creating database: $!\n"
	}
	return $result
	
}

################################## Main ########################################

die "Do not run as root" if (!&check_uid());

my %dbinput;
my $dbinput = &get_user;
foreach my $user (keys (%$dbinput))
{
	$dbinput{$user} = &read_file($dbinput->{$user})
}

my $exitcode = &create_database();
exit;

