TODO update, fix tux doesn't stop at igloo anymore
[supertux.git] / lib / app / setup.cpp
index a0b4b90..c7972c4 100644 (file)
@@ -17,6 +17,8 @@
 //  along with this program; if not, write to the Free Software
 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
+#include <config.h>
+
 #include <cassert>
 #include <cstdio>
 #include <iostream>
 
 #include <cctype>
 
-#include "../app/globals.h"
-#include "../app/setup.h"
-#include "../video/screen.h"
-#include "../video/surface.h"
-#include "../gui/menu.h"
-#include "../utils/configfile.h"
-#include "../audio/sound_manager.h"
-#include "../app/gettext.h"
+#include "globals.h"
+#include "setup.h"
+#include "video/screen.h"
+#include "video/surface.h"
+#include "gui/menu.h"
+#include "utils/configfile.h"
+#include "audio/sound_manager.h"
+#include "gettext.h"
 
 using namespace SuperTux;
 
@@ -65,7 +67,7 @@ void seticon(void);
 void usage(char * prog, int ret);
 
 /* Does the given file exist and is it accessible? */
-int FileSystem::faccessible(const std::string& filename)
+bool FileSystem::faccessible(const std::string& filename)
 {
   struct stat filestat;
   if (stat(filename.c_str(), &filestat) == -1)
@@ -82,7 +84,7 @@ int FileSystem::faccessible(const std::string& filename)
 }
 
 /* Can we write to this location? */
-int FileSystem::fwriteable(const std::string& filename)
+bool FileSystem::fwriteable(const std::string& filename)
 {
   FILE* fi;
   fi = fopen(filename.c_str(), "wa");
@@ -95,7 +97,7 @@ int FileSystem::fwriteable(const std::string& filename)
 }
 
 /* Makes sure a directory is created in either the SuperTux home directory or the SuperTux base directory.*/
-int FileSystem::fcreatedir(const std::string& relative_dir)
+bool FileSystem::fcreatedir(const std::string& relative_dir)
 {
   std::string path = st_dir + "/" + relative_dir + "/";
   if(mkdir(path.c_str(),0755) != 0)
@@ -250,16 +252,31 @@ std::set<std::string> FileSystem::dfiles(const std::string& rel_path, const  std
   return sdirs;
 }
 
-void Setup::info(const std::string& _package_name, const std::string& _package_symbol_name, const std::string& _package_version)
+std::string FileSystem::dirname(const std::string& filename)
+{
+  std::string::size_type p = filename.find_last_of('/');
+  if(p == std::string::npos)                              
+    return "";
+  
+  return filename.substr(0, p+1);
+}
+
+void Setup::init(const std::string& _package_name,
+        const std::string& _package_symbol_name,
+        const std::string& _package_version)
 {
-package_name = _package_name;
-package_symbol_name = _package_symbol_name;
-package_version = _package_version;
+  package_name = _package_name;
+  package_symbol_name = _package_symbol_name;    
+  package_version = _package_version;
+    
+  directories();
+  dictionary_manager.add_directory(datadir + "/locale");
+  dictionary_manager.set_charset("iso8859-1");
 }
 
 /* --- SETUP --- */
 /* Set SuperTux configuration and save directories */
-void Setup::directories(void)
+void Setup::directories()
 {
   std::string home;
   /* Get home directory (from $HOME variable)... if we can't determine it,
@@ -272,10 +289,9 @@ void Setup::directories(void)
   st_dir = home + "/." + package_symbol_name;
 
   /* Remove .supertux config-file from old SuperTux versions */
-  if(FileSystem::faccessible(st_dir))
-    {
-      remove(st_dir.c_str());
-    }
+  if(FileSystem::faccessible(st_dir)) {
+    remove(st_dir.c_str());
+  }
 
   st_save_dir = st_dir + "/save";
 
@@ -285,6 +301,12 @@ void Setup::directories(void)
 
   mkdir((st_dir + "/levels").c_str(), 0755);
 
+  // try current directory as datadir
+  if(datadir.empty()) {
+     if(FileSystem::faccessible("./data/credits.txt"))
+          datadir = "./data/";
+  }
+
   // User has not that a datadir, so we try some magic
   if (datadir.empty())
     {
@@ -300,23 +322,25 @@ void Setup::directories(void)
         {
          std::string exedir = std::string(dirname(exe_file)) + "/";
          
-          datadir = exedir + "../data"; // SuperTux run from source dir
+          datadir = exedir + "./data/"; // SuperTux run from source dir
           if (access(datadir.c_str(), F_OK) != 0)
             {
-             datadir = exedir + "../../data";  //SuperTux run from source dir (with libtool script)
+             datadir = exedir + "../../../../data/";  //SuperTux run from source dir (with libtool script)
              
              if (access(datadir.c_str(), F_OK) != 0)
              {
-              datadir = exedir + "../share/" + package_symbol_name; // SuperTux run from PATH
+              datadir = exedir + "../share/" + package_symbol_name + "/"; // SuperTux run from PATH
               if (access(datadir.c_str(), F_OK) != 0) 
                 { // If all fails, fall back to compiled path
-                  datadir = DATA_PREFIX; 
+                  datadir = DATA_PREFIX;
+                  datadir += "/"; 
                 }
              }
             }
         }
 #else
-  datadir = DATA_PREFIX;
+      datadir = DATA_PREFIX;
+      datadir += "/";
 #endif
     }
   printf("Datadir: %s\n", datadir.c_str());