#!/usr/bin/perl
#
# CFINGERD Configuration Script
# Version 3.0 by Ken Hollis <khollis@bitgate.com>
#
# This program is GPLed.  Please read LICENSE for more information.
# Copyright (C) 1996, Bitgate Software.

require 'perl/question.pl';
require 'perl/filemagic.pl';
require 'perl/gethost.pl';
require 'perl/generic.pl';

$VERSION = "1.3.1";
$| = 1;

sub show_header {
	system("clear");
	&disp_file("config/header");
}

sub show_footer {
	&disp_file("config/footer");
}

sub make_config {
	print "Creating src/config.h ... ";

	local($dateandtime) = `date`;
	chop($dateandtime);

	$define_bsd = "\n\37ifndef BSD\n\37define BSD 1\n\37endif\n";
	($uid,$gid) = split(',', $nobody);

	open(CF, "src/config.h.in");
	open(CFO, ">src/config.h");
	while(<CF>) {
		chop;

		s/\$0//;
		s/\$1/#define\tNOBODY_UID\t\t$uid/;
		s/\$2/#define\tNOBODY_GID\t\t$gid/;
		s/\$3/#define\tCOMPILE_DT\t\t\"$dateandtime\"/;
		s/\$4/#define\tWTMPFILE\t\t"$lastlog"/;

		if (($lastlog ne "/var/adm/lastlog") &&
		    ($lastlog ne "/var/log/lastlog")) {
			s/\$5/#undef\tUSE_LASTLOG/;
		} else {
			s/\$5/#define\tUSE_LASTLOG\t\t1/;
		}

		if ($shadow eq "Y") {
			s/\$6/#define\tHAS_SHADOW\t\t1/;
		} else {
			s/\$6/#undef\tHAS_SHADOW/;
		}

		print CFO "$_\n";
	}

	print CFO $define_bsd if $make eq "gmake";
	close(CFO);
	close(CF);

	print "created.\n";
}

sub make_userlist {
	print "Creating userlist/config.h ... ";

	open(CF, "userlist/config.h.in");
	open(CFO, ">userlist/config.h");

	while(<CF>) {
		chop;
		s/\$0//;

		print CFO "$_\n";
	}

	print CFO $define_bsd if $make eq "gmake";
	close(CFO);
	close(CF);

	print "created.\n";
}

sub make_makefile {
	print "Creating Makefile ... ";

	open(CF, "Makefile.in");
	open(CFO, ">Makefile");

	while(<CF>) {
		chop;
		s/\$0/$make/;

		print CFO "$_\n";
	}

	close(CF);
	close(CFO);

	print "created.\n";
}

sub configure {
	unlink("config.h");
	unlink("userlist/config.h");

	$go_on = &check_exist("1", "./Makefile", "totally clean installation", "config/makewarn");
	if ($go_on eq "Y") {
	} else {
		&disp_file("config/cancelled");
		exit;
	}

	$domain = &get_fqdn("FQDN?", "config/fqdn");

	system("clear");
	$os_type = &get_os;
	$lastlog = &get_lastlog;
	$nobody = &get_nobody;
	$shadow = &has_shadow;

	if ($os_type eq "LINUX") {
		$make = "make";
	} else {
		$make = "gmake";
	}

	print "\n";

	&make_config;
	&make_userlist;
	&make_makefile;
}

&show_header;
&configure;
&show_footer;
