
# script_x2crm_desc()
sub script_x2crm_desc
{
return "X2CRM";
}

sub script_x2crm_uses
{
return ( "php" );
}

sub script_x2crm_longdesc
{
return "X2CRM is a fast and compact marketing automation, sales force and customer service application powered by an easy to use website-optimized visual workflow engine";
}

# script_x2crm_versions()
sub script_x2crm_versions
{
return ( "7.1" );
}

sub script_x2crm_category
{
return "Commerce";
}

sub script_x2crm_php_vers
{
return ( 5 );
}

sub script_x2crm_php_modules
{
return ( "mysql", "curl", "mbstring" );
}

sub script_x2crm_php_optional_modules
{
return ( "imap", "mcrypt", "openssl", "ssh2" );
}

sub script_x2crm_dbs
{
return ( "mysql" );
}

sub script_x2crm_php_vars
{
return ( [ 'memory_limit', '256M', '+' ] );
}

# script_x2crm_params(&domain, version, &upgrade-info)
# Returns HTML for table rows for options for installing X2CRM
sub script_x2crm_params
{
local ($d, $ver, $upgrade) = @_;
local $rv;
local $hdir = &public_html_dir($d, 1);
if ($upgrade) {
	# Options are fixed when upgrading
	local ($dbtype, $dbname) = split(/_/, $upgrade->{'opts'}->{'db'}, 2);
	$rv .= &ui_table_row("Database for X2CRM tables", $dbname);
	local $dir = $upgrade->{'opts'}->{'dir'};
	$dir =~ s/^$d->{'home'}\///;
	$rv .= &ui_table_row("Install directory", $dir);
	}
else {
	# Show editable install options
	local @dbs = &domain_databases($d, [ "mysql" ]);
	$rv .= &ui_table_row("Database for X2CRM tables",
		     &ui_database_select("db", undef, \@dbs, $d, "x2crm"));
	$rv .= &ui_table_row("Install sub-directory under <tt>$hdir</tt>",
			     &ui_opt_textbox("dir", &substitute_scriptname_template("x2crm", $d), 30, "At top level"));
	}
return $rv;
}

