# HG changeset patch
# User Jason Grout <jason.grout@drake.edu>
# Date 1303404927 18000
# Node ID ab40033c9b471f70ba960fbc28263dbbed0b905d
# Parent  e78167b9be9fec21405f9312ceabea98b223c41e
Update the sage-location script so that pkg-config files are initialized *once* to replace the build directory with ${SAGE_ROOT}, and then wrap pkg-config to define this variable.

This means that we no longer have to keep rewriting pkg-config files in local/lib/pkgconfig every time our location updates.

diff --git a/pkg-config b/pkg-config
new file mode 100755
--- /dev/null
+++ b/pkg-config
@@ -0,0 +1,5 @@
+#!/bin/sh
+
+# reset the system path to pick up the system pkg-config
+PATH=$SAGE_ORIG_PATH
+pkg-config --define-variable=SAGE_ROOT=${SAGE_ROOT} $*
diff --git a/sage-env b/sage-env
--- a/sage-env
+++ b/sage-env
@@ -118,6 +118,12 @@
 SAGE_LOCAL="$SAGE_ROOT/local"   && export SAGE_LOCAL
 SAGE_DATA="$SAGE_ROOT/data"     && export SAGE_DATA
 SAGE_DOC="$SAGE_ROOT/devel/sage/doc" && export SAGE_DOC
+
+if [ -z "${SAGE_ORIG__PATH_SET}" ]; then
+    SAGE_ORIG_PATH=$PATH && export SAGE_ORIG_PATH
+    SAGE_ORIG_PATH_SET=True && export SAGE_ORIG_PATH_SET
+fi
+
 PATH="$SAGE_ROOT:$SAGE_LOCAL/bin:$PATH" && export PATH
 
 # We offer a toolchain option, so if $SAGE_LOCAL/toolchain/toolchain-env exists source it.
diff --git a/sage-location b/sage-location
--- a/sage-location
+++ b/sage-location
@@ -160,8 +160,13 @@
 
 def initialize_pkgconfig_files():
     """
-    Insert a sage_local variable in each pkg_config file and replace
-    them to make the paths portable.
+    Replace all instances of the path SAGE_ROOT with the literal
+    ${SAGE_ROOT} in pkg-config files.  When pkg-config is run,
+    it should be run as::
+
+        pkg-config --define-variable=SAGE_ROOT=$SAGE_ROOT 
+    
+    in order to properly define this variable.
     """
     LIB = os.path.join(os.path.abspath(SAGE_ROOT), 'local', 'lib')
     PKG = os.path.join(LIB,'pkgconfig')
@@ -170,36 +175,10 @@
         if os.path.splitext(filename)[1]==".pc":
             with open(filename) as file:
                 config = file.read()
-                
             new_config = config.replace(os.path.abspath(SAGE_ROOT), "${SAGE_ROOT}")
-
-            new_config = 'SAGE_ROOT=%s\n'%os.path.abspath(SAGE_ROOT)+new_config
-
             with open(filename, 'w') as file:
                 file.write(new_config)
 
-    
-
-def update_pkgconfig_files():
-    """
-    Change paths in package configuration files.
-    """
-    LIB = os.path.join(os.path.abspath(SAGE_ROOT), 'local', 'lib')
-    PKG = os.path.join(LIB,'pkgconfig')
-    for name in os.listdir(PKG):
-        filename=os.path.join(PKG,name)
-        if os.path.splitext(filename)[1]==".pc":
-            with open(filename) as file:
-                config = file.read()
-                
-            prefix_start=config.find('SAGE_ROOT=')
-            prefix_end=config.find('\n', prefix_start)
-            new_prefix='SAGE_ROOT=%s'%os.path.abspath(SAGE_ROOT)
-            new_config=config[:prefix_start]+new_prefix+config[prefix_end:]
-
-            with open(filename, 'w') as file:
-                file.write(new_config)
-    
 
 def remove_files(path, remove_extensions):
     """
@@ -230,7 +209,6 @@
         print "(please wait at most a few minutes)..."
         print "Do not interrupt this."
 	update_library_files()
-        update_pkgconfig_files()
         # Compiled python files need to be regenerated, so we remove them
         remove_files(os.path.join(SAGE_ROOT, 'local', 'lib', 'python'), 
                      remove_extensions=('.pyc', '.pyo'))
