# Copyright (c) 2007, Jannis Leidel
# All rights reserved.

# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
 
# Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
# Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# Neither the name of the <ORGANIZATION> nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.

# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

sub script_django_desc
{
return "Django";
}

sub script_django_uses
{
return ("python");
}

sub script_django_longdesc
{
return "A high-level Python Web framework that encourages rapid development and clean, pragmatic design.";
}

sub script_django_versions
{
return ( "trunk", "0.96.1", "0.95.2", "0.91.1", "0.90" );
}

sub  script_django_version_desc
{
local ($ver) = @_;
if ($ver == "trunk") {
    return "$ver (Development)";
}
return &compare_versions($ver, "0.95.2") > 0 ? "$ver (Stable)" : "$ver (Old)";
}

sub script_django_category
{
return "Development";
}

sub script_django_depends
{
local ($d, $ver) = @_;
&has_command("python") || return "The python command is not installed";
if ($ver == "trunk") {
    &has_command("svn") || return "The svn command is not installed";
}
&require_apache();
return undef;
}

sub script_django_params
{
local ($d, $ver, $upgrade) = @_;
local $rv;
#local $hdir = &public_html_dir($d, 1);
local $hdir = $d->{'home'}."/lib";
if ($upgrade) {
    # Options are fixed when upgrading
    local $dir = $upgrade->{'opts'}->{'dir'};
    $dir =~ s/^$d->{'home'}\///;
    $rv .= &ui_table_row("Install directory", $dir);
    }
else {
    local $dir = "django";
    $rv .= &ui_table_row("Install directory", $hdir."/".$dir);
    $rv .= &ui_hidden("dir", $dir);
    }
return $rv;
}

sub script_django_parse
{
local ($d, $ver, $in, $upgrade) = @_;
if ($upgrade) {
    # Options are always the same
    return $upgrade->{'opts'};
    }
else {
    local $hdir = $d->{'home'}."/lib";
    $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'}",
           };
    }
}

sub script_django_check
{
local ($d, $ver, $opts, $upgrade) = @_;
$opts->{'dir'} =~ /^\// || return "Missing or invalid install directory";
if (-r "$opts->{'dir'}/__init__.py") {
    return "Django appears to be already installed in the selected directory";
    }
return undef;
}

sub script_django_files
{
local ($d, $ver, $opts, $upgrade) = @_;
if ($ver == "trunk") {
    return ( );
}
local @files = ( { 'name' => "source",
           'file' => "Django-$ver.tar.gz",
           'url' => "http://www.djangoproject.com/download/$ver/tarball/",
           'nocache' => 1 } );
return @files;
}

sub script_django_commands
{
return ("tar", "gunzip", "svn");
}

sub script_django_install
{
local ($d, $ver, $opts, $files, $upgrade) = @_;
local ($out, $ex);
# Create target dir
if (!-d $opts->{'dir'}) {
        $out = &run_as_domain_user($d, "mkdir -p ".quotemeta($opts->{'dir'}));
        -d $opts->{'dir'} ||
                return (0, "Failed to create directory : <tt>$out</tt>.");
        }
local $temp = &transname();
if ($ver == "trunk") {
    $svnroot = "http://code.djangoproject.com/svn/django/trunk/django/";
    local $err = &run_as_domain_user($d, "svn co $svnroot ".quotemeta($opts->{'dir'}));
} else {
    local $err = &extract_script_archive($files->{'source'}, $temp, $d,
                    $opts->{'dir'}, "Django-$ver/django");
}
$err && return (0, "Failed to extract source : $err");
local $initfile = "$opts->{'dir'}/__init__.py";
-r $initfile || return (0, "Failed to copy source : <tt>$out</tt>.");

local $url = &script_path_url($d, $opts);
local $rp = $opts->{'dir'};
$rp =~ s/^$d->{'home'}\///;
return (1, "Initial Django ($ver) installation complete. Django is a development framework, so it doesn't do anything by itself!", "Under $rp", $url);
}

sub script_django_uninstall
{
local ($d, $ver, $opts) = @_;
local $derr = &delete_script_install_directory($d, $opts);
return (0, $derr) if ($derr);
if ($ver == "trunk") {
    if (-d "$opts->{'dir'}/.svn") {
        local $out = &backquote_logged("rm -rf ".quotemeta($opts->{'dir'})."/ 2>&1");
    }    
}
return (0, "Failed to delete files : <tt>$out</tt>") if ($out);
return (1, "Django ($ver) directory deleted.");
}

sub script_django_latest
{
local ($ver) = @_;
if ($ver != "trunk") {
    return ( "http://www.djangoproject.com/download/",
        "Django-([0-9\\.]+?)\\.tar\\.gz" );
} else {
    return (undef, undef);
}
}

1;