#! /usr/bin/perl
#
# psh - Perl Shell
#
# Copyright (C) 1999-2000 Gregor N. Purdy. All rights reserved.
# This script is free software. It may be copied or modified according
# to the same terms as Perl itself.
#

package Psh; # still use a package so getopt etc. is not imported into
             # the shell namespace

use locale;
use Psh;
use Psh::Locale::Base;
use Getopt::Std;

use vars qw($VERSION %opt);

$VERSION='0.006';

#
# Parse the command line and deal with the options except -r, which is
# handled in the MAIN program below. We do this part vary early in this
# file so that the results apply to all the setting up we do before the
# MAIN program.
#

getopts('dwr:c:', \%opt);


#
# -w is "warnings mode":
#

if ($opt{'w'}) {
	Psh::Util::print_out_i18n('simulate_perl_w');

	$^W = 1;
	use strict;
}

#
# -d is "debug mode":
#

if ($opt{'d'}) { $Psh::debugging = 1; }
else           { $Psh::debugging = 0; }

Psh::Util::print_debug("Debugging!\n");

Psh::minimal_initialize;
Psh::process_rc($opt{'r'});
Psh::finish_initialize;

# TODO: Is this implementation equivalent to sh's ?
if($opt{'c'}) {
	Psh::evl($opt{'c'});
	exit 0;
}

if (@ARGV) {
	Psh::process_args;
} else {
	while (1) {
		eval { Psh::main_loop; };
		Psh::handle_message($@,'main_loop');
    }
}

END {	# try and write the history no matter how we exit
	if ($Psh::save_history && !$Psh::readline_saves_history) {
		my $fhist = new FileHandle($Psh::history_file, 'a');
		if (defined($fhist)) {
			eval { flock($fhist, LOCK_EX); };
			if( @Psh::history>$Psh::history_length) {
				splice(@Psh::history,0,@Psh::history-$Psh::history_length);
			}
			foreach (@Psh::history) {
				$fhist->print("$_\n");
			}
			eval { flock($fhist, LOCK_UN); };
			$fhist->close();
		}
	}
}


END {	# restore the old $ENV{SHELL}
	$ENV{SHELL} = $Psh::old_shell if $Psh::old_shell;
}


exit 0;



# The following is for Emacs - I hope it won't annoy anyone
# but this could solve the problems with different tab widths etc
#
# Local Variables:
# tab-width:4
# indent-tabs-mode:t
# c-basic-offset:4
# perl-indent-level:4
# End:

