
# script_xoops_desc()
sub script_xoops_desc
{
return "Xoops";
}

sub script_xoops_uses
{
return ( "php" );
}

sub script_xoops_longdesc
{
return "XOOPS is a free open-source content management systems";
}

# script_xoops_versions()
sub script_xoops_versions
{
return ( "2.5.11-Beta1", "2.5.10" );
}

sub script_xoops_category
{
return "CMS";
}

sub script_xoops_php_vers
{
return ( 5 );
}

sub script_xoops_php_modules
{
return ("mysql", "iconv", "mbstring",
        "intl", "xml", "gd", "curl");
}

sub script_xoops_dbs
{
return ("mysql");
}

# script_xoops_params(&domain, version, &upgrade-info)
# Returns HTML for table rows for options for installing Wordpress
sub script_xoops_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);
	if ($dbtype) {
		$rv .= &ui_table_row("Database for Xoops 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 Xoops tables",
		     &ui_database_select("db", undef, \@dbs, $d, "xoops"));
	$rv .= &ui_table_row("Install sub-directory under <tt>$hdir</tt>",
			     &ui_opt_textbox("dir", &substitute_scriptname_template("xoops", $d), 30, "At top level"));
	}
return $rv;
}

# script_xoops_parse(&domain, version, &in, &upgrade-info)
# Returns either a hash ref of parsed options, or an error string
sub script_xoops_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_xoops_check(&domain, version, &opts, &upgrade-info)
# Returns an error message if a required option is missing or invalid
sub script_xoops_check
{
local ($d, $ver, $opts, $upgrade) = @_;
$opts->{'dir'} =~ /^\// || return "Missing or invalid install directory";
$opts->{'db'} || return "Missing database";
if (-r "$opts->{'dir'}/admin.php") {
	return "Xoops appears to be already installed in the selected directory";
	}
local ($dbtype, $dbname) = split(/_/, $opts->{'db'}, 2);
local $clash = &find_database_table($dbtype, $dbname, "xoops_.*");
$clash && return "Xoops appears to be already using the selected database (table $clash)";
return undef;
}

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

sub script_xoops_commands
{
return ("tar", "gunzip");
}

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

# Get and test DB connection. We may not have this if upgrading from an older
# installer though.
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("mysql", $d);
if ($dbname) {
	local $dberr = &check_script_db_connection($dbtype, $dbname, $dbuser, $dbpass);
	return (0, "Database connection failed : $dberr") if ($dberr);
	}

# Preserve old config file
local $cfile = "$opts->{'dir'}/mainfile.php";
local $cfile_sec = "$opts->{'dir'}/xoops_data/data/secure.php";
local $srccfile = "$opts->{'dir'}/mainfile.dist.php";
local $oldcfile = &transname();
local $oldcfile_sec = &transname();
if ($upgrade) {
	&copy_source_dest($cfile, $oldcfile);
	&copy_source_dest($cfile_sec, $oldcfile_sec) if (-r $cfile_sec);
	}

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

# Set file permissions
&make_file_php_writable($d, "$opts->{'dir'}/mainfile.php");
&make_file_php_writable($d, "$opts->{'dir'}/uploads", 1);
&make_file_php_writable($d, "$opts->{'dir'}/cache", 1);
&make_file_php_writable($d, "$opts->{'dir'}/templates_c", 1);

local $url = &script_path_url($d, $opts);
local $urlnoslash = $url;
$urlnoslash =~ s/\/$//;
if ($upgrade) {
	# Put back old config file
	&copy_source_dest_as_domain_user($d, $oldcfile, $cfile);
	&copy_source_dest_as_domain_user($d, $oldcfile_sec, $cfile_sec) if (-r $cfile_sec);
	&unlink_file("$opts->{'dir'}/install");
	}
