#!/usr/bin/perl

=pod
UE_APACHE_ROOT=/var/www;                                     export UE_APACHE_ROOT;
UE_CGI_DIR=$APACHE_ROOT/cgi-bin;                             export UE_CGI_DIR;
UE_IMAGE_DIR=$APACHE_ROOT/html/images;                       export UE_IMAGE_DIR;
UE_EXT_PATH=$APACHE_ROOT/html/js/ext-core/ext-core-debug.js; export UE_EXT_PATH;

UE_CAT_PROJ=MBC;                                             export UE_DOMAIN;
UE_CONTROLLER_DIR=/var/www/catalyst/MBC/lib/MBC/Controller;  export UE_CONTROLLER_DIR;
UE_TMPL_DIR=/var/www/catalyst/MBC/root/src;                  export UE_TMPL_DIR;

UE_DOMAIN=bioinformatics.ualr.edu;                           export UE_DOMAIN;
UE_ROOT_URL=http://$UE_DOMAIN/catalyst;                      export UE_ROOT_URL;
UE_EXT_URL=http://$UE_DOMAIN/js/ext-core/ext-core-debug.js;  export UE_EXT_URL;
=cut

use IO::Pipe; #used to safely run external commands

our ( @perl_switches, @catalyst, $domain );

# Redirect warnings and errors
open( STDERR, '>', '/tmp/uploadengine-install.tmp' );

start();

# Attempt to automagically find dirs from httpd.conf
my ( $root_dir, $html_dir, $cgi_dir ) = parse_conf( apache_conf() );

#if that didn't work, then ask the user
if(not defined $root_dir){
	# Get apache root 
	$root_dir = ask_user( "What is your root apache directory?", $root_dir );
}
if(not defined $domain){
	# Get domain name
	$domain = ask_user( "Please enter your server domain", get_domain() );
}

# Install config file
my $config_file = $root_dir . '.uploadengine';
if ( -e $root_dir . '.uploadengine' ) { 
	print "\nI found an existing config file:\n  $config_file \n\n"; 
	my $result = ask_user( "Replace the existing file", 'n' );
	if ( $result =~ m/y/i ) { 
		remove_config( $config_file); 
		install_config( $config_file ); 
	}
}
else{ 
	install_config( $config_file ); 
}

# Install CGI::UE

# Install mysql database

# Install Core in at least one Catalyst Controller dir
foreach my $switch ( @perl_switches ) {
#upload.pm controller
	if ( $switch =~ m#/(\w+)/lib# ) { 
		my $project = $1; 
		my $result  = ask_user( "Attempt to install catalyst '$switch'", 'n' );
		if ( $result =~ m/y/i ) {
			install_catalyst( $switch );
		}
	}
}

# Install Ext JavaScript files

# Install CGI sample files
if(not defined $cgi_dir){
	$cgi_dir=ask_user( "Your cgi-bin directory", $cgi_dir );
}
my $result = ask_user( "Install the CGI samples", 'n' );
if ( $result =~ m/y/i ){
	install_cgi( $cgi_dir, $html_dir );
}

# Install Catalyst sample files
foreach my $switch ( @perl_switches ) {
#uploadEG
	if ( $switch =~ m#/(\w+)/lib# ) { 
		my $project = $1; 
		my $result  = ask_user( "Attempt to install catalyst '$switch'", 'n' );
		if ( $result =~ m/y/i ) {
			install_catalyst( $switch );
		}
	}
}

close( STDERR );

exit;




sub ask_user {
	my ( $question, $default, $response ) = @_;
	print "$question [$default]:";
	chomp( $response = <> ); 
	if (! $response ) { $response = $default; }
	return $response;
}


sub get_apache_root {
	my ( $root_dir, $html_dir, $cgi_dir ) = parse_conf( apache_conf() );
	return ask_user( "Your root apache directory", $root_dir );
}	
	
sub get_domain {
	# Get domain name
	my ( $domain ) = get_domain();
	$domain = ask_user( "Your server domain", $domain );
}	

sub apache_conf {
	my $InputFromCmdHandle=new IO::Pipe;
	my $cmd     = 'find / httpd | grep "httpd.conf$"';
	open($InputFromCmdHandle,"-|",$cmd);
	my @resultArray=<$InputFromCmdHandle>;
	close($InputFromCmdHandle) or die("my child did not exit properly: $?");
	my $apache_conf;

	foreach my $result ( @resultArray ) {
		if ( $result =~ m#home# ) { 
			next;
		} else {
			if ( $result =~ m/httpd\.conf$/ ) { 
				$apache_conf = $result; 
			}
		}
	}

	if ( $apache_conf ) { print "\nI think I found your apache configuration file:\n  $apache_conf \n\n"; }
	else                { print "\nI could not find your apache configuration file.\n\n"; }

	return $apache_conf
}

