#!/usr/local/bin/perl -w

# Antonios Kavarnos (Antonios.Kavarnos.Home@cs.ntua.gr) 30 Sep 2000

# VERSION 0.3.17

# TODO
#	do not use ls, but glob
#	use Table.pm (e.g. for ls)
#	less: q to end, up-down to move...
#	run: better show of program run
#	ask name and save it
#	show name 
#	help_command
#	support for hp-terms in greek messages
#	PUT NAME (@hpc)
#	names even in greek and latin change (accented check)

use strict;

# ----- CONFIGURATION -----

my $editor_prg="/bin/rvi";
my $pc_prg="/usr/gnu/bin/gpc";
my $pager_prg="/usr/bin/less";
my $ls_prg="/bin/ls";
my $passwd_prg="/usr/bin/passwd";

my $programs_dir=$ENV{"HOME"}.".";
#my $info_fn=$ENV{"HOME"}."/.info";

# ----- END OF CONFIGURATION -----

#
# subroutines
#

sub user_input ($$)
{
	my($message,$regular)=@_;
	my($input);

	for (;;) {
		print "$message: ";
		chomp($input=<STDIN>);
		if ($input =~ /$regular/) {
			return $input;
		}
		print "Δώστε τα στοιχεία ΑΚΡΙΒΩΣ ΟΠΩΣ ΣΑΣ ΖΗΤΟΥΝΤΑΙ!\n";
	}
}

sub ask_enter ()
{
	print "\n\nΠατήστε <Enter> για να συνεχίσετε\n";
	<STDIN>;
	print "\n\n";
}

sub underline ($)
{
	my($line)=@_;
	my($length);

	chomp($line);
	print "$line\n";
	$length=length($line);
	print '-' x $length,"\n";
}

sub overline ($)
{
	my($line)=@_;
	my($length);

	chomp($line);
	$length=length($line);
	print '-' x $length,"\n";
	print "$line\n";
}

sub help_command (@)
{
	print "\n\nΗ ΕΝΤΟΛΗ ΑΥΤΗ ΔΕΝ ΕΙΝΑΙ ΕΤΟΙΜΗ ΑΚΟΜΗ\n\n";
}

sub dir_command (@)
{
	system($ls_prg);
}

sub dir_ext ($)
{
	my($param)=@_;

	#print "\tΑρχεία με κατάληξη $param που σας ανήκουν:\n";
	#print "\t------------------------------------------\n";
	underline("Αρχεία με κατάληξη $param που σας ανήκουν:");
	system("$ls_prg *$param");
	print '-'x70,"\n";
}

sub edit_command (@)
{
	my(@files)=@_;

	if (@files < 1) {
		dir_ext(".p");
		$files[0]=user_input("Δώστε όνομα αρχείου (επέκταση .p για πρόγραμμα Pascal)","^[A-Za-z0-9_.-]+\$");
	}
	system($editor_prg,$files[0]);
}

sub pascomp_command (@)
{
	my(@files)=@_;
	my($p_file,$exec_file);

	if (@files < 1) {
		dir_ext(".p");
		$files[0]=user_input("Δώστε όνομα αρχείου (επέκταση .p)","^[A-Za-z0-9_.-]+\.p\$");
	}
	$p_file=$files[0];
	if ($p_file !~ /\.p$/) {
		print "Η επέκταση του αρχείου δεν είναι .p\n";
		return;
	}
	$exec_file=$p_file;
	$exec_file=~s/\.p$/\.exec/;
	if (-e $exec_file) {
		chmod(0300,$exec_file);
	}
	system("$pc_prg -o $exec_file $p_file 2>&1 | $pager_prg");
	if (-e $exec_file) {
		chmod(0100,$exec_file);
	}
}

sub run_command (@)
{
	my(@files)=@_;

	if (@files < 1) {
		dir_ext(".exec");
		$files[0]=user_input("Δώστε όνομα αρχείου (επέκταση .exec για εκτελέσιμο)","^[A-Za-z0-9_.-]+\$");
	}
	print "\n\n\n\n\n";
	underline("===== Αρχή εκτέλεσης προγράμματος $files[0] =====");
	system("./$files[0]")==0 or print "Πρόβλημα στην εκτέλεση: $!\n";
	overline("===== Τέλος εκτέλεσης προγράμματος $files[0] =====");
}

sub delete_command (@)
{
	my(@files)=@_;

	if (@files < 1) {
		$files[0]=user_input("Δώστε όνομα αρχείου για ΜΗ ΑΚΥΡΩΣΙΜΗ διαγραφή","^[A-Za-z0-9_.-]+\$");
	}
	unlink("./$files[0]");
}

