From a2d2c254f96fe00ed597632009108674aca5316f Mon Sep 17 00:00:00 2001
From: Humdinger <humdingerb@gmail.com>
Date: Sat, 16 Jul 2016 09:26:26 +0200
Subject: Relocate save files, high scores and config

Originally by Markus Himmel.

diff --git a/client/chart.c b/client/chart.c
index 68e9f44..7da0542 100644
--- a/client/chart.c
+++ b/client/chart.c
@@ -202,17 +202,22 @@ void chart_load()
 	if ( charts ) list_delete( charts ); charts = 0;
 	charts = list_create( LIST_AUTO_DELETE, chart_set_delete );
     /* load highscore */
+#ifndef __HAIKU__
     if ( !chart_load_from_path( HI_DIR ) ) {
         fprintf( stderr, _("Unable to access highscore chart in '%s'.\n"), HI_DIR );
         fprintf( stderr, _("Trying to use config directory '%s'.\n"), config.dir_name );
+#endif
         if ( !chart_load_from_path( config.dir_name ) ) {
             fprintf( stderr, _("Unable to access highscore chart in config directory... won't be "
                              "able to save any results. Sorry.\n") );
             return;
         }
+#ifndef __HAIKU__
     }
+#endif
     printf( _("Saving highscore chart in: %s\n"), chart_path );
     /* compute size and position stuff of highscore */
+    {
     char *cheader = _("Name      Level Score");
     chart_pos.w = stk_font_string_width( ccfont, cheader );
     chart_pos.h = ccfont->height +  chart_gap + /* title + gap */
@@ -220,6 +225,7 @@ void chart_load()
               chart_gap + /*gap between caption and entries */
               ccfont->height; /* caption size */
     chart_level_offset = stk_font_string_width( ccfont, _("name.-----") ) + stk_font_string_width( ccfont, _("Level") ) / 2; /* level offset centered */
+	}
 }
 /*
 ====================================================================
@@ -344,6 +350,7 @@ void chart_show( Set_Chart *chart, int x, int y, int w, int h )
         chart->name );
 	/* caption */
     ccfont->align = STK_FONT_ALIGN_LEFT | STK_FONT_ALIGN_TOP;
+    {
     char *cheader = _("Name      Level Score");
     stk_font_write( ccfont, stk_display, 
         chart_pos.x, chart_pos.y + ccfont->height + chart_gap, -1, 
@@ -370,6 +377,7 @@ void chart_show( Set_Chart *chart, int x, int y, int w, int h )
             chart_pos.x + chart_pos.w, entry_offset, -1, number_buffer );
         /* change offset */
         entry_offset += font->height;
+    	}
     }
     stk_display_store_rect( &chart_pos );
 }
@@ -389,12 +397,13 @@ void chart_show_compact( Set_Chart *chart, int x, int y, int w, int h )
 
     /* caption */
     ccfont->align = STK_FONT_ALIGN_LEFT | STK_FONT_ALIGN_TOP;
+	{
     char *cheader = _("Name      Level Score");
     stk_font_write( ccfont, stk_display, px, py, -1, cheader );
 	
     /* get entry offset */
     entry_offset = ( ccfont->height + 2) + py;
-	
+
     /* entries */
     for ( i = 0; i < CHART_ENTRY_COUNT; i++ ) {
         font = cfont;
@@ -416,6 +425,7 @@ void chart_show_compact( Set_Chart *chart, int x, int y, int w, int h )
         /* change offset */
         entry_offset += font->height - 1;
     }
+	}
     { SDL_Rect region = { x, y, w, h };
     stk_display_store_rect( &region ); }
 }
diff --git a/client/config.c b/client/config.c
index 83a924c..42faa07 100644
--- a/client/config.c
+++ b/client/config.c
@@ -21,6 +21,11 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 
+#ifdef __HAIKU__
+#include <FindDirectory.h>
+#include <fs_info.h>
+#endif
+
 #include "config.h"
 #include "../common/parser.h"
 
