Description: Massage Sauce for Filesystem Hierarchy Standard
 On Linux, game can now read data files from /usr/share/darthinvaders.
 This may vary by prefix.
 Also it has (per-user due to convention) writable high scores, which
 will be kept in /home/.config or similar, if it exists.

Origin: <vendor|upstream|other>, <url of original patch>
Forwarded: not-needed
Last-Update: 2020-08-07

--- darthinvaders-1.0.orig/Test.cpp
+++ darthinvaders-1.0/Test.cpp
@@ -13,6 +13,13 @@
 #include <iostream>
 #include <fstream>
 
+#ifdef __linux__
+#include <unistd.h>
+#include <sys/types.h>
+#include <pwd.h>
+#include <libgen.h>
+#endif
+
 #include "Enemy.h"
 #include "Spaceship.h"
 #include "Bullet.h"
@@ -79,23 +86,61 @@ BackGround	BG;
 BackGround	MG;
 BackGround	FG;
 BackGround	MM;
-
+std::string confdir;
 
 int main(void)
 {
+  if (!al_init()) {
+    al_show_native_message_box(NULL, NULL, NULL, "Could not initialize allegro",
+                               NULL, NULL);
+                return -1;
+        }
+
+#ifdef __linux__
+   ALLEGRO_PATH* exepath = al_get_standard_path(ALLEGRO_EXENAME_PATH);
+   al_set_path_filename(exepath , NULL);
+   al_change_directory(al_path_cstr(exepath, '/'));
+   al_change_directory((string{dirname(strdup(al_path_cstr(exepath, '/')))} +"/share/darthinvaders").c_str());
+   al_destroy_path(exepath);
+
+	char const* cf = getenv("XDG_CONFIG_HOME");
+        if (!cf) {
+		char const* hd = getenv("HOME");
+		std::string homedir = getenv("HOME");
+                if (!hd) {
+                        homedir = getpwuid(getuid())->pw_dir;
+                } else {
+	                confdir = std::string(hd) + "/.config";
+        }
+        } else {
+		confdir = getenv("XDG_CONFIG_HOME");
+	}
+
+	confdir += "/";
+
+	// if scores file is not user's dir, copy it in.
+	// it'll still degrade  gracefully if you can't write here 
+	// Filesystem standard says to put highscores in /var/games but then new users have no write perms??
+	std::string path =  confdir +  "/scores.txt";
+	if (access (confdir.c_str(), F_OK) == 0 && access(path.c_str(), F_OK) != 0 && access (confdir.c_str(), W_OK) == 0) {
+        	ifstream source("scores.txt", ios::binary);
+        	ofstream dest(path, ios::binary);
+
+	        dest << source.rdbuf();
+
+	        source.close();
+	        dest.close();
+	}
+#else
+	confdir = "";
+#endif
 	readScores();																					//reads in scores from textfile
 	setEnemy();
 	enemyBullet.isFriendly = false;
 	srand((unsigned)time(NULL));
-
 	//variables
 
 
-	if (!al_init())
-	{
-		al_show_native_message_box(NULL, NULL, NULL, "Could not initialize allegro", NULL, NULL);
-		return -1;
-	}
 
 	//Allegro variables
 	ALLEGRO_DISPLAY *DISPLAY = NULL;
@@ -942,7 +987,7 @@ void DrawAnimation(ALLEGRO_BITMAP *pic,
 
 void readScores()
 {
-	ifstream myfile("scores.txt");
+	ifstream myfile(confdir+"scores.txt");
 	string line;
 	Highscores temp;
 	int count = 0;
@@ -976,7 +1021,7 @@ void writeScore()
 	arrScores[pos + 1].score = score;
 	arrScores[pos + 1].name = nameString;
 
-	ofstream OutputFile("scores.txt");
+	ofstream OutputFile(confdir+"scores.txt");
 	for (i = 0; i < 9; i++)
 	{
 		if (arrScores[i].name == "")