sub parse_conf {
	my $text  = get_file_text( @_ );
	my @lines = split( /\n/, $text );

	my ( $html_dir, $cgi_dir );
	foreach my $line ( @lines ) {
		if ( $line =~ m/^#/ ) {
			next;
		} else {
			if    ( $line =~ m/Directory/ ) {
				if    ( $line =~ m#^[^/]*([-/\w]+html)# )    { $html_dir = $1; }
				elsif ( $line =~ m#^[^/]*([-/\w]+cgi-bin)# ) { $cgi_dir  = $1; }
			} 
			elsif ( $line =~ m#PerlSwitches -I(.*)# ) { 
				push @perl_switches, $1;
			}
		}
	}

	return get_root_dir( $html_dir, $cgi_dir ), $html_dir, $cgi_dir;
}

sub get_root_dir {
	my ( $html, $cgi, $dir ) = @_;
	if ( $cgi =~ m#^(.*)cgi-bin# ) { $dir = $1; }
	if ( $html =~ m#^$dir# )       { return $dir; }
	else                           { return '';   }
}

sub get_file_text {
	my ( $file, $lines ) = @_;
	if ( -s $file ) {
		open( INFILE, '<', $file );
		while ( my $line = <INFILE> ) { $lines .= $line; }
		close( INFILE );
	} else {
		print "FILE NOT FOUND: $file \n";
		exit;
	}
	return $lines;
}

sub start {
	print "\nCGI::UploadEngine Install Script (c) 2010 Roger Hall\n\n";
	print "I will help you do the following tasks:\n";
	print "   1. Install the runtime config file in apache root directory\n";
	print "   2. Ask you to optionally install the CGI files\n";
	print "      A. Create a tmpl directory in the apache root directory\n";
	print "      B. Install the tmpl files\n";
	print "      C. Install the CGI files and libraries\n";
	print "   3. Ask you to optionally install catalyst Controllers\n";
	print "      A. Install the tmpl files\n";
	print "      B. Install the Controller modules \n";
	print "   4. Create and initialize 'files' database \n";
	print "      Note: you will need to enter your password, but it will \n";
	print "      not be saved or transmitted.\n";
	print "   2. directory\n";
}

sub install_config {
	my ( $file ) = @_; 
	my $InputFromCmdHandle=new IO::Pipe;
	my $cmd = "cp uploadengine $file";
	open($InputFromCmdHandle,"-|",$cmd);
	my @resultArray=<$InputFromCmdHandle>;
	my $result = join("\n",@resultArray);
	close($InputFromCmdHandle) or die("my child did not exit properly: $?");
	if ( $result ) { print "\nERROR: $result\n"; }
	else           { print "\nI installed the configuration file:\n  $file\n\n"; }
	return $result;
}

sub remove_config {
	my ( $file ) = @_; 
	my $InputFromCmdHandle=new IO::Pipe;
	my $cmd = "rm -f $file";
	open($InputFromCmdHandle,"-|",$cmd);
	my @resultArray=<$InputFromCmdHandle>;
	my $result = join("\n",@resultArray);
	close($InputFromCmdHandle) or die("my child did not exit properly: $?");
	if ( $result ) { print "\nERROR: $result\n"; }
	else           { print "\nI removed the configuration file:\n  $file\n"; }
	return $result;
}

sub install_cgi {
	my ( $cgi_dir, $html_dir ) = @_; 
	
	my $images_dir = ask_user( 'Image directory', $html_dir . '/images' );

	print "\n";
	copy_file( 'cgi-bin/upload_cfg.pl', $cgi_dir );
	copy_file( 'cgi-bin/upload_html.pl', $cgi_dir );
	copy_file( 'cgi-bin/form_eg.cgi', $cgi_dir );
	copy_file( 'cgi-bin/handler_eg.cgi', $cgi_dir );
	copy_file( 'images/alert.jpg', $images_dir );
	copy_file( 'images/checked.jpg', $images_dir );
	print "\n";

	return $result;
}

sub copy_file {
	my ( $file, $dir ) = @_;
	my $InputFromCmdHandle=new IO::Pipe;
	my $cmd = "cp $file $dir";
	open($InputFromCmdHandle,"-|",$cmd);
	my @resultArray=<$InputFromCmdHandle>;
	my $result = join("\n",@resultArray);
	close($InputFromCmdHandle) or die("my child did not exit properly: $?");
	print "     CMD: $cmd\n";
	if ( $result ) { print "\nERROR: $result\n"; }
	else           { print "I copied the file $file to $dir.\n"; }
}

sub install_config {
	my ( $file ) = @_; 
	my $InputFromCmdHandle=new IO::Pipe;
	my $cmd = "cp uploadengine $file";
	open($InputFromCmdHandle,"-|",$cmd);
	my @resultArray=<$InputFromCmdHandle>;
	my $result = join("\n",@resultArray);
	close($InputFromCmdHandle) or die("my child did not exit properly: $?");
	if ( $result ) { print "\nERROR: $result\n"; }
	else           { print "\nI installed the configuration file:\n  $file\n\n"; }
	return $result;
}


sub install_catalyst {
	my ( $switch ) = @_; 
	my $InputFromCmdHandle=new IO::Pipe;
	my $cmd = "cp uploadengine $file";
	open($InputFromCmdHandle,"-|",$cmd);
	my @resultArray=<$InputFromCmdHandle>;
	my $result = join("\n",@resultArray);
	close($InputFromCmdHandle) or die("my child did not exit properly: $?");
	if ( $result ) { print "\nERROR: $result\n"; }
	else           { print "\nI installed the configuration file:\n  $file\n\n"; }
	return $result;
}

sub get_domain { 
	my $InputFromCmdHandle=new IO::Pipe;
	my $cmd="uname -n";
	open($InputFromCmdHandle,"-|",$cmd);
	my @resultArray=<$InputFromCmdHandle>;
	chomp(@resultArray); 
	#i'm shifting cuz there should only be one element
	my $result = shift(@resultArray);
	close($InputFromCmdHandle) or die("my child did not exit properly: $?");
	return $result; 
}


__END__

    
