Index: openacs-4/packages/static-pages/tcl/static-pages-procs.tcl
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/static-pages/tcl/static-pages-procs.tcl,v
diff -u -r1.11.2.3 -r1.11.2.4
--- openacs-4/packages/static-pages/tcl/static-pages-procs.tcl	12 Dec 2002 01:32:48 -0000	1.11.2.3
+++ openacs-4/packages/static-pages/tcl/static-pages-procs.tcl	12 Dec 2002 22:40:36 -0000	1.11.2.4
@@ -234,7 +234,6 @@
 	    set parent_folder_id $root_folder_id
 	    foreach directory $path {
 		append cumulative_path "$directory/"
-                ns_log Notice "atp:cumulative_path: '$cumulative_path', root_folder_id: '$root_folder_id'"
 		if (![info exists path_exists($cumulative_path)]) {
 		    # check db
 		    set folder_id [db_string get_folder_id {
@@ -442,6 +441,11 @@
 	end;
     }
 
+    # TODO: We should have a sp_deleted_item hook just like we do for
+    # new, old, and changd items.  As it is now, we delete the file
+    # out of the database but provide NO notification that we did so.
+    # --atp@piskorski.com, 2002/12/12 12:43 EST
+
     sp_sync_cr_with_filesystem_unlock $package_id
     set return_mesg "Done."
     return 0
@@ -574,14 +578,24 @@
 
 
 ad_proc -private sp_get_page_id { filename } {
-    Gets page_id
+    Returns a two item list of the page_id and the static-pages
+    package_id it belongs to.
+
     @author Dave Bauer (dave@thedesignexperience.org)
     @creation-date 2001-07-30
 } {
-    return [list [db_string search_page "
-    select static_page_id from static_pages sp, sp_folders spf 
-               where filename='$filename' and sp.folder_id=spf.folder_id
-               and package_id=[apm_package_id_from_key "static-pages"]" -default -1]]
+    # This package is no longer a singleton, so can't use
+    # apm_package_id_from_key here: --atp@piskorski.com, 2001/08/26
+    # 22:37 EDT
+
+    set package_key [sp_package_key_is]
+    if { [db_0or1row page_and_package_ids {}] } {
+        set results [list $static_page_id $package_id]
+    } else {
+        set results [list -1 -1]
+    }
+
+    return $results
 }
 
 ad_proc -public sp_flush_page { page_id } {
@@ -594,6 +608,18 @@
     util_memoize_flush [list sp_get_page_info_query $page_id]
 }
 
+
+ad_proc -public sp_package_key_is {} {
+   Simply returns the package key string for this package.
+   @author Andrew Piskorski (atp@piskorski.com)
+   @creation-date 2001/08/26
+} {
+   # TODO: Might want to have this pull and cache the actual key from
+   # the database.
+   return {static-pages}
+}
+
+
 ad_proc -public sp_serve_html_page { } {
     Registered proc to serve up static pages.
 
@@ -602,18 +628,26 @@
 } {
     set filename [ad_conn file]
     set sp_filename [sp_get_relative_file_path $filename]
-     
-    set page_id [util_memoize [list sp_get_page_id $sp_filename]]
 
-    set package_id [nsv_get static_pages package_id]
+    # In order to determine per-instance parameters like
+    # TemplatingEnabledP, need to know the package_id of the
+    # static-pages instance where this page is located, which is
+    # likely NOT the package_id returned by [ad_conn package_id]:
 
+    foreach [list page_id package_id] \
+        [util_memoize [list sp_get_page_id $sp_filename]] { break }
+
     set file [ad_conn file]
     ad_conn -set subsite_id [site_node_closest_ancestor_package "acs-subsite"]
 
     # If the page is in the db, serve it carefully; otherwise just dump it out.
     if { $page_id >= 0 } {
         set page_info [util_memoize [list sp_get_page_info_query $page_id]]
 
+        # TODO: Below, what if we only allow registered users to make
+        # comments?  Or some smaller group of users?  What then?
+        # --atp@piskorski.com, 2001/08/22 23:09 EDT
+
         # We only show the link here if the_public has 
         # general_comments_create privilege on the page.  Why the_public
         # rather than the current user?  Because we don't want admins to
@@ -671,13 +705,47 @@
     }
 }
 
+
 ad_proc -private sp_register_extension {} {
     Register the handler for each static page file extension.
 } {
-    foreach extension [split [string tolower [string trim [ad_parameter -package_id [apm_package_id_from_key static-pages] AllowedExtensions]]] " "] {
-	rp_register_extension_handler $extension sp_serve_html_page
-	rp_register_extension_handler [string toupper $extension] sp_serve_html_page
+    set proc_name {sp_register_extension}
+
+    set package_key [sp_package_key_is]
+    set package_ids [db_list all_static_pages_package_instances {
+        select package_id
+        from apm_packages
+        where package_key = :package_key
+    }]
+
+    # Generate unique list of all file-name extensions used by all
+    # package instances:
+
+    array set extensions_arr [list]
+    foreach package_id $package_ids {
+        foreach extension [split [string tolower [string trim [ad_parameter -package_id $package_id AllowedExtensions]]] " "] {
+            set extensions_arr($extension) {}
+        }
     }
+
+    foreach extension [array names extensions_arr] {
+        # TODO: Are we supposed to use the sp_serve_html_page proc for
+        # ALL file-name extensions, even if they're PDF or MS Word
+        # documents?  I think not!  Need a better way to map file-name
+        # extensions to proper static-pages extension handler procs.
+        # --atp@piskorski.com, 2002/12/11 22:55 EST
+
+        if { [regexp {htm} $extension] } {
+            set handler_proc {sp_serve_html_page}
+            rp_register_extension_handler $extension $handler_proc
+            rp_register_extension_handler [string toupper $extension] $handler_proc
+        } else {
+            ns_log Notice "$proc_name:  NOT registering any proc to handle files with extension '$extension'."
+
+            # TODO: Add a PDF or other extension handler?  Necessary
+            # only if you want to be able to make comments on non-HTML
+            # files, I think.
+            # --atp@piskorski.com, 2002/12/11 22:55 EST
+        }
+    }
 }
-# Register the handler for each static page file extension.
-sp_register_extension
Index: openacs-4/packages/static-pages/www/page-visit.tcl
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/static-pages/www/page-visit.tcl,v
diff -u -r1.4 -r1.4.2.1
--- openacs-4/packages/static-pages/www/page-visit.tcl	18 Sep 2002 12:03:33 -0000	1.4
+++ openacs-4/packages/static-pages/www/page-visit.tcl	12 Dec 2002 22:40:48 -0000	1.4.2.1
@@ -25,5 +25,64 @@
 #}
 
 
-ad_returnredirect $filename
+#ad_returnredirect $filename
 
+
+ad_proc -private sp_package_url {package_key} {
+   <p>Given a package key, return a URL of a mounted
+   package instance. If there is more than one instance
+   of the package mounted, the one with the lowest
+   <code>package_id</code> will be returned. If the
+   package is not instantiated or not mounted anywhere,
+   an error is raised. The proc is meant to be memoized.
+   </p>
+} {
+    set proc_name {sp_package_url}
+
+    set found_p [db_0or1row get_any_package_instance {
+        select min(package_id) as package_id
+        from apm_packages
+        where package_key = :package_key
+    }]
+
+    if { !$found_p } {
+        error "$proc_name: the '$package_key' package is not instantiated."
+    }
+
+    set found_p [db_0or1row get_mount_point {
+        select site_node.url(min(node_id)) as url
+        from site_nodes
+        where object_id = :package_id
+    }]
+
+    if { !$found_p } {
+        error "$proc_name: the '$package_key' package is not mounted."
+    }
+
+    return $url
+}
+
+
+# There are two possiblities: Either the static page is beneath the
+# site global www/ directory (and the filename starts "/www/"), or it
+# is beneath one of the package www directories (and the filename
+# starts "/packages/":
+
+if { [string first "/www/" $filename] == 0 } {
+    set redirect_to [string range $filename [string length "/www/"] end]
+} elseif { [regexp "^/packages/(\[^/\]+)" $filename match package_dir] } {
+   # TODO: We are assuming that the package directory name $package_dir
+   # is in fact always the package key.  Is this really true?
+
+   if { ! [regexp "^/packages/$package_dir/www/(.+)" $filename match url_part] } {
+      ad_return_error "Error in filename" "This page has an invalid filename: '$filename'."
+   }
+
+   set redirect_to "[sp_package_url $package_dir]$url_part"
+} else {
+   ad_return_error "Error in filename" "This page has an invalid filename: '$filename'."
+   return
+}
+
+ns_log Notice "atp:  redirect_to: '$redirect_to'  filename: '$redirect_to'"
+ad_returnredirect $redirect_to
Index: openacs-4/packages/static-pages/www/admin/fs-scan-progress.tcl
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/static-pages/www/admin/fs-scan-progress.tcl,v
diff -u -r1.2.2.2 -r1.2.2.3
--- openacs-4/packages/static-pages/www/admin/fs-scan-progress.tcl	12 Dec 2002 01:32:58 -0000	1.2.2.2
+++ openacs-4/packages/static-pages/www/admin/fs-scan-progress.tcl	12 Dec 2002 22:40:58 -0000	1.2.2.3
@@ -37,7 +37,6 @@
 
 set title "Filesystem search"
 set context_bar [ad_context_bar $title]
-#set context_bar [ad_admin_context_bar {index "Static Pages Admin"} $title]
 
 
 ReturnHeaders
@@ -49,12 +48,8 @@
 
 set package_id [ad_conn package_id]
 set root_folder_id [sp_root_folder_id $package_id]
+set fs_root "[acs_root_dir][ad_parameter -package_id $package_id {fs_root}]"
 
-# TODO: Add the fs_root parameter to the package:
-# --atp@piskorski.com, 2002/12/11 18:12 EST
-#set fs_root "[acs_root_dir][ad_parameter -package_id $package_id {fs_root}]"
-set fs_root "[acs_root_dir]/www"
-
 ns_write "
 <p>
 [sp_sync_cr_with_filesystem \
Index: openacs-4/packages/static-pages/www/admin/fs-scan.tcl
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/static-pages/www/admin/fs-scan.tcl,v
diff -u -r1.2.2.2 -r1.2.2.3
--- openacs-4/packages/static-pages/www/admin/fs-scan.tcl	12 Dec 2002 01:32:58 -0000	1.2.2.2
+++ openacs-4/packages/static-pages/www/admin/fs-scan.tcl	12 Dec 2002 22:40:58 -0000	1.2.2.3
@@ -39,11 +39,10 @@
 
 set title "Filesystem search"
 set context [list $title]
-#set context_bar [ad_admin_context_bar {index "Static Pages Admin"} $title]
 
 set package_id [ad_conn package_id]
 set root_folder_id [sp_root_folder_id $package_id]
-set fs_root "[acs_root_dir]/www"
+set fs_root "[acs_root_dir][ad_parameter -package_id $package_id {fs_root}]"
 
 set result [sp_sync_cr_with_filesystem \
 	-file_unchanged_proc sp_old_item \
Index: openacs-4/packages/static-pages/www/admin/index.adp
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/static-pages/www/admin/index.adp,v
diff -u -r1.3.2.1 -r1.3.2.2
--- openacs-4/packages/static-pages/www/admin/index.adp	24 Oct 2002 20:36:38 -0000	1.3.2.1
+++ openacs-4/packages/static-pages/www/admin/index.adp	12 Dec 2002 22:40:58 -0000	1.3.2.2
@@ -2,7 +2,8 @@
 <property name="title">@title@</property>
 <property name="context">@context@</property>
 
-<p>There @are@ @n_static_pages@ static @pages@ in the system.</p>
+<p>There @are@ @n_static_pages@ static @pages@ in the 
+tree rooted at <code>@fs_root@</code>.</p>
 
 <p>
 <a href="fs-scan-progress">Scan filesystem for static pages</a>
Index: openacs-4/packages/static-pages/www/admin/index.tcl
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/static-pages/www/admin/index.tcl,v
diff -u -r1.2 -r1.2.2.1
--- openacs-4/packages/static-pages/www/admin/index.tcl	6 Sep 2002 12:30:13 -0000	1.2
+++ openacs-4/packages/static-pages/www/admin/index.tcl	12 Dec 2002 22:40:58 -0000	1.2.2.1
@@ -34,5 +34,5 @@
 }
 
 set title "Static Pages Administration"
-
+set fs_root "[acs_root_dir][ad_parameter {fs_root}]"
 set context [list $title]