#! /bin/sh /usr/share/dpatch/dpatch-run
## 05_webpath.dpatch by Alexis Sukrieh <sukria@sukria.net>
##
## All lines beginning with `## DP:' are a description of the patch.
## DP: Update Config.pm to point to the right locations and support vhosts

@DPATCH@
diff -urNad trunk~/Bugzilla/Config.pm trunk/Bugzilla/Config.pm
--- trunk~/Bugzilla/Config.pm	2006-11-04 15:56:30.000000000 +0100
+++ trunk/Bugzilla/Config.pm	2006-11-15 07:56:22.000000000 +0100
@@ -51,9 +51,24 @@
 # graphs (since the path will be wrong in the HTML). This will be fixed at
 # some point.
 
-# constant paths
-our $libpath = '.';
-our $templatedir = "$libpath/template";
+# support for multi-site installation through X_BUGZILLA_SITE variable to
+# be passed by Apache.
+our $xsite = '';
+if (defined $ENV{X_BUGZILLA_SITE}) {
+    $xsite = "$ENV{X_BUGZILLA_SITE}";
+    # untaint (make sure it has no '/' and is overall something sane)
+    if ($xsite =~ m/^(\w[\w\-.]*)$/) {
+        $xsite ="$1";
+    } else {
+        die "invalid X_BUGZILLA_SITE: $xsite";
+    }
+}
+
+# constant default paths
+our $libpath = '/usr/share/bugzilla';
+our $pmpath = '/usr/share/perl5';
+our $templatedir = "/var/lib/bugzilla/template";
+our $params_file = "/etc/bugzilla/params";
 
 # variable paths
 our $project;
@@ -61,15 +76,28 @@
 our $datadir;
 if ($ENV{'PROJECT'} && $ENV{'PROJECT'} =~ /^(\w+)$/) {
     $project = $1;
-    $localconfig = "$libpath/localconfig.$project";
-    $datadir = "$libpath/data/$project";
-} else {
-    $localconfig = "$libpath/localconfig";
-    $datadir = "$libpath/data";
+    $localconfig = "/etc/bugzilla/localconfig.$project";
+    $datadir = "/var/lib/bugzilla/data/$project";
 }
+
+# If we have a $xsite (the Debian way), let's manage the conf paths correctly
+elsif ($xsite) {
+	$localconfig = "/etc/bugzilla/sites/${xsite}/localconfig";
+	$params_file = "/etc/bugzilla/sites/${xsite}/params";
+	$datadir     = "/var/lib/bugzilla/data-${xsite}";
+    $templatedir = (-d "/var/lib/bugzilla/template-${xsite}") ?  
+                    "/var/lib/bugzilla/template-${xsite}" : 
+                    "/var/lib/bugzilla/template";
+}
+
+# Default case
+else {
+    $localconfig = "/etc/bugzilla/localconfig";
+    $datadir = "/var/lib/bugzilla/data";
+}
+
 our $attachdir = "$datadir/attachments";
 our $webdotdir = "$datadir/webdot";
-
 our @parampanels = ();
 
 # Module stuff
@@ -93,12 +121,20 @@
 Exporter::export_ok_tags('admin', 'db', 'locations', 'params');
 
 # Bugzilla version
-$Bugzilla::Config::VERSION = "2.22.1";
+$Bugzilla::Config::VERSION = "2.22.1-debian2";
 
 use Safe;
 
 use vars qw(@param_list);
 
+# Debian -
+# For providing a simple way to install several virtual hosts of the same
+# package, we use also here an Apache environment variable for specifing
+# the webpath. This will give a nice way to customize several VirtualHosts.
+our $WEBPATH;
+$WEBPATH = $ENV{'X_BUGZILLA_WEBPATH'} if defined $ENV{'X_BUGZILLA_WEBPATH'};
+$WEBPATH = "/bugzilla/" if (not defined $WEBPATH);
+
 # Data::Dumper is required as needed, below. The problem is that then when
 # the code locally sets $Data::Dumper::Foo, this triggers 'used only once'
 # warnings.
@@ -115,25 +151,32 @@
 
 # XXX - mod_perl - need to register Apache init handler for params
 sub _load_datafiles {
+
+    # make sure localconfig exists
+    unless (-f $localconfig ) {
+	    die "$localconfig is not here";
+    }
+
     # read in localconfig variables
-    do $localconfig;
+    do $localconfig or 
+        die "Could not execute `$localconfig'! ; make sure permissions are ok.";
 
-    if (-e "$datadir/params") {
+    if (-e "$params_file") {
         # Handle reading old param files by munging the symbol table
         # Don't have to do this if we use safe mode, since its evaled
         # in a sandbox where $foo is in the same module as $::foo
         #local *::param = \%param;
 
-        # Note that checksetup.pl sets file permissions on '$datadir/params'
+        # Note that checksetup.pl sets file permissions on '$params_file'
 
         # Using Safe mode is _not_ a guarantee of safety if someone does
         # manage to write to the file. However, it won't hurt...
         # See bug 165144 for not needing to eval this at all
         my $s = new Safe;
 
-        $s->rdo("$datadir/params");
-        die "Error reading $datadir/params: $!" if $!;
-        die "Error evaluating $datadir/params: $@" if $@;
+        $s->rdo("$params_file");
+        die "Error reading $params_file: $!" if $!;
+        die "Error evaluating $params_file: $@" if $@;
 
         # Now read the param back out from the sandbox
         %param = %{$s->varglob('param')};
@@ -147,7 +190,7 @@
 my %params;
 
 # Load in the param definitions
-foreach my $item ((glob "$libpath/Bugzilla/Config/*.pm")) {
+foreach my $item ((glob "$pmpath/Bugzilla/Config/*.pm")) {
     $item =~ m#/([^/]+)\.pm$#;
     my $module = $1;
     next if ($module eq 'Common');
@@ -309,10 +352,21 @@
 
     close $fh;
 
-    rename $tmpname, "$datadir/params"
-      || die "Can't rename $tmpname to $datadir/params: $!";
+    # Debian - 
+    # We write it by hand
+    # this works really better than the original rename() call
+    # and will prevent overwriting of symlinks.
+    open TMPFILE, "<$tmpname" or die "$!";
+    open PARAMSFILE, ">$params_file" or die $!;
+    while (<TMPFILE>) {
+           print PARAMSFILE $_;
+    }
+    close TMPFILE;
+    close PARAMSFILE;
+    unlink $tmpname;
+ 
 
-    ChmodDataFile("$datadir/params", 0666);
+    ChmodDataFile("$params_file", 0666);
 }
 
 # Some files in the data directory must be world readable if and only if
diff -urNad trunk~/Bugzilla/Template.pm trunk/Bugzilla/Template.pm
--- trunk~/Bugzilla/Template.pm	2006-11-04 15:56:30.000000000 +0100
+++ trunk/Bugzilla/Template.pm	2006-11-15 07:55:51.000000000 +0100
@@ -567,6 +567,9 @@
                 require Bugzilla::BugMail;
                 Bugzilla::BugMail::Send($id, $mailrecipients);
             },
+            
+            # The webpath
+            'webpath' => $Bugzilla::Config::WEBPATH,
 
             # Bugzilla version
             # This could be made a ref, or even a CONSTANT with TT2.08
