
# script_grav_desc()
sub script_grav_desc
{
return "Grav";
}

sub script_grav_uses
{
return ( "php" );
}

sub script_grav_longdesc
{
return "Grav is a free software, self-hosted content management system";
}

sub script_grav_category
{
return "CMS";
}

# script_grav_versions()
sub script_grav_versions
{
return ( "1.7.38" );
}

sub script_grav_release
{
return 4;	# Fix PHP required version
}

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

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

sub script_grav_php_modules
{
return ( "mbstring", "dom", "xml", "curl", "zip", "gd" );
}

# script_grav_params(&domain, version, &upgrade-info)
# Returns HTML for table rows for options for installing PHP-NUKE
sub script_grav_params
{
local ($d, $ver, $upgrade) = @_;
local $rv;
local $hdir = &public_html_dir($d, 1);
if ($upgrade) {
	# Options are fixed when upgrading
	local $dir = $upgrade->{'opts'}->{'dir'};
	$dir =~ s/^$d->{'home'}\///;
	$rv .= &ui_table_row("Install directory", $dir);
	}
else {
	# Show editable install options
	$rv .= &ui_table_row("Install sub-directory under <tt>$hdir</tt>",
		     &ui_opt_textbox("dir", &substitute_scriptname_template("grav", $d), 30, "At top level"));
	}
return $rv;
}

# script_grav_parse(&domain, version, &in, &upgrade-info)
# Returns either a hash ref of parsed options, or an error string
sub script_grav_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'}";
	return { 'dir' => $dir,
		 'path' => "/$in{'dir'}", };
	}
}

# script_grav_check(&domain, version, &opts, &upgrade-info)
# Returns an error message if a required option is missing or invalid
sub script_grav_check
{
local ($d, $ver, $opts, $upgrade) = @_;
$opts->{'dir'} =~ /^\// || return "Missing or invalid install directory";
if (-r "$opts->{'dir'}/now.json") {
	return "Grav appears to be already installed in the selected directory";
	}
return undef;
}

# script_grav_files(&domain, version, &opts, &upgrade-info)
# Returns a list of files needed by Twiki, each of which is a hash ref
# containing a name, filename and URL
sub script_grav_files
{
local ($d, $ver, $opts, $upgrade) = @_;
local @files = ( { 'name' => "source",
	   'file' => "grav-admin-v${ver}.zip",
	   'url' => "https://getgrav.org/download/core/grav-admin/$ver" } );
return @files;
}

sub script_grav_commands
{
return ("unzip");
}

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

# Extract tar file to temp dir and copy to target
local $temp = &transname();
local $skip = $upgrade ? [ "user" ] : undef;
local $err = &extract_script_archive($files->{'source'}, $temp, $d,
                                     $opts->{'dir'}, "grav-admin",
                                     0, 0, $skip);
$err && return (0, "Failed to extract source : $err");

local $url = &script_path_url($d, $opts);

local $rp = $opts->{'dir'};
$rp =~ s/^$d->{'home'}\///;
return (1, "Initial Grav installation complete. Go to <a href='$url' target=_blank>$url</a> to complete the installation process", "Under $rp", $url);
}

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

return (1, "Grav directory deleted.");
}

# script_grav_latest(version)
# Returns a URL and regular expression or callback func to get the version
sub script_grav_latest
{
local ($ver) = @_;
return ( "https://getgrav.org/downloads",
	 "Current Version: <strong>([0-9\\.]+)<" );
}

sub script_grav_site
{
return "https://getgrav.org/";
}

1;

