#! /usr/local/bin/perl
#############################################################################
# (a) Mehmet Sen
#############################################################################
$caller=$ARGV[0];
if ($caller ne "Dontworycontinuetorun")
{print "Please do not run me directly, instead run RUNME!!\n\t\tThank you.\n";exit;}


print " \n\n Please enter suggested values in paranthesis as long as there is no conflict in your circumstances!!\n\n ";
print " \n\n\t\tPRESS ENTER TO KEEP DEFAULTS!!!!\n\n";
print " \t\t\t\t-OK-\n\n";
$Ok=<STDIN>;

$install_user=getlogin;
$hostname =qx'hostname';chop($hostname);
($machine,@_)= split (/\./,$hostname);
$post_domain=join('.',@_);
$domain="students.".$post_domain;
$dir=qx'pwd';chop($dir);
$port2=2000;
$ports=7079;
$portd=9700;
$java="/usr/npac/java2/bin/java";

#print "\t\tEnter INSTALLATION DIRECTORY ($dir): ";
#$install_dir=<STDIN>;chop($install_dir);
if ($install_dir eq""){$install_dir=$dir;}
if ($install_dir =~ /\/\Z/){chop($install_dir); }


print "\t\tEnter WEB  DOMAIN NAME ($domain) : ";
$install_domain=<STDIN>;chop($install_domain);if ($install_domain eq""){$install_domain=$domain;}


print "\t\tEnter INSTALLATION MACHINE NAME ($machine) : ";
$install_machine=<STDIN>;chop($install_machine);if ($install_machine eq""){$install_machine=$machine;}

print "\t\tEnter URL PORT ($port2) : ";
$install_port2=<STDIN>;chop($install_port2);if ($install_port2 eq""){$install_port2=$port2;}
$install_port1=$install_port2-2;

print "\t\tEnter INTERNAL SERVLET PORT ($ports) : ";
$install_servlet_port=<STDIN>;chop($install_servlet_port);if ($install_servlet_port eq""){$install_servlet_port=$ports;}

print "\t\tEnter INTERNAL DATABASE PORT ($portd) : ";
$install_database_port=<STDIN>;chop($install_database_port);if ($install_database_port eq""){$install_database_port=$portd;}

#print "\t\tEnter CGI SCRIPT ALIAS (cgi-bin) :" ;
#$install_cgi_end=<STDIN>;chop($install_cgi_end);

print "\t\tEnter 'java' path ($java) :";
$install_java=<STDIN>;chop($install_java);if ($install_java eq""){$install_java=$java;}

$jdbc="jdbc:msql://$install_machine.$post_domain:$install_database_port/GS";
print "\t\tEnter JDBC DRIVER URL ($jdbc): ";
$install_jdbc=<STDIN>;chop($install_jdbc);if ($install_jdbc eq""){$install_jdbc=$jdbc;}



$install_url="https://$install_domain:$install_port2";
$install_cgi="https://$install_domain:$install_port2/$install_cgi_end";

print "\n\nDear  $install_user, \n\n ";
print "Installing to \t\t\t$install_dir\n ";
print "Installing server for\t\t$install_url \n\t  with JDBC at\t\t$install_jdbc\n ";
print "Reserving the following ports;\t$install_port1,$install_port2,$install_servlet_port,$install_database_port\n\n ";

print "P.S. IF ANY VALUE IS WRONG, KILL THIS PROCESS AND START AGAIN,\n\n\t\t\t\tor\n\n\t\t\t\tpress enter to CONTINUE.\n\n";
$ddddd=<STDIN>;
print ".\n.\n.\n.\n.\n.\n..................... Starting Updates!\n";

#############################################################################

$dirname = "$install_dir";
$subdir = 1;
$extension='ALL';
$DONTTOUCHBACKUPS=1;
$replace="INSTALL-";
$by="INSTALL-*";



print ".\n.\n.\n.\n.\n.\n..................... Replacing $replace with $by in $dirname \n";
if ($DONTTOUCHBACKUPS) {
    print "SKIP  *~ \n" ;
    print "SKIP  *.default\n";
    print "SKIP  *.tar\n";
    print "SKIP  *.gz\n";
    print "SKIP  *.zip\n";
    print "SKIP  *.jar\n";
}
    
handledir($dirname,0);
print "\n=====================================================\n";
print "\nPlease place your security certificate and private key to $install-dir/sslcert as 'certificate.crt' and 'private.key'\n";


#############################################################################
sub handledir() {
local($dir,$l)=@_;
#print "$dir\n";
if ($l ==1 ) { print "\tCHECKING $dir\n"; }
opendir(DIR, $dir) || print "ERROR: CAN NOT OPEN DIRECTORY:  $dir !!!\n";
foreach $fn ( readdir(DIR)) {
	if ($fn eq ".") { next;}
	elsif ($fn eq "..") { next;}
	$fn=$dir."/".$fn;

	if (-d $fn) {
	        if ( $x =readlink($fn) ) {
		    print "SKIPPING THE LINK at $fn --> $x \n";
		    next;
		}
		$l+=1;
		if ($subdir) {
		    handledir($fn,$l);
		}
		$l-=1;
	} else {
	        if ( $x =readlink($fn) ) {
		    print "SKIPPING THE LINK at $fn --> $x \n";
		    next;
		}
		if ($extension eq "ALL" ) {;}
		elsif (!($fn =~ /\.$extension\b/ )){ next;}
		else{ print "$fn WARNING"; exit;}
		if ($DONTTOUCHBACKUPS) {
		    # print "$fn ? ";
		    if ( ($fn =~ /~/ )){ next;}
		    elsif ( ($fn =~ /\.default\b/ )){ next;}
		    elsif ( ($fn =~ /\.jar\b/ )){next;}
		    elsif ( ($fn =~ /\.tar\b/ )){next;}
		    elsif ( ($fn =~ /\.gz\b/ )){next;}
		    elsif ( ($fn =~ /\.zip\b/ )){next;}
		}
		if (  searchfile($fn) )
		{handlefile($fn,$l);	}
	}
}
}

sub searchfile(){
    local($file)=@_;
    open(IN, "<$file"); 
    while (<IN>) {
	$line=$_;
	if ($line=~ /$replace/) { close(IN); print "$file "; return 1;}
    }
    close(IN);
    #print "$file CLEAR\n";
    return 0;
}

sub handlefile(){
local($file,$l)=@_;
#print "Return without handling \n";return;
open(IN, "<$file"); 
$of=$file."___temp";
open(OUT, ">$of"); 

	 while (<IN>) {
		$line=$_;
		$line=~ s/INSTALL-DIR/$install_dir/g;
		$line=~ s/INSTALL-PORT1/$install_port1/g;
		$line=~ s/INSTALL-PORT2/$install_port2/g;
		$line=~ s/INSTALL-SERVLET-PORT/$install_servlet_port/g;
		$line=~ s/INSTALL-DATABASE-PORT/$install_database_port/g;
		$line=~ s/INSTALL-JDBC/$install_jdbc/g;
		$line=~ s/INSTALL-URL/$install_url/g;
		$line=~ s/INSTALL-SERVER-NAME/$install_domain/g;
		$line=~ s/INSTALL-JAVA/$install_java/g;
		$line=~ s/INSTALL-USER/$install_user/g;
	 	print OUT $line;
	}
 close(IN);
 close(OUT);

 #rename($of,$file); #hurts attributes
system("cp $of $file");unlink("$of");
 print " OK \n";

}




