
# script_rainloop_desc()
sub script_rainloop_desc
{
return "RainLoop";
}

sub script_rainloop_uses
{
return ( "php" );
}

sub script_rainloop_longdesc
{
return "RainLoop is a simple, modern and fast web-based email client";
}

# script_rainloop_versions()
sub script_rainloop_versions
{
return ( "1.17.0" );
}

sub script_rainloop_category
{
return "Email";
}

sub script_rainloop_php_modules
{
return ( "mysql", "iconv", "curl", "json", "xml", "dom" );
}

sub script_rainloop_php_optional_modules
{
return ( "openssl" );
}

sub script_rainloop_php_vers
{
return ( 5 );
}

sub script_rainloop_release
{
return 2;	# Fix download URL
}

# script_rainloop_params(&domain, version, &upgrade-info)
# Returns HTML for table rows for options for installing PHP-NUKE
sub script_rainloop_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("rainloop", $d), 30, "At top level"));
	}
return $rv;
}

# script_rainloop_parse(&domain, version, &in, &upgrade-info)
# Returns either a hash ref of parsed options, or an error string
sub script_rainloop_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_def'} ? "/" : "/$in{'dir'}", };
	}
}

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

# script_rainloop_files(&domain, version, &opts, &upgrade-info)
# Returns a list of files needed by RainLoop, each of which is a hash ref
# containing a name, filename and URL
sub script_rainloop_files
{
local ($d, $ver, $opts, $upgrade) = @_;
local @files = ( { 'name' => "source",
	           'file' => "rainloop-$ver.tar.gz",
	           'url' => "https://www.rainloop.net/repository/webmail/rainloop-latest.zip" },
	    );
return @files;
}

sub script_rainloop_commands
{
return ("zip");
}

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

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

# Protect data directory
if (!$upgrade) {
	&open_tempfile_as_domain_user($d, HTACCESS, ">$opts->{'dir'}/data/.htaccess");
	&print_tempfile(HTACCESS, "Require all denied\n");
	&close_tempfile_as_domain_user($d, HTACCESS);
	}

# Return a URL for the user
local $url = &script_path_url($d, $opts);
local $adminurl = $url."?admin";
local $rp = $opts->{'dir'};
$rp =~ s/^$d->{'home'}\///;
return (1, "Initial RainLoop installation complete. Go to <a target='_blank' href='$adminurl'>$adminurl</a> to perform initial setup. The initial administration login is <tt>admin</tt> and the password is <tt>12345</tt>.", "Under $rp");
}

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

# script_rainloop_latest(version)
# Returns a URL and regular expression or callback func to get the version
sub script_rainloop_latest
{
local ($ver) = @_;
return ( "http://www.rainloop.net/downloads/",
	 ">v([0-9\\.]+)<" );
}

sub script_rainloop_site
{
return 'http://www.rainloop.net/';
}

1;