sub rename_command (@)
{
	my(@files)=@_;

	if (@files == 0) {
		$files[0]=user_input("Δώστε το όνομα ενός υπάρχοντος αρχείου για μετονομασία","^[A-Za-z0-9_.-]+\$");
		$files[1]=user_input("Δώστε το καινούριο όνομα για το παραπάνω αρχείο","^[A-Za-z0-9_.-]+\$");
	} elsif (@files != 2) {
		print "Η εντολή rename συντάσσεται με ακριβώς δύο ορίσματα\n";
		return;
	}
	rename($files[0],$files[1]) or print "Πρόβλημα στη μετονομασία";
}

sub chpass_command ()
{
	system($passwd_prg);
}

sub main_menu ()
{
	my($command,@commands);

	do {
		print "

======================================================================
ΕΘΝΙΚΟ ΜΕΤΣΟΒΙΟ ΠΟΛΥΤΕΧΝΕΙΟ
ΕΡΓΑΣΤΗΡΙΟ ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΥ ΥΠΟΛΟΓΙΣΤΩΝ
======================================================================

dir				κατάλογος των αρχείων που σας ανήκουν
edit [filename]			δημιουργία και επεξεργασια αρχείου κειμένου
pascomp [filename.p]		μεταγλώττιση αρχείου pascal
run [filename.exec]		εκτέλεση προγράμματος
delete [filename]		διαγραφή αρχείου
rename oldname newname		μετονομασία αρχείου
chpass				αλλαγή συνθηματικού (password)

exit				έξοδος από το σύστημα

======================================================================
";
		print "\n\tΕΝΤΟΛΗ: ";
		$command=<STDIN>;
		if (! defined $command) {
			$command="exit";
			@commands=();
		} else {
			chomp($command);
			$command=~s/^\s+//g;
			$command=~s/\s+$//g;
			$command=~s/[^A-Za-z0-9_. -]//g;
			@commands=split(/\s+/,$command);
			if (@commands > 0) {
				$command=shift(@commands);
			}
		}
		print "\n","-"x70,"\n";
		if ($command eq 'help') {
			help_command(@commands);
		} elsif ($command eq 'dir') {
			dir_command(@commands);
		} elsif ($command eq 'edit') {
			edit_command(@commands);
		} elsif ($command eq 'pascomp') {
			pascomp_command(@commands);
		} elsif ($command eq 'run') {
			run_command(@commands);
		} elsif ($command eq 'delete') {
			delete_command(@commands);
		} elsif ($command eq 'rename') {
			rename_command(@commands);
		} elsif ($command eq 'chpass') {
			chpass_command();
		} elsif ($command eq 'exit') {
			# nothing
		} elsif ($command eq '') {
			# nothing
		} else {
			print "\n\nΑΓΝΩΣΤΗ ΕΝΤΟΛΗ: \"$command\"\n\n";
		}
		print "\n","-"x70,"\n";
		if ($command ne "exit") {
			ask_enter();
		}
	} while ($command ne "exit");
}

#
# main
#

umask(022);
if (exists $ENV{"SSH2_CLIENT"}) {
	die "No SSH, stopped";
}
$ENV{"LESS"}='-XPless environment; press q to exit';
#chdir($programs_dir) or die "Could not chdir(): $!, stopped";
main_menu();
exit(0);

#####
##### DELETE
#####
die "DELETED";

#login();

#sub input_password ($)
#{
#	my($message)=@_;
#	my($password);
#
#	print "$message: ";
#	system("stty","-echo");
#	chomp($password=<STDIN>);
#	print "\n";
#	system("stty","echo");
#	return $password;
#}

#sub plain_fill_zero ($)
#{
#	my($plain)=@_;
#	my($result);
#
#	while (length($plain)<8) {
#		$plain.="\x0";
#	}
#	return $plain;
#}

#my $allow_user_creation_flag="T";
#my $allow_user_creation_flag="";
#my $passwd_fn="/home/akav/var/projects/nsh/passwd";
#my $passwd_fn="/home/novice/etc/passwd";
#my $users_dir="/home/akav/var/projects/nsh/USERS";

#sub password_crypt ($)
#{
#	my($plain)=@_;
#	my($time,$salt,$encrypted);
#	my($result);
#
#	$time=time;
#	$salt=join('',('.','/',0..9,'A'..'Z','a'..'z')[$time % 64,$time / 64 % 64]);
#	$result=crypt($plain,$salt);
#	#print STDERR "DEBUG ",__LINE__,": salt:$salt plain:$plain crypt:$result\n";
#	return $result;
#}

