/****************************************************************************
 * This file is part of the project AqDiagram.
 * AqDiagram (c) by 2020 Martin Preuss, all rights reserved.
 *
 * The license for this file can be found in the file COPYING which you
 * should have received along with this file.
 ****************************************************************************/

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif


#include "./aqdiagram.h"


#include <gwenhywfar/gwenhywfar.h>
#include <gwenhywfar/pathmanager.h>
#include <gwenhywfar/debug.h>





int AQDG_Init()
{
  int rv;
  const char *s;

  rv=GWEN_Init();
  if (rv) {
    DBG_ERROR(AQDG_LOGDOMAIN, "here (%d)", rv);
    return rv;
  }
  if (!GWEN_Logger_IsOpen(AQDG_LOGDOMAIN)) {
    GWEN_Logger_Open(AQDG_LOGDOMAIN,
                     "aqdiagram", 0,
		     GWEN_LoggerType_Console,
		     GWEN_LoggerFacility_User);
  }

  s=getenv("AQDG_LOGLEVEL");
  if (s && *s) {
    GWEN_LOGGER_LEVEL ll;

    ll=GWEN_Logger_Name2Level(s);
    GWEN_Logger_SetLevel(AQDG_LOGDOMAIN, ll);
  }
  else
    GWEN_Logger_SetLevel(AQDG_LOGDOMAIN, GWEN_LoggerLevel_Notice);

  DBG_INFO(AQDG_LOGDOMAIN,
           "AqDiagram v%s: initialising",
           AQDG_VERSION_FULL_STRING);

  /* define locale paths */
  GWEN_PathManager_DefinePath(AQDG_PM_LIBNAME, AQDG_PM_LOCALEDIR);
#if defined(OS_WIN32) || defined(ENABLE_LOCAL_INSTALL)
  /* add folder relative to EXE */
  GWEN_PathManager_AddRelPath(AQDG_PM_LIBNAME,
			      AQDG_PM_LIBNAME,
			      AQDG_PM_LOCALEDIR,
			      LOCALEDIR,
			      GWEN_PathManager_RelModeExe);
#else
  /* add absolute folder */
  GWEN_PathManager_AddPath(AQDG_PM_LIBNAME,
			   AQDG_PM_LIBNAME,
			   AQDG_PM_LOCALEDIR,
			   LOCALEDIR);
#endif

  /* define data paths */
  GWEN_PathManager_DefinePath(AQDG_PM_LIBNAME, AQDG_PM_DATADIR);
#if defined(OS_WIN32) || defined(ENABLE_LOCAL_INSTALL)
  /* add folder relative to EXE */
  GWEN_PathManager_AddRelPath(AQDG_PM_LIBNAME,
			      AQDG_PM_LIBNAME,
			      AQDG_PM_DATADIR,
			      AQDIAGRAM_DATA_DIR,
			      GWEN_PathManager_RelModeExe);
#else
  /* add absolute folder */
  GWEN_PathManager_AddPath(AQDG_PM_LIBNAME,
			   AQDG_PM_LIBNAME,
			   AQDG_PM_DATADIR,
			   AQDIAGRAM_DATA_DIR);
#endif

  if (1) {
    GWEN_STRINGLIST *sl=GWEN_PathManager_GetPaths(AQDG_PM_LIBNAME, AQDG_PM_LOCALEDIR);
    if (sl) {
      const char *localedir=GWEN_StringList_FirstString(sl);

      rv=GWEN_I18N_BindTextDomain_Dir(PACKAGE, localedir);
      if (rv) {
	DBG_ERROR(AQDG_LOGDOMAIN, "Could not bind textdomain (%d)", rv);
      }
      else {
	rv=GWEN_I18N_BindTextDomain_Codeset(PACKAGE, "UTF-8");
	if (rv) {
	  DBG_ERROR(AQDG_LOGDOMAIN, "Could not set codeset (%d)", rv);
	}
      }

      GWEN_StringList_free(sl);
    }
  }

  return 0;
}



int AQDG_Fini()
{
  /* undefine our own paths */
  GWEN_PathManager_UndefinePath(AQDG_PM_LIBNAME, AQDG_PM_LOCALEDIR);
  GWEN_PathManager_UndefinePath(AQDG_PM_LIBNAME, AQDG_PM_DATADIR);
  
  /* remove AqBanking additions to all pathmanagers */
  GWEN_PathManager_RemovePaths(AQDG_PM_LIBNAME);
  GWEN_Logger_Close(AQDG_LOGDOMAIN);
  GWEN_Fini();
  return 0;
}



