From: Kentaro Hayashi <kenhys@xdump.org>
Date: Sat, 10 Aug 2024 22:13:30 +0900
Subject: Tweak GRDIR for debian package

There is a assumption that gr-framework is installed under
prefix/gr/{bin,lib,fonts} etc and GRDIR use it.

This assumption will not fit for Debian because binary should be
installed at /usr/bin, library should be installed at
/usr/lib/x86_64-linux-gnu/, and fonts should be installed at
/usr/share/gr/fonts so on.

GRDIR vary on the context, we don't use GRDIR, instead use
specific fallback path by default.

ref. https://gr-framework.org/environment_variables.html

Author: Kentaro HAYASHI <kenhys@xdump.org>
Subject: Tweak GRDIR for debian package
Forwarded: not-needed
Signed-off-by: Kentaro Hayashi <kenhys@xdump.org>
---
 CMakeLists.txt            |  2 ++
 lib/gks/font.c            |  5 +++++
 lib/gks/ft.c              |  5 +++++
 lib/gks/plugin.c          |  5 +++++
 lib/gks/socket.c          |  5 +++++
 lib/gks/util.c            |  6 ++++++
 lib/gr/mathtex2_kerning.c |  5 +++++
 lib/gr/stream.c           |  6 ++++++
 lib/gr3/gr3_glx.c         | 10 ++++++++++
 9 files changed, 49 insertions(+)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index c3adc93..bb3b5c6 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -315,6 +315,7 @@ foreach(LIBRARY gks_static gks_shared)
                       $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
   )
   target_compile_definitions(${LIBRARY} PRIVATE GRDIR="${GR_DIRECTORY}")