# script_x2crm_parse(&domain, version, &in, &upgrade-info)
# Returns either a hash ref of parsed options, or an error string
sub script_x2crm_parse
{
local ($d, $ver, $in, $upgrade) = @_;
if ($upgrade) {
	# Options are always the same
	return $upgrade->{'opts'};
	}
else {
	local $hdir = &public_html_dir($d, 0);
	$in{'dir_def'} || $in{'dir'} =~ /\S/ && $in{'dir'} !~ /\.\./ ||
		return "Missing or invalid installation directory";
	local $dir = $in{'dir_def'} ? $hdir : "$hdir/$in{'dir'}";
	local ($newdb) = ($in->{'db'} =~ s/^\*//);
	return { 'db' => $in->{'db'},
		 'newdb' => $newdb,
	         'dir' => $dir,
		 'path' => $in{'dir_def'} ? "/" : "/$in{'dir'}", };
	}
}

# script_x2crm_check(&domain, version, &opts, &upgrade-info)
# Returns an error message if a required option is missing or invalid
sub script_x2crm_check
{
local ($d, $ver, $opts, $upgrade) = @_;
$opts->{'dir'} =~ /^\// || return "Missing or invalid install directory";
$opts->{'db'} || return "Missing database";
if (-r "$opts->{'dir'}/protected/config/X2Config.php") {
	return "X2CRM appears to be already installed in the selected directory";
	}
local ($dbtype, $dbname) = split(/_/, $opts->{'db'}, 2);
local $clash = &find_database_table($dbtype, $dbname, "x2_");
$clash && return "X2CRM appears to be already using the selected database (table <tt>$clash</tt> exists)";
return undef;
}

# script_x2crm_files(&domain, version, &opts, &upgrade-info)
# Returns a list of files needed by X2CRM, each of which is a hash ref
# containing a name, filename and URL
sub script_x2crm_files
{
local ($d, $ver, $opts, $upgrade) = @_;
local @files = ( { 'name' => "source",
	   'file' => "X2CRM-$ver.zip",
	   'url' => "https://github.com/X2Engine/X2CRM/archive/refs/tags/$ver.zip" });
return @files;
}

sub script_x2crm_commands
{
return ("unzip");
}

# script_x2crm_install(&domain, version, &opts, &files, &upgrade-info)
# Actually installs X2CRM, and returns either 1 and an informational
# message, or 0 and an error
sub script_x2crm_install
{
local ($d, $version, $opts, $files, $upgrade, $domuser, $dompass) = @_;
local ($out, $ex);

if ($opts->{'newdb'} && !$upgrade) {
	local $err = &create_script_database($d, $opts->{'db'});
	return (0, "Database creation failed : $err") if ($err);
	}
local ($dbtype, $dbname) = split(/_/, $opts->{'db'}, 2);
local $dbuser = &mysql_user($d);
local $dbpass = &mysql_pass($d);
local $dbhost = &get_database_host($dbtype, $d);
local $dberr = &check_script_db_connection($dbtype, $dbname, $dbuser, $dbpass);
return (0, "Database connection failed : $dberr") if ($dberr);

# Extract tar file to temp dir and copy to target
local $temp = &transname();
local $err = &extract_script_archive($files->{'source'}, $temp, $d,
                                     $opts->{'dir'}, "X2CRM-*/x2engine");
$err && return (0, "Failed to extract source : $err");

# Update the install config file
local $url = &script_path_url($d, $opts);
local $cfile = "$opts->{'dir'}/installConfig.php";
local $lref = &read_file_lines_as_domain_user($d, $cfile);
foreach my $l (@$lref) {
	if ($l =~ /^\$host\s*=/) {
		$l = "\$host = '$dbhost';";
		}
	if ($l =~ /^\$db\s*=/) {
		$l = "\$db = '$dbname';";
		}
	if ($l =~ /^\$user\s*=/) {
		$l = "\$user = '$dbuser';";
		}
	if ($l =~ /^\$pass\s*=/) {
		$l = "\$pass = '$dbpass';";
		}
	if ($l =~ /^\$adminEmail\s*=/) {
		$l = '$adminEmail = \''.$d->{'emailto_addr'}.'\';';
		}
	if ($l =~ /^\$adminUsername\s*=/) {
		$domuser =~ s/-/_/g;
		$l = "\$adminUsername = '$domuser';";
		}
	if ($l =~ /^\$adminPassword\s*=/) {
		$l = "\$adminPassword = '$dompass';";
		}
	}
&flush_file_lines_as_domain_user($d, $cfile);

# Build the command line to call X2Engine's install script
local $cmd = &get_php_cli_command($opts->{'phpver'});
$cmd .= " initialize.php silent";
local $phpver = $opts->{'phpver'} || 5;
if (-d "$d->{'home'}/etc/php$phpver") {
	$cmd = "PHPRC=$d->{'home'}/etc/php$phpver $cmd";
	}

# Run it as the domain owner
local $out = &run_as_domain_user($d,
	"cd ".quotemeta($opts->{'dir'})." && ".$cmd." 2>&1");
if ($? || $out =~ /FAILED/) {
	return (-1, "Installation script failed : <pre>".
		    &html_escape($out)."</pre>");
	}

# Tell the user about the new install
local $rp = $opts->{'dir'};
$rp =~ s/^$d->{'home'}\///;
return (1, "Initial X2CRM installation complete. Go to <a target=_blank href='$url'>$url</a> to manage it.", "Under $rp using MySQL database $dbname", $url, $domuser, $dompass );
}

# script_x2crm_uninstall(&domain, version, &opts)
# Un-installs a X2CRM installation, by removing it's files
# Returns 1 on success and a message, or 0 on failure and an error
sub script_x2crm_uninstall
{
local ($d, $version, $opts) = @_;


# Remove the contents of the target directory
local $derr = &delete_script_install_directory($d, $opts);
return (0, $derr) if ($derr);

# Remove x2crm tables from the database
&cleanup_script_database($d, $opts->{'db'}, "x2_");

# Take out the DB
if ($opts->{'newdb'}) {
	&delete_script_database($d, $opts->{'db'});
	}

return (1, "Deleted X2CRM directory and tables.");
}

# script_x2crm_check_latest(version)
# Checks if some version is the latest for this project, and if not returns
# a newer one. Otherwise returns undef.
sub script_x2crm_latest
{
return ( "https://github.com/X2Engine/X2CRM/releases",
         "href.*\/tag\/([\\d\\.]+)" );
}

sub script_x2crm_site
{
return 'https://x2crm.com/';
}

sub script_x2crm_passmode
{
return (1, 6);
}

sub script_x2crm_disabled
{
return 0;
}

1;

