Subject: [bug#66262] [PATCH v4 3/6] gnu: openfoam-org: Improve internal path handling.

From: reza <[email-masked]>
Date: Wed, 29 Nov 2023 14:29:09 +0000

* gnu/packages/simulation.scm (openfoam-org): Improve handling of internal
path WM_PROJECT_DIR.

Change-Id: I43de76fb009f955294686a0b45bf3dc0217e7dc7
---
 gnu/packages/simulation.scm | 57 ++++++++++++++++++++++++++++---------
 1 file changed, 43 insertions(+), 14 deletions(-)

diff --git a/gnu/packages/simulation.scm b/gnu/packages/simulation.scm
index 73e53fe35f..75eb070f64 100644
--- a/gnu/packages/simulation.scm
+++ b/gnu/packages/simulation.scm
@@ -197,8 +197,10 @@ (define-public openfoam-org
       ;; Executable files and shared libraries are located in the 'platforms'
       ;; subdirectory.
       #:strip-directories
-      #~(list "share/OpenFOAM/platforms/linux64GccDPInt32Opt/bin"
-              "share/OpenFOAM/platforms/linux64GccDPInt32Opt/lib")
+      #~(list (string-append "OpenFOAM-" #$(package-version this-package)
+                             "/platforms/linux64GccDPInt32Opt/bin")
+	      (string-append "OpenFOAM-" #$(package-version this-package)
+                             "/platforms/linux64GccDPInt32Opt/lib"))
 
       #:modules
       '((ice-9 ftw)
@@ -278,7 +280,9 @@ (define-public openfoam-org
                                                       "/lib,")) "" libraries))
                         (openfoam-lib
                          (string-append #$output
-                                        "/share/OpenFOAM/platforms/linux64GccDPInt32Opt/lib"))
+                                        "/share/OpenFOAM-"
+                                        #$(package-version this-package)
+                                        "/platforms/linux64GccDPInt32Opt/lib"))
                         (ldflags
                          (string-append "-Wl,"
                                         rpaths
@@ -338,19 +342,32 @@ (define-public openfoam-org
                 ;; (with-directory-excursion "tutorials"
                 ;; (invoke "bash" "-c" "source ../etc/bashrc && ./Alltest"))
                 ) #t))
+          (add-before 'install 'set-paths
+            (lambda _
+              (let ((install-path (string-append #$output
+				                 "/share/OpenFOAM-"
+                                                 #$(package-version this-package))))
+                (substitute* "etc/bashrc"
+	          (("^\\[ \"\\$BASH\".*$") "")
+	          (("^export FOAM_INST_DIR=\\$\\(cd.*$")
+	           (string-append "export FOAM_INST_DIR=" install-path "\n"))
+	          (("^export FOAM_INST_DIR=\\$HOME.*$") ""))) #t))
           (replace 'install
             (lambda _
-              (let ((install-dir (string-append #$output
-                                                "/share/OpenFOAM")))
-                (mkdir-p install-dir) ;create install directory
+              (let ((install-path (string-append #$output
+                                                 "/share/OpenFOAM-"
+                                                 #$(package-version this-package))))
+                (mkdir-p install-path) ;create install directory
                 ;; move contents of build directory to install directory
-                (copy-recursively "." install-dir))))
+                (copy-recursively "." install-path))))
           (add-after 'install 'add-symbolic-link
             (lambda _
               (let* ((bin (string-append #$output "/bin"))
                      (lib (string-append #$output "/lib"))
+                     (etc (string-append #$output "/etc"))
                      (openfoam (string-append #$output
-                                              "/share/OpenFOAM"))
+                                              "/share/OpenFOAM-"
+                                              #$(package-version this-package)))
                      (build-bin (string-append openfoam
                                                "/platforms/linux64GccDPInt32Opt/bin"))
                      (build-lib (string-append openfoam
@@ -373,12 +390,24 @@ (define-public openfoam-org
                                        (string-append bin "/" file))))
                           (scandir foam-bin))
                 ;; add symbolic link for standard 'lib' directory
-                (symlink build-lib lib)) #t)))))
-    (native-search-paths
-     (list (search-path-specification
-            (variable "WM_PROJECT_DIR")
-            (separator #f)
-            (files '("share/OpenFOAM")))))
+                (symlink build-lib lib)
+                ;; add symbolic link for bashrc file
+                (mkdir-p etc)
+                (symlink (string-append openfoam "/etc/bashrc")
+                         (string-append etc "/bashrc"))) #t))
+          (add-after 'add-symbolic-link 'wrap
+            (lambda* (#:key python inputs outputs #:allow-other-keys)
+              (let* ((bin (string-append #$output "/bin"))
+                     (openfoam (string-append #$output
+                                              "/share/OpenFOAM-"
+                                              #$(package-version this-package))))
+                (for-each
+                 (lambda (program)
+                   (unless (member program
+                                   '("." ".."))
+                     (wrap-program (string-append bin "/" program)
+                       `("WM_PROJECT_DIR" ":" prefix (,openfoam)))))
+                 (scandir bin))))))))
     ;; Note:
     ;; Tutorial files are installed read-only in /gnu/store.
     ;; To allow write permissions on files copied from the store a
-- 
2.41.0