+  target_compile_definitions(${LIBRARY} PRIVATE GRLIB="${CMAKE_INSTALL_LIBDIR}")
   set_target_properties(
     ${LIBRARY}
     PROPERTIES C_STANDARD 90
@@ -457,6 +458,7 @@ foreach(LIBRARY gr3_static gr3_shared)
     target_compile_definitions(${LIBRARY} PRIVATE _CRT_SECURE_NO_WARNINGS)
   endif()
   target_compile_definitions(${LIBRARY} PRIVATE GRDIR="${GR_DIRECTORY}")
+  target_compile_definitions(${LIBRARY} PRIVATE GRLIB="${CMAKE_INSTALL_LIBDIR}")
   if(NOT ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC"))
     target_compile_options(${LIBRARY} ${GR3_LINK_MODE} -pthread)
   endif()
diff --git a/lib/gks/font.c b/lib/gks/font.c
index dd64f9f..c02a266 100644
--- a/lib/gks/font.c
+++ b/lib/gks/font.c
@@ -37,8 +37,13 @@ int gks_open_font(void)
   path = gks_getenv("GKS_FONTPATH");
   if (path == NULL)
     {
+#if 1
+      /* Force debian specific fallback path - /usr/share/gr/fonts/gksfont.dat */
+      path = "/usr/share/gr";
+#else
       path = gks_getenv("GRDIR");
       if (path == NULL) path = GRDIR;
+#endif
     }
   strcpy(fontdb, (char *)path);
   strcat(fontdb, "/fonts/gksfont.dat");
diff --git a/lib/gks/ft.c b/lib/gks/ft.c
index 5f08162..6d7cf18 100644
--- a/lib/gks/ft.c
+++ b/lib/gks/ft.c
@@ -734,7 +734,12 @@ static ft_path_char_t *gks_ft_get_font_path(const char *font_name, const char *f
   prefix = gks_getenv("GKS_FONTPATH");
   if (prefix == NULL)
     {
+#if 1
+      /* Force debian specific falllback /usr/share/gr/fonts/... */
+      prefix = "/usr/share/gr";
+#else
       prefix = gks_getenv("GRDIR");
+#endif
     }
   if (prefix == NULL)
     {
diff --git a/lib/gks/plugin.c b/lib/gks/plugin.c
index abc92ee..4b58d5c 100644
--- a/lib/gks/plugin.c
+++ b/lib/gks/plugin.c
@@ -67,9 +67,14 @@ static void *load_library(const char *name)
   handle = dlopen(pathname, RTLD_LAZY);
   if (handle == NULL)
     {
+#if 1
+      /* Force debian specific fallback path - /usr/lib/$(DEB_HOST_MULTIARCH)/gr/ */
+      snprintf(pathname, MAXPATHLEN, "%s/gr/%s.%s", GRLIB, name, EXTENSION);
+#else
       grdir = gks_getenv("GRDIR");
       if (grdir == NULL) grdir = GRDIR;
       snprintf(pathname, MAXPATHLEN, "%s/lib/%s.%s", grdir, name, EXTENSION);
+#endif
       handle = dlopen(pathname, RTLD_LAZY);
     }
 #endif
diff --git a/lib/gks/socket.c b/lib/gks/socket.c
index 0f00afd..18e38f5 100644
--- a/lib/gks/socket.c
+++ b/lib/gks/socket.c
@@ -316,6 +316,10 @@ static int open_socket(int wstype)
       command = gks_getenv("GKS_QT");
       if (command == NULL)
         {
+#if 1
+          /* Force debian specific fallback path */
+          command = "/usr/bin/gksqt";
+#else
           env = gks_getenv("GRDIR");
           if (env == NULL) env = GRDIR;
 
@@ -326,6 +330,7 @@ static int open_socket(int wstype)
           snprintf(cmd, MAXPATHLEN, "%s/bin/gksqt", env);
 #endif
           command = cmd;
+#endif
         }
 #endif
     }
diff --git a/lib/gks/util.c b/lib/gks/util.c
index 08e29ce..4db608f 100644
--- a/lib/gks/util.c
+++ b/lib/gks/util.c
@@ -1576,6 +1576,11 @@ static int have_gksqt(void)
   env = gks_getenv("GKS_QT");
   if (env == NULL)
     {
+#if 1
+      /* Force debian specific fallback path */
+      path = (char *)gks_malloc(MAXPATHLEN);
+      snprintf(path, MAXPATHLEN, "/usr/bin/gksqt");
+#else
       env = gks_getenv("GRDIR");
       if (env == NULL) env = GRDIR;
 
@@ -1588,6 +1593,7 @@ static int have_gksqt(void)
 #endif
 #else
       snprintf(path, MAXPATHLEN, "%s\\bin\\gksqt.exe", env);
+#endif
 #endif
     }
   else
diff --git a/lib/gr/mathtex2_kerning.c b/lib/gr/mathtex2_kerning.c
index 7a182ba..cdda650 100644
--- a/lib/gr/mathtex2_kerning.c
+++ b/lib/gr/mathtex2_kerning.c
@@ -21,8 +21,13 @@ static void init_offsets(void)
   path = gks_getenv("GKS_FONTPATH");
   if (path == NULL)
     {
+#if 1
+      /* Force debian specific fallback path - /usr/share/gr/fonts/mathtex2_offsets.bin */
+      path = "/usr/share/gr";
+#else
       path = gks_getenv("GRDIR");
       if (path == NULL) path = GRDIR;
+#endif
     }
   strcpy(offsets_path, (char *)path);
 #ifndef _WIN32
diff --git a/lib/gr/stream.c b/lib/gr/stream.c
index 27fac81..9aab3b8 100644
--- a/lib/gr/stream.c
+++ b/lib/gr/stream.c
@@ -513,6 +513,11 @@ int gr_startlistener(void)
   command = gks_getenv("GR_PLOT");
   if (command == NULL)
     {
+#if 1
+      /* Force debian specific fallback path - /usr/bin/grplot */
+      cmd = (char *)gks_malloc(MAXPATHLEN);
+      snprintf(cmd, MAXPATHLEN, "/usr/bin/grplot --listen");
+#else
       env = gks_getenv("GRDIR");
       if (env == NULL) env = GRDIR;
 
@@ -521,6 +526,7 @@ int gr_startlistener(void)
       snprintf(cmd, MAXPATHLEN, "%s/Applications/grplot.app/Contents/MacOS/grplot --listen", env);
 #else
       snprintf(cmd, MAXPATHLEN, "%s/bin/grplot --listen", env);
+#endif
 #endif
       command = cmd;
     }
diff --git a/lib/gr3/gr3_glx.c b/lib/gr3/gr3_glx.c
index 0e5ba38..ab29152 100644
--- a/lib/gr3/gr3_glx.c
+++ b/lib/gr3/gr3_glx.c
@@ -42,6 +42,15 @@ int gr3_platform_initGL_(void)
   gr3_log_("gr3_platform_initGL_();");
   if (!platform_library)
     {
+#if 1
+      const char *grdir = GRLIB;
+      if (strlen(grdir) + strlen("libGR3platform.so") < MAXPATHLEN)
+        {
+          char pathname[MAXPATHLEN];
+          snprintf(pathname, MAXPATHLEN, "%s/libGR3platform.so", grdir);
+          platform_library = dlopen(pathname, RTLD_NOW | RTLD_LOCAL);
+        }
+#else
       const char *grdir = getenv("GRDIR");
       if (grdir == NULL)
         {
@@ -53,6 +62,7 @@ int gr3_platform_initGL_(void)
           snprintf(pathname, MAXPATHLEN, "%s/lib/libGR3platform.so", grdir);
           platform_library = dlopen(pathname, RTLD_NOW | RTLD_LOCAL);
         }
+#endif
     }
   if (!platform_library)
     {
