@zikula_tables = (
   	"admin_category",
	"admin_module",
	"blocks",
	"block_placements",
	"block_positions",
	"bundles",
	"categories_attributes",
	"categories_category",
	"categories_registry",
	"extensions",
	"extension_deps",
	"groups",
	"group_applications",
	"group_membership",
	"group_perms",
	"hook_binding",
	"hook_runtime",
	"menu_items",
	"module_vars",
	"sc_intrusion",
	"search_result",
	"search_stat",
	"session_info",
	"users",
	"users_attributes",
	"users_verifychg",
	"zauth_authentication_mapping",
	"zikula_routes_route",
    );

# script_zikula_desc()
sub script_zikula_desc
{
return "Zikula";
}

sub script_zikula_uses
{
return ( "php" );
}

sub script_zikula_longdesc
{
return "Zikula allows you to build simple one-page websites to individual web applications utilising different types of extensions for making your project to something special";
}

# script_zikula_versions()
sub script_zikula_versions
{
return ( "3.1.0" );
}

sub script_zikula_category
{
return "CMS";
}

sub script_zikula_php_vers
{
local ($d, $ver) = @_;
return ( 5 );
}

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

sub script_zikula_php_vars
{
return ( 
        [ 'memory_limit', '512M', '+' ] 
       );
}

sub script_zikula_perl_modules
{
return ( "Digest::MD5" );
}

sub script_zikula_dbs
{
return ("mysql", "postgres");
}

sub script_zikula_php_fullver
{
local ($d, $ver, $sinfo) = @_;
return "7.2.5";
}

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

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

# script_zikula_files(&domain, version, &opts, &upgrade-info)
# Returns a list of files needed by Zikula, each of which is a hash ref
# containing a name, filename and URL
sub script_zikula_files
{
local ($d, $ver, $opts, $upgrade) = @_;
my $shortver = $ver;
$shortver =~ s/\.//g;
local @files = ( { 'name' => "source",
	   'file' => "zikula-$ver.zip",
	   'method' => "GET",
	   'url' => "https://github.com/zikula/core/releases/download/$ver/zikula.zip" } );
return @files;
}

sub script_zikula_commands
{
return ("unzip");
}

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

# Get DB details
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 = $dbtype eq "mysql" ? &mysql_user($d) : &postgres_user($d);
local $dbpass = $dbtype eq "mysql" ? &mysql_pass($d) : &postgres_pass($d, 1);
local $dbphptype = $dbtype eq "mysql" ? "mysql" : "psql";
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 $oldcfile = &transname();
local $oldcfile2 = &transname();
local $oldcfile3 = &transname();
local $oldhfile = &transname();
local $cfile = "$opts->{'dir'}/.env.local";
local $cfile2 = "$opts->{'dir'}/config/services_custom.yaml";
local $cfile3 = "$opts->{'dir'}/src/extensions";
local $hfile = "$opts->{'dir'}/public/.htaccess";

# Preserve user data
&copy_source_dest($cfile, $oldcfile) if(-r $cfile);
&copy_source_dest($cfile2, $oldcfile2) if (-r $cfile2);
&copy_source_dest($cfile3, $oldcfile3) if (-r $cfile3);
&copy_source_dest($hfile, $oldhfile) if (-r $hfile);

local $err = &extract_script_archive($files->{'source'}, $temp, $d,
				     $opts->{'dir'}, "zikula");
$err && return (0, "Failed to extract source : $err");

# Don't preserve bundled extension
local $oldcfile4 = &transname();
local $cfile4 = "$opts->{'dir'}/src/extensions/zikula";
&copy_source_dest($cfile4, $oldcfile4) if (-r $cfile4);

local $url = &script_path_url($d, $opts);
if ($upgrade) {
	# Put user data back
	&copy_source_dest_as_domain_user($d, $oldcfile, $cfile);
	&copy_source_dest_as_domain_user($d, $oldcfile2, $cfile2);
	&copy_source_dest_as_domain_user($d, $oldcfile3, $cfile3);
	&copy_source_dest_as_domain_user($d, $oldcfile4, $cfile4);
	&copy_source_dest_as_domain_user($d, $oldhfile, $hfile);
	}
else {
	# Update the config file
	my %envlocal;
	$envlocal{'DATABASE_USER'} = $dbuser;
	$envlocal{'DATABASE_PWD'} = $dbpass;
	$envlocal{'DATABASE_NAME'} = $dbname;
	$envlocal{'DATABASE_URL'} = "$dbtype://$envlocal{'DATABASE_USER'}:$envlocal{'DATABASE_PWD'}\@$dbhost/$envlocal{'DATABASE_NAME'}";
	write_file("$opts->{'dir'}/.env.local", \%envlocal);

	# Fix .htaccess
	if (-r $hfile) {
		my $hfile_data = read_file_contents($hfile);
		$hfile_data =~ s/(.*FollowSymlinks.*)/#$1/g;
		write_file_contents($hfile, $hfile_data);
		}
	# Set cache dir permissions
	&set_permissions_as_domain_user($d, 0770, 
                   ("$opts->{'dir'}/var/cache",
                   	"$opts->{'dir'}/var/log"));
	}

# Fix the .htaccess file if needed, since mod_php directives may not work
local $p = &domain_has_website($d);
if ($p eq 'web' &&
    !$apache::httpd_modules{'mod_php4'} &&
    !$apache::httpd_modules{'mod_php5'}) {
	my $htfile = "$opts->{'dir'}/.htaccess";
	my $lref = &read_file_lines_as_domain_user($d, $htfile);
	foreach my $l (@$lref) {
		if ($l =~ /^\s*php_/) {
			$l = "# $l";
			}
		}
	&flush_file_lines_as_domain_user($d, $htfile);
	}

# Return a URL for the user
local $rp = $opts->{'dir'};
$rp =~ s/^$d->{'home'}\///;
my $type2 = $upgrade ? 'upgrade' : 'installation';
my $type = $upgrade ? "Zikula $type2 complete" : "Initial Zikula $type2 complete";
return (1, "$type. Go to <a href=${url}public target=_blank>${url}public</a> to complete the $type2 process.",
	"Under $rp using $dbphptype database $dbname", "${url}public");
}

# script_zikula_uninstall(&domain, version, &opts)
# Un-installs a Zikula installation, by deleting the directory and database.
# Returns 1 on success and a message, or 0 on failure and an error
sub script_zikula_uninstall
{
local ($d, $version, $opts) = @_;

# Remove zk_ tables from the database
if (&compare_versions($version, "3.0.0") >= 0) {
	&cleanup_script_database($d, $opts->{'db'}, \@zikula_tables);
	}

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

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

return (1, "Zikula directory and tables deleted.");
}

# script_wordpress_latest(version)
# Returns a URL and regular expression or callback func to get the version
sub script_zikula_latest
{
my ($ver) = @_;
return ( "https://ziku.la/en/downloads/",
	 "Download\\s+Zikula.*?(3\\.[0-9\\.]+)" );
}

sub script_zikula_site
{
return 'https://ziku.la/';
}

sub script_zikula_passmode
{
return 0;
}

1;