else {
	# Edit main configuration file
	&copy_source_dest_as_domain_user($d, $srccfile, $cfile);
	local $lref = &read_file_lines_as_domain_user($d, $cfile);
	foreach my $l (@$lref) {
		if ($l =~ /^(\s*)define\(['|"]XOOPS_ROOT_PATH['|"]/) {
			$l = "${1}define(\"XOOPS_ROOT_PATH\", \"$opts->{'dir'}\");\n";
			}
		if ($l =~ /^(\s*)define\(['|"]XOOPS_PATH['|"]/) {
			$l = "${1}define(\"XOOPS_PATH\", \"$opts->{'dir'}/xoops_lib\");\n";
			}
		if ($l =~ /^(\s*)define\(['|"]XOOPS_VAR_PATH['|"]/) {
			$l = "${1}define(\"XOOPS_VAR_PATH\", \"$opts->{'dir'}/xoops_data\");\n";
			}
		if ($l =~ /^(\s*)define\(['|"]XOOPS_URL['|"]/) {
			$l = "${1}define(\"XOOPS_URL\", \"$urlnoslash\");\n";
			}
		if ($l =~ /^(\s*)define\(['|"]XOOPS_DB_TYPE['|"]/) {
			$l = "${1}define(\"XOOPS_DB_TYPE\", \"mysql\");\n";
			}
		if ($l =~ /^(\s*)define\(['|"]XOOPS_DB_PREFIX['|"]/) {
			$l = "${1}define(\"XOOPS_DB_PREFIX\", \"xoops\");\n";
			}
		if ($l =~ /^(\s*)define\(['|"]XOOPS_DB_HOST['|"]/) {
			$l = "${1}define(\"XOOPS_DB_HOST\", \"$dbhost\");\n";
			}
		if ($l =~ /^(\s*)define\(['|"]XOOPS_DB_USER['|"]/) {
			$l = "${1}define(\"XOOPS_DB_USER\", \"$dbuser\");\n";
			}
		if ($l =~ /^(\s*)define\(['|"]XOOPS_DB_PASS['|"]/) {
			$l = "${1}define(\"XOOPS_DB_PASS\", \"".
			     &php_quotemeta($dbpass)."\");\n";
			}
		if ($l =~ /^(\s*)define\(['|"]XOOPS_DB_NAME['|"]/) {
			$l = "${1}define(\"XOOPS_DB_NAME\", \"$dbname\");\n";
			}
		if ($l =~ /^(\s*require\s+XOOPS_VAR_PATH.*?)/) {
			$l = "#$1" if (! -r $cfile_sec);
			}
		}
	&flush_file_lines_as_domain_user($d, $cfile);
	}

# Return a URL for the user
local $rp = $opts->{'dir'};
$rp =~ s/^$d->{'home'}\///;
my $dbinfo = $upgrade ? "" : "<br>For filling database credentials use for host <tt>$dbhost</tt>, for name <tt>$dbname</tt>, for user <tt>$dbuser</tt> and for password use <tt>$dbpass</tt>.";
my $msg = $upgrade ? "Xoops upgrade complete. Go to <a href='$url' target='_blank'>$url</a> to manage it" : "Initial Xoops installation complete. Go to <a href='${url}install' target='_blank'>${url}install</a> to complete the installation process";
return (1, "$msg.$dbinfo", "Under $rp", $url);
}

# script_xoops_uninstall(&domain, version, &opts)
# Un-installs a Wordpress installation, by deleting the directory and database.
# Returns 1 on success and a message, or 0 on failure and an error
sub script_xoops_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);

if ($opts->{'db'}) {
	# Remove all xoops_ tables from the database, if we have one
	&cleanup_script_database($d, $opts->{'db'}, "(.*)");

	# Take out the DB
	if ($opts->{'newdb'}) {
		&delete_script_database($d, $opts->{'db'});
		}
	return (1, "Xoops directory and database deleted.");
	}
else {
	return (1, "Xoops directory deleted. Database tables must be removed manually.");
	}
}

# script_xoops_latest(version)
# Returns a URL and regular expression or callback func to get the version
sub script_xoops_latest
{
local ($ver) = @_;
return ( "https://github.com/XOOPS/XoopsCore25/releases",
	 "href.*\/tag\/v([\\d\\.]+-(Beta|RC)[\\d]+)|href.*\/tag\/v([\\d\\.]+)" );
}

sub script_xoops_site
{
return 'http://www.xoops.org/';
}

sub script_xoops_disabled
{
return 0;
}

1;

