#! /usr/bin/env perl
# kcd-inst
# Copyright (c) 2000, 2001 Kriang Lerdsuwanakij
# email:	lerdsuwa@users.sourceforge.net
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

use English;

$prog_name = "kcd-inst";
$prog_ver = "\5.7.0";	# Backslash make it not a list variable

$sysconfdir = "\/etc";	# Backslash make it not a list variable

$not_available = "not available";
$default_mode = $not_available;

sub version_and_exit {
	print "$prog_name $prog_ver\n";
	exit 0;
}

sub help_and_exit {
	print "$prog_name $prog_ver - Install kcd command in user shell startup files\n";
	print "Usage: $prog_name [local|global]\n";
	print "       $prog_name [-h|--help|-v|--version]\n\n";
	print "Default mode is $default_mode for this installation.\n";
	exit 0;
}

sub modify_startup_real {
	my @parm = @_;
	my ($file, $mode, $run_file);

	die "$prog_name: invalid parameter in modify_startup\n"
		if scalar(@parm) < 3;

	$file = shift @parm;
	$mode = shift @parm;
	$run_file = shift @parm;

	die "$prog_name: invalid mode in modify_startup\n"
		if $mode ne "sh" && $mode ne "csh";

	if (! -e $file) {
		open OUTFILE, ">$file"
			or die "$prog_name: cannot create $file\n";
		print OUTFILE "# Created by kcd\n";
	}
	elsif (! -f $file) {
		die "$prog_name: $file is not a file, ignored\n";
	}
	else {
		open INFILE, "$file"
			or die "$prog_name: cannot open $file for reading\n";

		while (<INFILE>) {
			if (/For [Kk][Cc][Dd]/) {
				die "$prog_name: \"For kcd\" marker found, $file ignored\n";
			}
		}

		open OUTFILE, ">>$file"
			or die "$prog_name: cannot open $file for append\n";
	}

	print "$prog_name: updating $file\n";

	print OUTFILE "\n# For kcd\n";
	if ($mode eq "sh") {
		print OUTFILE "if [ -e \"$run_file\" ]; then\n";
		print OUTFILE "\t. \"$run_file\"\n";
		print OUTFILE "fi\n";
	}
	else {
		print OUTFILE "test -e \"$run_file\"\n";
		print OUTFILE "if (\$status == 0) then\n";
		print OUTFILE "\tsource \"$run_file\"\n";
		print OUTFILE "endif\n";
	}
	close OUTFILE;
}

sub modify_startup {
	eval {
		modify_startup_real @_;
	};
	if ($@) {
		print $@;
	}
}

if ($0 =~ /\/bin\/$prog_name/) {
	$default_mode = "global";
}
elsif ($0 =~ /\/kcd-bin\/$prog_name/) {
	$default_mode = "local";
}

if (@ARGV > 1) {
	help_and_exit();
}
elsif (@ARGV == 1) {
	if ($ARGV[0] eq "-h" || $ARGV[0] eq "--help") {
		help_and_exit();
	}
	elsif ($ARGV[0] eq "-v" || $ARGV[0] eq "--version") {
		version_and_exit();
	}
	else {
		$mode = $ARGV[0];
	}
}
else {
	if ($default_mode eq $not_available) {
		help_and_exit();
	}
	else {
		$mode = $default_mode;
	}
}

if ($mode eq "local") {
	$run_file_sh = "$ENV{HOME}/kcd-bin/kcd.sh.userinit";
	$run_file_csh = "$ENV{HOME}/kcd-bin/kcd.csh.userinit";
}
elsif ($mode eq "global") {
	if (-e "$sysconfdir/profile.d") {
		$run_file_sh = "$sysconfdir/profile.d/kcd.sh";
		$run_file_csh = "$sysconfdir/profile.d/kcd.csh";
	}
	else {
		$run_file_sh = "$sysconfdir/kcd.sh.init";
		$run_file_csh = "$sysconfdir/kcd.csh.init";
	}
}
else {
	die "$prog_name: invalid argument $mode\n";
}

$found = 0;
while (($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwent) {
#	print $uid, " ", $name, " ", $UID, " ", $ENV{USER}, "\n";
	if ($uid == $UID && $name eq $ENV{USER}) {
		$found = 1;
		last;
	}
}
endpwent;

die "$prog_name: cannot find user information\n" if ! $found;
die "$prog_name: cannot find user home directory\n" if ! $dir;
die "$prog_name: cannot find user login shell\n" if ! $shell;

# For bash, zsh
modify_startup("$dir/.bashrc", "sh", $run_file_sh)
	if -e "$dir/.bashrc" || $shell =~ /\/bash$/;
modify_startup("$dir/.zshrc", "sh", $run_file_sh)
	if -e "$dir/.zshrc" || $shell =~ /\/zsh$/;

# For ash
modify_startup("$dir/.profile", "sh", $run_file_sh)
	if $shell =~ /\/ash$/;

# For tcsh
modify_startup("$dir/.tcshrc", "csh", $run_file_csh)
	if -e "$dir/.tcshrc" || ($shell =~/\/tcsh$/ && ! -e "$dir/.cshrc");
modify_startup("$dir/.cshrc", "csh", $run_file_csh)
	if -e "$dir/.cshrc";

