
# script_forwards_desc()
sub script_forwards_desc
{
return "Forwards";
}

sub script_forwards_uses
{
return ( "php", "horde" );
}

sub script_forwards_longdesc
{
return "Forwards is a Horde module for setting user e-mail forwards with support for several popular mailers";
}

# script_forwards_versions()
sub script_forwards_versions
{
return ( "h3-3.2.1" );
}

sub script_forwards_category
{
return "Horde";
}

# script_forwards_depends(&domain, version)
sub script_forwards_depends
{
local ($d, $ver) = @_;

# Check for horde install
local @scripts = &list_domain_scripts($d);
local ($horde) = grep { $_->{'name'} eq 'horde' } @scripts;
return ("Forwards requires that Horde be installed first") if (!$horde);

return ( );
}

# script_forwards_params(&domain, version, &upgrade-info)
# Returns HTML for table rows for options for installing PHP-NUKE
sub script_forwards_params
{
local ($d, $ver, $upgrade) = @_;
local $rv;
local @scripts = &list_domain_scripts($d);
local ($horde) = grep { $_->{'name'} eq 'horde' } @scripts;
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
        local $odir = $horde->{'opts'}->{'dir'};
        $odir =~ s/^\Q$d->{'home'}\E\///;
	$rv .= &ui_table_row("Install sub-directory under <tt>$odir</tt>",
			     &ui_textbox("dir", "forwards", 30));
	}
return $rv;
}

# script_forwards_parse(&domain, version, &in, &upgrade-info)
# Returns either a hash ref of parsed options, or an error string
sub script_forwards_parse
{
local ($d, $ver, $in, $upgrade) = @_;
if ($upgrade) {
	# Options are always the same
	return $upgrade->{'opts'};
	}
else {
	local @scripts = &list_domain_scripts($d);
	local ($horde) = grep { $_->{'name'} eq 'horde' } @scripts;
	$in{'dir'} =~ /\S/ && $in{'dir'} !~ /\.\./ ||
		return "Missing or invalid installation directory";
	local $dir = "$horde->{'opts'}->{'dir'}/$in{'dir'}";
	$dir =~ s/\/\//\//g;
        local $path = "$horde->{'opts'}->{'path'}/$in{'dir'}";
        $path =~ s/\/\//\//g;
	return { 'dir' => $dir,
		 'path' => $path,
		 'db' => $horde->{'opts'}->{'db'},
		 'horde' => 1 };
	}
}

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

# script_forwards_files(&domain, version, &opts, &upgrade-info)
# Returns a list of files needed by Forwards, each of which is a hash ref
# containing a name, filename and URL
sub script_forwards_files
{
local ($d, $ver, $opts, $upgrade) = @_;
local @files = ( { 'name' => "source",
	   'file' => "forwards-$ver.tar.gz",
	   'nocheck' => 1,
	   'url' => "ftp://ftp.horde.org/pub/forwards/forwards-$ver.tar.gz" } );
return @files;
}

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

# script_forwards_install(&domain, version, &opts, &files, &upgrade-info)
# Actually installs Forwards, and returns either 1 and an informational
# message, or 0 and an error
sub script_forwards_install
{
local ($d, $version, $opts, $files, $upgrade) = @_;
local ($out, $ex);
local @scripts = &list_domain_scripts($d);
local ($horde) = grep { $_->{'name'} eq 'horde' } @scripts;

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

# Copy the config files
local $cdir = "$opts->{'dir'}/config";
opendir(DIR, $cdir);
foreach my $f (readdir(DIR)) {
	if ($f =~ /^(.*)\.dist$/ && !-r "$cdir/$1") {
		&run_as_domain_user($d, "cp ".quotemeta("$cdir/$f")." ".
					      quotemeta("$cdir/$1"));
		&make_file_php_writable($d, "$cdir/$1");
		}
	}
closedir(DIR);
&make_file_php_writable($d, $cdir, 1);

# Return a URL for the user
local $url = &script_path_url($d, $opts);
local $rp = $opts->{'dir'};
$rp =~ s/^$d->{'home'}\///;
return (1, "Forwards installation complete. It can be accessed at <a target=_blank href='$url'>$url</a>. However, it must first be configured using Horde at <a target=_blank href='$horde->{'url'}'>$horde->{'url'}</a>, on the Setup page under Administration.", "Under $rp", $url);
}

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

# script_forwards_latest()
# Returns a URL and regular expression or callback func to get the version
sub script_forwards_latest
{
return ( "http://ftp.horde.org/pub/forwards/",
         "forwards-(h3-[0-9\\.]+)\\.tar\\.gz" );
}

sub script_forwards_site
{
return 'http://www.horde.org/forwards/';
}

sub script_forwards_disabled
{
return 2;
}

1;