@@ -38,7 +43,19 @@ Config config;
 void config_check_dir()
 {
     char level_dir[512];
-    snprintf( config.dir_name, sizeof(config.dir_name)-1, "%s/%s", (getenv( "HOME" )?getenv( "HOME" ):"."), CONFIG_DIR_NAME );
+#ifdef __HAIKU__
+    dev_t volume = dev_for_path("/boot");
+    if ( find_directory( B_USER_SETTINGS_DIRECTORY, volume, false,
+        config.dir_name, sizeof(config.dir_name)-1 ) == B_OK &&
+        strlen(config.dir_name) + strlen(CONFIG_DIR_NAME)
+        < sizeof(config.dir_name)-1 ) {
+        strcat(config.dir_name, "/");
+        strcat(config.dir_name, CONFIG_DIR_NAME);
+    } else
+        snprintf( config.dir_name, sizeof(config.dir_name)-1, "%s/%s", (getenv( "HOME" )?getenv( "HOME" ):"."), CONFIG_DIR_NAME );
+#else
+     snprintf( config.dir_name, sizeof(config.dir_name)-1, "%s/%s", (getenv( "HOME" )?getenv( "HOME" ):"."), CONFIG_DIR_NAME );
+#endif
     /* test and create .lgames */
     if ( opendir( config.dir_name ) == 0 ) {
         fprintf( stderr, "couldn't find/open config directory '%s'\n", config.dir_name );
diff --git a/client/editor.c b/client/editor.c
index e182081..ff8503e 100644
--- a/client/editor.c
+++ b/client/editor.c
@@ -15,6 +15,11 @@
  *                                                                         *
  ***************************************************************************/
 
+#ifdef __HAIKU__
+#include <FindDirectory.h>
+#include <fs_info.h>
+#endif
+
 #include "lbreakout.h"
 #include "../game/game.h"
 #include "game.h"
@@ -722,7 +727,22 @@ int editor_init( char *file_name )
 {
     FILE *file = 0;
     /* set full file name */
-    snprintf( edit_file_name, sizeof(edit_file_name)-1, "%s/%s/lbreakout2-levels/%s", (getenv( "HOME" )?getenv( "HOME" ):"."), CONFIG_DIR_NAME, file_name );
+#ifdef __HAIKU__
+    dev_t volume = dev_for_path("/boot");
+    if ( find_directory(B_USER_SETTINGS_DIRECTORY, volume, false,
+        edit_file_name, sizeof(edit_file_name)-1 ) == B_OK &&
+        strlen(edit_file_name) + strlen(CONFIG_DIR_NAME) + strlen(file_name)
+        < sizeof(edit_file_name) - 18 ) {
+        strcat(edit_file_name, "/");
+        strcat(edit_file_name, CONFIG_DIR_NAME);
+        strcat(edit_file_name, "/lbreakout2-levels/");
+        strcat(edit_file_name, file_name);
+    } else
+        snprintf( edit_file_name, sizeof(edit_file_name)-1, "%s/%s/lbreakout2-levels/%s", (getenv( "HOME" )?getenv( "HOME" ):"."), CONFIG_DIR_NAME, file_name );
+    fprintf( stderr, "HAIKU: Saving at %s\n", edit_file_name);
+#else
+     snprintf( edit_file_name, sizeof(edit_file_name)-1, "%s/%s/lbreakout2-levels/%s", (getenv( "HOME" )?getenv( "HOME" ):"."), CONFIG_DIR_NAME, file_name );
+#endif
     /* test this file for write access. use append to keep contents */
     if ( ( file = fopen( edit_file_name, "a" ) ) == 0 ) {
         fprintf( stderr, "Permission to write to file '%s' denied.\n", edit_file_name );
diff --git a/client/lbreakout.h b/client/lbreakout.h
index 4b0090c..8ab9cd6 100644
--- a/client/lbreakout.h
+++ b/client/lbreakout.h
@@ -48,6 +48,8 @@ Global definitions for LBreakout and general system includes.
 /* config directory name in home directory */
 #ifdef _WIN32
 #define CONFIG_DIR_NAME "lgames"
+#elif __HAIKU__
+#define CONFIG_DIR_NAME "LGames"
 #else
 #define CONFIG_DIR_NAME ".lgames"
 #endif
diff --git a/client/main.c b/client/main.c
index 81e7f57..7f29cfc 100644
--- a/client/main.c
+++ b/client/main.c
@@ -23,6 +23,11 @@
 #include <unistd.h>
 #endif
 
+#ifdef __HAIKU__
+#include <FindDirectory.h>
+#include <fs_info.h>
+#endif
+
 #include "lbreakout.h"
 #include "../game/game.h"
 #include "file.h"
@@ -198,7 +203,19 @@ int main(int argc, char *argv[])
                 /* new set? */
                 if ( strequal( NEW_SET, edit_set ) ) {
                     editor_file = calloc( 16, sizeof( char ) );
+#ifdef __HAIKU__
+                    if ( find_directory(B_USER_SETTINGS_DIRECTORY, dev_for_path("/boot"), false,
+                        path, sizeof(path)-1 ) == B_OK &&
+                        strlen(path) + strlen(CONFIG_DIR_NAME)
+                        < sizeof(path) - 18 ) {
+                        strcat(path, "/");
+                        strcat(path, CONFIG_DIR_NAME);
+                        strcat(path, "/lbreakout2-levels");
+                    } else
+                        snprintf( path, sizeof(path)-1, "%s/%s/lbreakout2-levels", getenv( "HOME" )? getenv("HOME"):".", CONFIG_DIR_NAME );
+#else
                     snprintf( path, sizeof(path)-1, "%s/%s/lbreakout2-levels", getenv( "HOME" )? getenv("HOME"):".", CONFIG_DIR_NAME );
+#endif
                     if ( !enter_string( font, _("Set Name:"), editor_file, 12 ) || !file_check( path, editor_file, "w" ) ) {
                         free( editor_file );
                         break;
diff --git a/client/manager.c b/client/manager.c
index 5c94c38..c798bc1 100644
--- a/client/manager.c
+++ b/client/manager.c
@@ -15,6 +15,11 @@
  *                                                                         *
  ***************************************************************************/
 
+#ifdef __HAIKU__
+#include <FindDirectory.h>
+#include <fs_info.h>
+#endif
+
 #include "manager.h"
 #include "../game/game.h"
 #include "file.h"
@@ -278,7 +283,19 @@ void levelsets_load_names()
         delete_text( text );
     }
     /* parse home directory */
-    snprintf( level_dir, sizeof(level_dir)-1, "%s/%s/lbreakout2-levels", (getenv( "HOME" )?getenv( "HOME" ):"."), CONFIG_DIR_NAME );
+#ifdef __HAIKU__
+    if ( find_directory( B_USER_SETTINGS_DIRECTORY, dev_for_path("/boot"), false,
+        level_dir, sizeof(level_dir)-1 ) == B_OK &&
+        strlen(level_dir) + strlen(CONFIG_DIR_NAME)
+        < sizeof(level_dir)-19 ) {
+        strcat(level_dir, "/");
+        strcat(level_dir, CONFIG_DIR_NAME);
+        strcat(level_dir, "/lbreakout2-levels");
+    } else
+        snprintf( level_dir, sizeof(level_dir)-1, "%s/%s/lbreakout2-levels", (getenv( "HOME" )?getenv( "HOME" ):"."), CONFIG_DIR_NAME );
+#else
+     snprintf( level_dir, sizeof(level_dir)-1, "%s/%s/lbreakout2-levels", (getenv( "HOME" )?getenv( "HOME" ):"."), CONFIG_DIR_NAME );
+#endif
     if ( ( text = get_file_list( level_dir, 0, level_dir ) ) ) {
         for ( i = 0; i < text->count; i++ ) {
             /* filter stuff */
@@ -353,7 +370,21 @@ void cb_delete_set()
         return;
     }
     /* get file name + path */
-    snprintf( fname, sizeof(fname)-1,"%s/%s/lbreakout2-levels/%s", getenv( "HOME" ), CONFIG_DIR_NAME, levelset_names_home[config.levelset_id_home] );
+#ifdef __HAIKU__
+    if ( find_directory( B_USER_SETTINGS_DIRECTORY, dev_for_path("/boot"), false,
+        fname, sizeof(fname)-1 ) == B_OK &&
+        strlen(fname) + strlen(CONFIG_DIR_NAME)
+        + strlen(levelset_names_home[config.levelset_id_home])
+        < sizeof(fname)-19 ) {
+        strcat(fname, "/");
+        strcat(fname, CONFIG_DIR_NAME);
+        strcat(fname, "/lbreakout2-levels/");
+        strcat(fname, levelset_names_home[config.levelset_id_home]);
+    } else
+        snprintf( fname, sizeof(fname)-1,"%s/%s/lbreakout2-levels/%s", getenv( "HOME" ), CONFIG_DIR_NAME, levelset_names_home[config.levelset_id_home] );
+#else
+     snprintf( fname, sizeof(fname)-1,"%s/%s/lbreakout2-levels/%s", getenv( "HOME" ), CONFIG_DIR_NAME, levelset_names_home[config.levelset_id_home] );
+#endif
     remove( fname );
     levelsets_load_names(); /* reinit name lists and configs indices */
     /* reassign these name lists as position in memory has changed */
diff --git a/client/slot.c b/client/slot.c
index 238d4c8..a32805a 100644
--- a/client/slot.c
+++ b/client/slot.c
@@ -15,15 +15,34 @@
  *                                                                         *
  ***************************************************************************/
 
+#ifdef __HAIKU__
+#include <FindDirectory.h>
+#include <fs_info.h>
+#endif
+
 #include "lbreakout.h"
 #include "slot.h"
 
 static FILE *open_slot( int id, char *mode )
 {
     char fname[512];
-    snprintf( fname, 511, "%s/%s/lbr2_save_%d", 
-              (getenv( "HOME" )?getenv( "HOME" ):"."), 
+#ifdef __HAIKU__
+    dev_t volume = dev_for_path("/boot");
+    if ( find_directory(B_USER_SETTINGS_DIRECTORY, volume, false,
+        fname, sizeof(fname)-1 ) == B_OK && strlen(fname) +
+        strlen(CONFIG_DIR_NAME) < sizeof(fname) - 16 ) {
+        off_t len = strlen(fname);
+        snprintf( fname+len, 511-len, "/%s/lbr2_save_%d", CONFIG_DIR_NAME, id);
+	} else
+        snprintf( fname, 511, "%s/%s/lbr2_save_%d",
+              (getenv( "HOME" )?getenv( "HOME" ):"."),
               CONFIG_DIR_NAME, id );
+    fprintf( stderr, "HAIKU: Accessing savefile at %s\n", fname );
+#else
+    snprintf( fname, 511, "%s/%s/lbr2_save_%d",
+              (getenv( "HOME" )?getenv( "HOME" ):"."),
+              CONFIG_DIR_NAME, id );
+#endif
     //printf( "accessing '%s' in mode '%s'\n", fname, mode );
     return fopen( fname, mode );
 }
@@ -62,9 +81,22 @@ int slot_save( int slot_id, GameSlot *gs )
 int slot_delete( int slot_id )
 {
     char fname[512];
+#ifdef __HAIKU__
+    dev_t volume = dev_for_path("/boot");
+    if ( find_directory(B_USER_SETTINGS_DIRECTORY, volume, false,
+        fname, sizeof(fname)-1 ) == B_OK && strlen(fname) +
+        strlen(CONFIG_DIR_NAME) < sizeof(fname) - 16 ) {
+        off_t len = strlen(fname);
+        snprintf( fname+len, 511-len, "/%s/lbr2_save_%d", CONFIG_DIR_NAME, slot_id);
+    } else
+        snprintf( fname, 511, "%s/%s/lbr2_save_%d",
+              (getenv( "HOME" )?getenv( "HOME" ):"."),
+              CONFIG_DIR_NAME, slot_id );
+#else
     snprintf( fname, 511, "%s/%s/lbr2_save_%d", 
               (getenv( "HOME" )?getenv( "HOME" ):"."), 
               CONFIG_DIR_NAME, slot_id );
+#endif
     if ( remove(fname) != 0 )
     {
         fprintf( stderr, "ERROR: couldn't delete file %s\n", fname );
diff --git a/client/theme.c b/client/theme.c
index ad6de96..b1ef0a3 100644
--- a/client/theme.c
+++ b/client/theme.c
@@ -15,6 +15,11 @@
  *                                                                         *
  ***************************************************************************/
 
+#ifdef __HAIKU__
+#include <FindDirectory.h>
+#include <fs_info.h>
+#endif
+
 #include <sys/types.h>
 #include <unistd.h>
 #include <sys/stat.h>
@@ -133,10 +138,25 @@ static char theme_dir[512];
 static char theme_path[512];
 void theme_set_dir( char *name )
 {
-    if ( name[0] == '~' )
+    if ( name[0] == '~' ) {
+#ifdef __HAIKU__
+        dev_t volume = dev_for_path("/boot");
+        if ( find_directory( B_USER_SETTINGS_DIRECTORY, volume, false,
+            theme_dir, sizeof(theme_dir)-1 ) == B_OK &&
+            strlen(theme_dir) + strlen(CONFIG_DIR_NAME) + strlen(name)
+            < sizeof(theme_dir)-19 ) {
+            strcat(theme_dir, "/");
+            strcat(theme_dir, CONFIG_DIR_NAME);
+            strcat(theme_dir, "/lbreakout2-themes/");
+            strcat(theme_dir, name + (off_t)1);
+        } else
+            snprintf( theme_dir, sizeof(theme_dir)-1, "%s/%s/lbreakout2-themes/%s",
+                     (getenv( "HOME" )?getenv( "HOME" ):"."), CONFIG_DIR_NAME, name + 1 );
+#else
         snprintf( theme_dir, sizeof(theme_dir)-1, "%s/%s/lbreakout2-themes/%s", 
                  (getenv( "HOME" )?getenv( "HOME" ):"."), CONFIG_DIR_NAME, name + 1 );
-    else {
+#endif
+    } else {
         if ( STRCMP( SRC_DIR, "." ) ) {
             getcwd( theme_dir, 511 );
             strcat( theme_dir, "/gfx/" );
@@ -327,6 +347,19 @@ void theme_get_list()
         closedir( hdir );
     }
     /* home theme directory */
+#ifdef __HAIKU__
+    if ( find_directory( B_USER_SETTINGS_DIRECTORY, dev_for_path("/boot"), false,
+        dir, sizeof(dir)-1 ) == B_OK &&
+        strlen(dir) + strlen(CONFIG_DIR_NAME)
+        < sizeof(dir)-19 ) {
+        strcat(dir, "/");
+        strcat(dir, CONFIG_DIR_NAME);
+        strcat(dir, "/lbreakout2-themes");
+    } else
+        snprintf( dir, sizeof(dir)-1, "%s/%s/lbreakout2-themes", (getenv( "HOME" )?getenv( "HOME" ):"."), CONFIG_DIR_NAME );
+#else
+    snprintf( dir, sizeof(dir)-1, "%s/%s/lbreakout2-themes", (getenv( "HOME" )?getenv( "HOME" ):"."), CONFIG_DIR_NAME );
+#endif
     snprintf( dir, sizeof(dir)-1, "%s/%s/lbreakout2-themes", (getenv( "HOME" )?getenv( "HOME" ):"."), CONFIG_DIR_NAME );
     if ( ( hdir = opendir( dir ) ) != 0 ) {
         while ( ( entry = readdir( hdir ) ) ) {
diff --git a/game/levels.c b/game/levels.c
index a93a633..2d7e84c 100644
--- a/game/levels.c
+++ b/game/levels.c
@@ -15,6 +15,11 @@
  *                                                                         *
  ***************************************************************************/
 
+#ifdef __HAIKU__
+#include <FindDirectory.h>
+#include <fs_info.h>
+#endif
+
 #include "levels.h"
 
 /*
@@ -66,9 +71,26 @@ FILE *levelset_open( const char *fname, char *mode )
 	FILE *file;
 	char path[512];
 	if ( fname[0] == '~' ) {
-		snprintf( path, sizeof(path)-1, "%s/%s/lbreakout2-levels/%s", 
-			(getenv( "HOME" )?getenv( "HOME" ):"."), 
+#ifdef __HAIKU__
+        if ( find_directory( B_USER_SETTINGS_DIRECTORY, dev_for_path("/boot"), false,
+            path, sizeof(path)-1 ) == B_OK &&
+            strlen(path) + strlen(CONFIG_DIR_NAME)
+            + strlen(fname)
+            < sizeof(path)-19 ) {
+            strcat(path, "/");
+            strcat(path, CONFIG_DIR_NAME);
+            strcat(path, "/lbreakout2-levels/");
+            strcat(path, fname + 1);
+        } else
+			snprintf( path, sizeof(path)-1, "%s/%s/lbreakout2-levels/%s",
+				(getenv( "HOME" )?getenv( "HOME" ):"."),
+				CONFIG_DIR_NAME, fname + 1 );
+	fprintf ( stderr, "HAIKU: opening %s\n", path );
+#else
+		snprintf( path, sizeof(path)-1, "%s/%s/lbreakout2-levels/%s",
+			(getenv( "HOME" )?getenv( "HOME" ):"."),
 			CONFIG_DIR_NAME, fname + 1 );
+#endif
 	}
 	else
 	if ( fname[0] != '/' ) /* keep global pathes */
@@ -275,10 +297,25 @@ int levelset_save( LevelSet *set, char *fname )
 	int i, j, k;
 
 	if ( set == 0 || set->count == 0 ) return 0;
-
+#ifdef __HAIKU__
+	if ( find_directory( B_USER_SETTINGS_DIRECTORY, dev_for_path("/boot"), false,
+		path, sizeof(path)-1 ) == B_OK &&
+		strlen(path) + strlen(CONFIG_DIR_NAME)
+		+ strlen(fname)
+		< sizeof(path)-19 ) {
+		strcat(path, "/");
+		strcat(path, CONFIG_DIR_NAME);
+		strcat(path, "/lbreakout2-levels/");
+		strcat(path, (fname[0]=='~') ? fname + 1 : fname);
+		} else
+			snprintf( path, sizeof(path)-1, "%s/%s/lbreakout2-levels/%s", 
+				(getenv( "HOME" )?getenv( "HOME" ):"."), 
+				CONFIG_DIR_NAME, (fname[0]=='~')?fname+1:fname );
+#else
 	snprintf( path, sizeof(path)-1, "%s/%s/lbreakout2-levels/%s", 
 			(getenv( "HOME" )?getenv( "HOME" ):"."), 
 			CONFIG_DIR_NAME, (fname[0]=='~')?fname+1:fname );
+#endif
 	if ( ( file = fopen( path, "w" ) ) == 0 ) {
 		fprintf( stderr, "couldn't open %s\n", path );
 		return 0;
-- 
2.7.0


From 01b2e359745bf090fe5c17c65ff29f2a794ecee2 Mon Sep 17 00:00:00 2001
From: Humdinger <humdingerb@gmail.com>
Date: Sat, 16 Jul 2016 09:31:55 +0200
Subject: Changes to configure

Remove -lm, originally by Markus Himmel.

diff --git a/configure b/configure
index 6f899f5..5f1dc4c 100755
--- a/configure
+++ b/configure
@@ -8573,7 +8573,7 @@ if ${ac_cv_lib_m_main+:} false; then :
   $as_echo_n "(cached) " >&6
 else
   ac_check_lib_save_LIBS=$LIBS
-LIBS="-lm  $LIBS"
+LIBS="$LIBS"
 cat confdefs.h - <<_ACEOF >conftest.$ac_ext
 /* end confdefs.h.  */
 
@@ -8602,7 +8602,7 @@ if test "x$ac_cv_lib_m_main" = xyes; then :
 #define HAVE_LIBM 1
 _ACEOF
 
-  LIBS="-lm $LIBS"
+  LIBS="$LIBS"
 
 else
   as_fn_error $? "lib math is needed" "$LINENO" 5
-- 
2.7.0