#sub password_ok ($$$)
#{
#	my($login,$plain,$encrypted)=@_;
#
#	return (crypt($plain,$encrypted) eq $encrypted);
#}

#{
#	my %login2password;
#
#	sub parse_password_file ()
#	{
#		my ($login,$passwd);
#
#		open(PASSWD,$passwd_fn) or die("Could not open $passwd_fn");
#		while (<PASSWD>) {
#			chomp;
#			($login,$passwd)=split(/:/);
#			$login2password{$login}=$passwd;
#		}
#		close(PASSWD);
#	}
#
#	sub login ()
#	{
#		my($first,$middle,$last);
#		my($birth_date,$plain_password,$verify_password);
#		my($login,$temp);
#
#		parse_password_file();
#		do {
#			$last=user_input("Επίθετο (σε λατινικούς χαρακτήρες)","^[A-Za-z]+\$");
#			$last=~tr/[A-Z]/[a-z]/;
#			$first=user_input("Μικρό όνομα (σε λατινικούς χαρακτήρες)","^[A-Za-z-]+\$");
#			$first=~tr/[A-Z]/[a-z]/;
#			$middle=user_input("Αρχικό γράμμα πατέρα (ένα λατινικό χαρακτήρα)","^[A-Za-z]\$");
#			$middle=~tr/[A-Z]/[a-z]/;
#			$birth_date=user_input("Ημερομηνία γέννησης (πχ. 14-06-1981)","^[0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]\$");
#
#			$login="$last.$first.$middle.$birth_date";
#			if (exists $login2password{$login}) {
#				$temp="yes";
#			} else {
#				$temp=user_input("\nΠλήρες όνομα: $first $middle. $last\nΗμερομηνία γέννησης: $birth_date\n\nΣωστά; (yes/no)","^((yes)|(no))\$");
#				$temp=~tr/[A-Z]/[a-z]/;
#			}
#		} while ($temp ne "yes");
#
#		if (exists $login2password{$login}) {
#			for (;;) {
#				$plain_password=input_password("Password");
#				if (! password_ok($login,$plain_password,$login2password{$login})) {
#					sleep(2);
#					print "ΛΑΘΟΣ Password! (control-c για έξοδο)\n";
#				} else {
#					last;
#				}
#			}
#		} elsif ($allow_user_creation_flag) {
#			print "\n","-"x70,"\nΔΗΜΙΟΥΡΓΙΑ ΚΑΙΝΟΥΡΙΟΥ ΛΟΓΑΡΙΑΣΜΟΥ (για να ακυρώσετε πατήστε control-c)\n\n";
#			print "Θα σας ζητηθεί ένα καινούριο Password. ΠΡΟΣΟΧΗ! ΜΗΝ ΤΟ ΞΕΧΑΣΕΤΕ.\n";
#			print "Θα σας ζητείται ΑΚΡΙΒΩΣ ΟΠΩΣ ΘΑ ΤΟ ΔΩΣΕΤΕ κάθε φορά που έρχεστε στο εργαστήριο!\n";
#			print "-"x70,"\n";
#			do {
#				$plain_password=input_password("Δώστε ένα καινούριο Password (ΔΕΝ ΦΑΙΝΕΤΑΙ ΣΤΗΝ ΟΘΟΝΗ)");
#				$verify_password=input_password("Ξαναδώστε για επιβεβαίωση το ίδιο καινούριο Password");
#				if ($plain_password ne $verify_password) {
#					print "Τα Passwords ΔΕΝ είναι ίδια\n";
#				}
#			} while ($plain_password ne $verify_password);
#
#			mkdir("$users_dir/$login",0700) or die "Could not mkdir $users_dir/$login: $!";
#			# XXXXX create lock file
#			open(PASSWD,">>$passwd_fn") or die "Could not append to $passwd_fn: $!";
#			print PASSWD "$login:",password_crypt($plain_password),"\n";
#			close(PASSWD);
#		} else {
#			print "\n\nΔΕΝ ΥΠΑΡΧΕΙ ΤΕΤΟΙΟΣ ΧΡΗΣΤΗΣ ΚΑΙ Η ΔΗΜΟΥΡΓΙΑ ΝΕΩΝ ΧΡΗΣΤΩΝ ΔΕΝ ΕΠΙΤΡΕΠΕΤΑΙ\n\n";
#			exit(1);
#		}
#
#		chdir("$users_dir/$login") or die "Could not chdir to $users_dir/$login: $!";
#		# XXXXX lock lock_file, if failed someone already in
#	}
#}

# XXXXX NAME HERE
