Description: use Debian's resource paths
 GenomeThreader in the original form requires environment variables set
 to specific paths _or_ uses hardcoded subdirectories of the directory where
 the gth binary is. Since this is /usr/bin in Debian and the FHS disallows
 subdirs there. we patch the source to add new default search directories.
Author: Sascha Steinbiss <satta@debian.org>
--- a/src/gth/bssm_param.c
+++ b/src/gth/bssm_param.c
@@ -29,6 +29,7 @@
 #include "gth/gthprobdef.h"
 #include "gth/gthspeciestab.h"
 #include "gth/showbool.h"
+#include "libgenomethreader/findfile.h"
 
 /*
   This is a collection of functions associated with
@@ -451,42 +452,7 @@
   if (gt_file_exists(filename))
     gt_str_append_cstr(path, filename);
   else {
-    if (strchr(filename, GT_PATH_SEPARATOR)) {
-      gt_error_set(err, "filename \"%s\" contains illegal symbol '%c': the "
-                        "path list specified by environment variable \"%s\" "
-                        "cannot be searched for it", filename,
-                        GT_PATH_SEPARATOR, BSSMENVNAME);
-      had_err = -1;
-    }
-    /* check for file path in environment variable */
-    if (!had_err)
-      had_err = gt_file_find_in_env(path, filename, BSSMENVNAME, err);
-    if (!had_err && !gt_str_length(path)) {
-      gt_error_set(err, "file \"%s\" not found in directory list specified "
-                        "by environment variable %s", filename, BSSMENVNAME);
-      had_err = -1;
-    }
-    if (!had_err) {
-      /* path found -> append filename */
-      gt_str_append_char(path, GT_PATH_SEPARATOR);
-      gt_str_append_cstr(path, filename);
-    }
-    else {
-      /* check for file path relative to binary */
-      int new_err = gt_file_find_exec_in_path(path, gt_error_get_progname(err),
-                                              NULL);
-      if (!new_err) {
-        gt_assert(gt_str_length(path));
-        gt_str_append_char(path, GT_PATH_SEPARATOR);
-        gt_str_append_cstr(path, "bssm");
-        gt_str_append_char(path, GT_PATH_SEPARATOR);
-        gt_str_append_cstr(path, filename);
-        if (gt_file_exists(gt_str_get(path))) {
-          gt_error_unset(err);
-          had_err = 0;
-        }
-      }
-    }
+    had_err = gth_find_file(filename, BSSMENVNAME, "bssm", path, err);
   }
 
   if (!had_err) {
--- a/src/gth/input.c
+++ b/src/gth/input.c
@@ -30,6 +30,7 @@
 #include "gth/input.h"
 #include "gth/md5_cache.h"
 #include "gth/parse_options.h"
+#include "libgenomethreader/findfile.h"
 
 #define GTHFORWARD  1        /* run program to match forward */
 #define GTHREVERSE  (1 << 1) /* run program to match reverse */
@@ -653,45 +654,14 @@
 {
   GtStr *path = gt_str_new();
   int had_err = 0;
+
   if (gt_file_exists(scorematrixfile)) {
     gt_str_set(path, scorematrixfile);
     return path;
   }
-  if (strchr(scorematrixfile, GT_PATH_SEPARATOR)) {
-    gt_error_set(err, "filename \"%s\" contains illegal symbol '%c': the path "
-                      "list specified by environment variable \"%s\" cannot be "
-                      "searched for it", scorematrixfile, GT_PATH_SEPARATOR,
-                      GTHDATAENVNAME);
-    had_err = -1;
-  }
-  if (!had_err)
-    had_err = gt_file_find_in_env(path, scorematrixfile, GTHDATAENVNAME, err);
-  if (!had_err && !gt_str_length(path)) {
-    gt_error_set(err, "file \"%s\" not found in directory list specified by "
-                 "environment variable %s", scorematrixfile, GTHDATAENVNAME);
-    had_err = -1;
-  }
-  if (!had_err) {
-    gt_assert(gt_str_length(path));
-    /* path found -> append score matrix file name */
-    gt_str_append_char(path, GT_PATH_SEPARATOR);
-    gt_str_append_cstr(path, scorematrixfile);
-  }
-  else {
-    /* check for file relative to binary */
-    int new_err = gt_file_find_exec_in_path(path, gt_error_get_progname(err),
-                                            NULL);
-    if (!new_err) {
-      gt_str_append_char(path, GT_PATH_SEPARATOR);
-      gt_str_append_cstr(path, GTHDATADIRNAME);
-      gt_str_append_char(path, GT_PATH_SEPARATOR);
-      gt_str_append_cstr(path, scorematrixfile);
-      if (gt_file_exists(gt_str_get(path))) {
-        gt_error_unset(err);
-        had_err = 0;
-      }
-    }
-  }
+
+  had_err = gth_find_file(scorematrixfile, GTHDATAENVNAME, GTHDATADIRNAME, path,
+                          err);
   if (had_err) {
     gt_str_delete(path);
     return NULL;
--- /dev/null
+++ b/src/libgenomethreader/findfile.c
@@ -0,0 +1,74 @@
+/*
+  Copyright (c) 2020 Sascha Steinbiss <sascha@steinbiss.name>
+
+  Permission to use, copy, modify, and distribute this software for any
+  purpose with or without fee is hereby granted, provided that the above
+  copyright notice and this permission notice appear in all copies.
+
+  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+*/
+
+#include "findfile.h"
+#include "core/fileutils_api.h"
+#include "core/compat_api.h"
+
+int gth_find_file(const char *filename, const char *envname,
+                  const char *dirname, GtStr *out, GtError *err)
+{
+  int had_err = 0;
+  const char **defaultpath;
+  static const char *defaultpaths[] = {
+    "/usr/share/genomethreader",
+    "/usr/local/share/genomethreader",
+    "/usr/lib/genomethreader",
+    "/usr/local/lib/genomethreader",
+    NULL
+  };
+
+  gt_str_reset(out);
+  /* check in directory given by envname */
+  if (getenv(envname)) {
+    gt_str_append_cstr(out, getenv(envname));
+    gt_str_append_char(out, GT_PATH_SEPARATOR);
+    gt_str_append_cstr(out, filename);
+    if (gt_file_exists(gt_str_get(out))) {
+      return 0;
+    }
+  }
+
+  /* check for file relative to binary */
+  gt_str_reset(out);
+  had_err = gt_file_find_exec_in_path(out, gt_error_get_progname(err), NULL);
+  if (!had_err) {
+    gt_str_append_char(out, GT_PATH_SEPARATOR);
+    gt_str_append_cstr(out, dirname);
+    gt_str_append_char(out, GT_PATH_SEPARATOR);
+    gt_str_append_cstr(out, filename);
+    if (gt_file_exists(gt_str_get(out))) {
+      return 0;
+    }
+  }
+  
+  /* check for file in set of default paths */
+  for (defaultpath = defaultpaths; *defaultpath; defaultpath++) {
+    gt_str_reset(out);
+    gt_str_append_cstr(out, *defaultpath);
+    gt_str_append_char(out, GT_PATH_SEPARATOR);
+    gt_str_append_cstr(out, dirname);
+    gt_str_append_char(out, GT_PATH_SEPARATOR);
+    gt_str_append_cstr(out, filename);
+    if (gt_file_exists(gt_str_get(out))) {
+      return 0;
+    }
+  }
+  
+  gt_error_set(err, "could not find file '%s' in any of the search paths",
+               filename);
+  return -1;
+}
--- /dev/null
+++ b/src/libgenomethreader/findfile.h
@@ -0,0 +1,26 @@
+/*
+  Copyright (c) 2020 Sascha Steinbiss <sascha@steinbiss.name>
+
+  Permission to use, copy, modify, and distribute this software for any
+  purpose with or without fee is hereby granted, provided that the above
+  copyright notice and this permission notice appear in all copies.
+
+  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+*/
+
+#ifndef GTH_FINDFILE_H
+#define GTH_FINDFILE_H
+
+#include "core/str_api.h"
+#include "core/error_api.h"
+
+int gth_find_file(const char *filename, const char *envname,
+                  const char *dirname, GtStr *out, GtError *err);
+                  
+#endif
--- a/src/libgenomethreader/gthmkvtree.c
+++ b/src/libgenomethreader/gthmkvtree.c
@@ -6,6 +6,7 @@
 #include "core/ma_api.h"
 #include "gth/gthdef.h"
 #include "gth/gthoutput.h"
+#include "libgenomethreader/findfile.h"
 #include "libgenomethreader/gthmkvtree.h"
 #include "types.h"
 #include "virtualdef.h"
@@ -52,39 +53,14 @@
 {
   int rval, had_err = 0;
   gt_error_check(err);
-  if (!getenv(GTHDATAENVNAME)) {
-    gt_error_set(err, "$%s not defined. Please set correctly", GTHDATAENVNAME);
-    had_err = -1;
-  }
+
+  GtStr *path = gt_str_new();
+  had_err = gth_find_file(mapping, GTHDATAENVNAME, GTHDATADIRNAME, path, err);
   if (!had_err) {
-    rval = snprintf(smapfile, PATH_MAX+1, "%s%c%s", getenv(GTHDATAENVNAME),
-                    GT_PATH_SEPARATOR, mapping);
-    gt_assert(rval < PATH_MAX + 1);
-    if (!gt_file_exists(smapfile)) {
-      gt_error_set(err, "cannot open smap file '%s'", smapfile);
-      had_err = -1;
-    }
-  }
-  if (had_err) {
-    /* check for file relative to binary */
-    GtStr *path;
-    int new_err;
-    path = gt_str_new();
-    new_err = gt_file_find_exec_in_path(path, gt_error_get_progname(err), NULL);
-    if (!new_err) {
-      gt_str_append_char(path, GT_PATH_SEPARATOR);
-      gt_str_append_cstr(path, GTHDATADIRNAME);
-      gt_str_append_char(path, GT_PATH_SEPARATOR);
-      gt_str_append_cstr(path, mapping);
-      if (gt_file_exists(gt_str_get(path))) {
-        rval = snprintf(smapfile, PATH_MAX+1, "%s", gt_str_get(path));
-        gt_assert(rval < PATH_MAX + 1);
-        gt_error_unset(err);
-        had_err = 0;
-      }
-    }
-    gt_str_delete(path);
+   rval = snprintf(smapfile, PATH_MAX+1, "%s", gt_str_get(path));
+   gt_assert(rval < PATH_MAX + 1);
   }
+  gt_str_delete(path);
   return had_err;
 }
 
--- a/src/libgenomethreader/gthpre.c
+++ b/src/libgenomethreader/gthpre.c
@@ -11,6 +11,7 @@
 #include "gth/gthdef.h"
 #include "gth/gthoutput.h"
 #include "gth/input.h"
+#include "libgenomethreader/findfile.h"
 #include "libgenomethreader/gthmkvtree.h"
 #include "libgenomethreader/gthpolyafunc.h"
 #include "libgenomethreader/gthpre.h"
@@ -301,49 +302,22 @@
 
   /* create masked file if necessary */
   if (createmaskedfile) {
-    sprintf(dnafilename, "%s.%s", POLYA_FASTAFILENAME, DNASUFFIX);
-    /* check if polyatail.dna exists */
-    fp = scanpathsforfile(GTHDATAENVNAME, dnafilename); /* XXX */
-    if (!fp) {
-      gt_error_set(err, "file \"%s\" not found in $%s. Set correctly?",
-                   dnafilename, GTHDATAENVNAME);
-      had_err = -1;
-    }
-    if (!had_err /* XXX */ && DELETEFILEHANDLE(fp)) {
-      fprintf(stderr,"%s\n", messagespace());
-      exit(EXIT_FAILURE);
-    }
-
-    /* call vmatch and save masked file */
-    if (!had_err && !getenv(GTHDATAENVNAME)) {
-      gt_error_set(err, "$%s not defined. Please set correctly",
-                   GTHDATAENVNAME);
-      had_err = -1;
-    }
+    int rval = 0;
+    GtStr *path = gt_str_new();
 
+    sprintf(dnafilename, "%s.%s", POLYA_FASTAFILENAME, DNASUFFIX);
+    had_err = gth_find_file(dnafilename, GTHDATAENVNAME, GTHDATADIRNAME, path,
+                            err);
     if (!had_err) {
-      sprintf(dnafilename, "%s%c%s", getenv(GTHDATAENVNAME), GT_PATH_SEPARATOR,
-              POLYA_FASTAFILENAME);
-    }
-    else {
-      /* check for file relative to binary */
-      GtStr *path;
-      int new_err;
-      path = gt_str_new();
-      new_err = gt_file_find_exec_in_path(path, gt_error_get_progname(err),
-                                          NULL);
-      if (!new_err) {
-        gt_str_append_char(path, GT_PATH_SEPARATOR);
-        gt_str_append_cstr(path, GTHDATADIRNAME);
-        if (gt_file_exists_and_is_dir(gt_str_get(path))) {
-          sprintf(dnafilename, "%s%c%s", gt_str_get(path), GT_PATH_SEPARATOR,
-                  POLYA_FASTAFILENAME);
-          gt_error_unset(err);
-          had_err = 0;
-        }
-      }
+       rval = snprintf(dnafilename, PATH_MAX+1, "%s", gt_str_get(path));
+       gt_assert(rval < PATH_MAX + 1);
+       /* clip off DNASUFFIX */
+       gt_assert(strlen(dnafilename) > 5);
+       dnafilename[strlen(dnafilename)-4] = '\0';
     }
+    gt_str_delete(path);
 
+    /* call vmatch and save masked file */
     if (!had_err) {
       /* open file pointer for masked reference file */
       fp = gt_fa_xfopen(maskedfilename, "w");
