#!/usr/bin/perl -w

use strict;
use File::Copy;
use File::Path;
use File::Find;
use LWP::UserAgent;

sub modify_file
{
    my ($fname, $fparams) = @_;

    unless (open F, $fname){
        print STDERR "postinstall: can't open file `$fname` for reading\n";
        return 0;
    }

    my $file_content;
    while (<F>){
        $file_content .= $_;
    }
    close F;

    my ($k,$v);
    while (($k,$v)=each(%$fparams)){
        $file_content =~ s/\@\@${k}\@\@/$v/g;
    }

    unless (open F, ">$fname"){
        print STDERR "postinstall: can't open file `$fname` for writing\n";
        return 0;
    }
    print F $file_content;
    close F;
    return 1;
}


#########################
#	Quote Section	#
#########################

sub mysql_quote
{
    my ($qstr) = @_;
    # replace ' for \'
    # replace \ for \\
    $qstr =~ s/\\/\\\\/g;
    $qstr =~ s/'/\\'/g;
    return $qstr;
}

sub php_quote
{
    my ($qstr) = @_;
    # replace ' for \'
    # replace \ for \\
    $qstr =~ s/\\/\\\\/g;
    $qstr =~ s/'/\\'/g;
    return $qstr;
}

sub shell_quote
{
    my ($qstr) = @_;
    # replace ' for \'
    # replace \ for \\
    $qstr =~ s/\\/\\\\/g;
    $qstr =~ s/"/\\\"/g;
    $qstr =~ s/\$/\\\$/g;
    return $qstr;
}

sub parse_input_to_hash
{
    my %params = ();
    while (<STDIN>){
        my ($k,$v);
        if (/^([^=]+)=(.+)$/){
            $v = $2;
            chomp $v;
            $k = $1;
            $params{"$k"} = $v;
            print STDERR $_;
        }
    }
    return %params;
}

sub parse_plesk_config_file
{
    my %psa_params = ();
    open PSACONF, '/etc/psa/psa.conf';
    print "opening psa config\n";
    while (<PSACONF>){
        chomp;
        unless (/^#/){
            if (/^(\s*[_a-zA-Z]+)\s+(.+?)\s*$/){
                # print "$1 : $2\n";
                $psa_params{$1} = $2;
            }
        }
    }
    close PSACONF;
    return %psa_params;
}

sub check_parameter
{
    my ($param, %params) = @_;
    unless (defined $params{$param}){
        return 0;
    } else {
        return 1;
    }
}

sub check_parameters
{
    my ($imp_params, %params) = @_;
    my $is_error = 0;
    foreach (@$imp_params){
        unless (check_parameter($_, %params)){
            print "postinstall: no parameter $_ specified for application\n";
            $is_error = 1;
        }
    }
    if ($is_error){
        exit 1;
    }
}

sub get_proto
{
    my ($p_ssl_target_directory) = @_;
    my $proto;
    if ($p_ssl_target_directory eq 'true'){
            $proto = 'https://';
    } else {
            $proto = 'http://';
    }
    return $proto;
}

sub get_documents_directory
{
    my ($p_ssl_target_directory) = @_;
    my $documents_directory;
    if ($p_ssl_target_directory eq 'true'){
            $documents_directory = 'httpsdocs';
    } else {
            $documents_directory = 'httpdocs';
    }
    return $documents_directory;
}

sub get_root_url
{
    my ($proto, $documents_directory, $p_domain_name, $p_install_prefix) = @_;
    my $root_url = '';
    $root_url = $proto.$p_domain_name."/".$p_install_prefix;

    if($p_install_prefix eq "."){
        $root_url =~ s/\/\.//g;
    }
    return $root_url;
}


my @imp_params = qw( vhost_path domain_name install_prefix ssl_target_directory admin_login admin_passwd license_number );
my $is_error=0;

my %params = parse_input_to_hash();
my %psa_params = parse_plesk_config_file();
check_parameters(\@imp_params, %params);

my $proto = get_proto($params{'ssl_target_directory'});
my $documents_directory = get_documents_directory($params{'ssl_target_directory'});
my $root_url = get_root_url($proto, $documents_directory, $params{'domain_name'}, $params{'install_prefix'});

my $curl_cmd = "curl -k -G -d".'UserName='.$params{'admin_login'}.' -d Password='.$params{'admin_passwd'}.' -d RemoveScript=Yes -d RemoveData=Yes '.${root_url}."/remove.mvc?";

my $str_res_curl = `$curl_cmd`;

if ($?){
# error occured during mysql
    print STDERR "unable to get url:\n${str_res_curl}\n";
    print STDERR "$curl_cmd\n";
    exit 1;
}

if (-e "".$params{'vhost_path'}."/mivadata/setup.xml"){
    unlink("".$params{'vhost_path'}."/mivadata/setup.xml");
}

if($params{'install_prefix'} eq "."){
        unlink("".$params{'vhost_path'}."/httpsdocs/admin.mvc");
        unlink("".$params{'vhost_path'}."/httpsdocs/5.00");
        unlink("".$params{'vhost_path'}."/httpsdocs/graphics");
        unlink("".$params{'vhost_path'}."/httpsdocs/evalchecki.mvc");
        unlink("".$params{'vhost_path'}."/httpsdocs/evalcheckp.mvc");
        unlink("".$params{'vhost_path'}."/httpsdocs/paylinki.mvc");
        unlink("".$params{'vhost_path'}."/httpsdocs/paylinkp.mvc");
        unlink("".$params{'vhost_path'}."/httpsdocs/paypalc.mvc");
        unlink("".$params{'vhost_path'}."/httpsdocs/paypali.mvc");
        unlink("".$params{'vhost_path'}."/httpsdocs/paypalp.mvc");
        unlink("".$params{'vhost_path'}."/httpsdocs/remove.mvc");
} else{
    if (-e "".$params{'vhost_path'}."/httpsdocs/".$params{'install_prefix'}){
        unlink("".$params{'vhost_path'}."/httpsdocs/".$params{'install_prefix'});
    }
}
