#!/usr/bin/perl
#
# Installation script for CFINGERD
# version 2.0.0 by Ken Hollis

$cfgpath = "/etc/cfingerd.conf";

if (open (C, "src/config.h")) {
    while (<C>) {
	/^#define\s+CFINGERD_CONF\s+(.*)$/ && ($cfgpath = $1);
    }
    close (C);
} else {
    print "Can't read src/config.h, using defaults.\n";
}

system("clear");
print "Welcome to Install for cfingerd 1.4.x.\n\n";

if ($ARGV[0] eq "-docs") {
    print "Force documentation install detected.\n\n";
}

if ($ARGV[0] ne "-docs") {
    if (-e "Makefile") {
	if (-e $cfgpath) {
	    print <<"EOT";
You already have a version of cfingerd.conf in the proper directory, which 
is where the normal configuration file is stored.  If you like, install 
will create a new configuration file for you, and save the old 
cfingerd.conf file as \"cfingerd.conf-old\" in case you need it for 
reference.

If you've previously had version 1.0.0 or below of cfingerd, this new 
cfingerd.conf file will be incompatible.
EOT
	    print "\nInstall new cfingerd.conf [Y]: ";
	    $doinst = <STDIN>;
	    chop($doinst);
	    $doinst =~ tr/a-z/A-Z/;
	    if (($doinst eq "Y") ||
		($doinst eq "YE") ||
		($doinst eq "YES") ||
		($doinst eq "")) {
		system("mv $cfgpath $cfgpath-old");
		system("cp -f cfingerd.conf $cfgpath");
		print "\nOkay, it's been copied, and the old one was copied to \"cfingerd.conf.old\".\n";
	    } else {
		print "Skipped.\n";
	    }
	} else {
	    print "Installing cfingerd.conf.\n";
	    system("cp -f cfingerd.conf $cfgpath");
	}
    } else {
	print "You need to compile cfingerd first!  Type \"Configure\" if you've not\n";
	print "already done so, then switch to root, and type \"make install\".\n";
	print "\nBesides, this script is automatically called from within the Makefile,\n";
	print "so there's no need for you to type it in manually.\n\n";
	exit;
    }

    system("rm -f /usr/man/man1/cfingerd.1");
    system("rm -f /usr/man/man1/cfingerd.conf.1");

    print "\nRemoved cfingerd and cfingerd.conf man pages from section 1 in case\n";
    print "they existed on your system.  (This may have been left over from an older\n";
    print "version.)  Old versions of cfingerd stored the manual pages in section 1.\n\n";
}

if ((-e "/etc/cfingerd") && ($ARGV[0] ne "-docs")) {
    print "Skipping the creation of your /etc/cfingerd directory, since it already\n";
    print "exists.  If you want, copy everything out of the texts directory into\n";
    print "the /etc/cfingerd root directory, and everything out of the scripts\n";
    print "directory into the /etc/cfingerd/scripts directory.\n";
} else {
    print "Creating /etc/cfingerd directory...\n";
    system("mkdir /etc/cfingerd");
    system("chmod 700 /etc/cfingerd");
    print "Copying over text files to the new directory...\n";
    system("cp -f texts/* /etc/cfingerd");
    print "Creating /etc/cfingerd/scripts directory...\n";
    system("mkdir /etc/cfingerd/scripts");
    system("chmod 700 /etc/cfingerd/scripts");
    print "Copying over the new scripts...\n";
    system("cp -f scripts/* /etc/cfingerd/scripts");
    system("chmod 700 /etc/cfingerd/scripts/*");
}

system("cp -f src/cfingerd /usr/sbin/in.cfingerd");

if ($ARGV[0] ne "-docs") {
    print "\nThat completes the installation.  Check the \"INSTALL\" file for information\n";
    print "on how to put cfingerd in your inetd.conf file.  If it's already there, it\n";
    print "should work fine.  With that in mind, simply test it with a \"finger @\".\n";
    print "\n";
    print "Hit [RETURN]: ";
    <STDIN>;
    print "\n";
} else {
    print "\nDocumentation installation complete.\n";
}
