Add setupcheck script
authorChristian Weiske <cweiske@cweiske.de>
Sun, 4 Jun 2023 11:33:18 +0000 (13:33 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Sun, 4 Jun 2023 11:33:18 +0000 (13:33 +0200)
src/ProfileDb.php
www/setupcheck.php [new file with mode: 0644]

index 5eed7262d1bbd16b246ec69f8f121b493f840a56..29f3eda13eb111b2b049797019b804cef678771d 100644 (file)
@@ -10,7 +10,7 @@ class ProfileDb
 
     public function __construct()
     {
-        $dbFile = dirname(__FILE__, 2) . '/data/profiles.sqlite3';
+        $dbFile = static::getDbFilePath();
         $this->db = new PDO(
             'sqlite:' . $dbFile,
             '', '',
@@ -22,6 +22,11 @@ class ProfileDb
         $this->createTablesIfNeeded();
     }
 
+    public static function getDbFilePath()
+    {
+        return dirname(__FILE__, 2) . '/data/profiles.sqlite3';
+    }
+
     public function getProfileByHardwareId(string $hwId): ?Profile
     {
         $stmt = $this->db->prepare('SELECT * FROM gamesticks WHERE hwId = :id');
diff --git a/www/setupcheck.php b/www/setupcheck.php
new file mode 100644 (file)
index 0000000..715f6b0
--- /dev/null
@@ -0,0 +1,62 @@
+<?php
+/**
+ * Check if everything is fine
+ */
+header('HTTP/1.0 500 Internal Server Error');
+
+function error($msg)
+{
+    header('HTTP/1.0 500 Internal Server Error');
+    header('Content-Type: text/plain');
+    echo 'Error: ' . $msg . "\n";
+    exit(1);
+}
+
+$configFile = __DIR__ . '/../config.php';
+if (!file_exists($configFile)) {
+    error('config.php missing. Copy config.php.dist to config.php');
+}
+
+require_once $configFile;
+require_once __DIR__ . '/../src/ProfileDb.php';
+
+$dbFile = ProfileDb::getDbFilePath();
+if (!file_exists($dbFile)) {
+    $dbFileDir = dirname($dbFile);
+    if (!is_writable($dbFileDir)) {
+        error('Database file directory is not writable: ' . $dbFileDir);
+    }
+}
+
+if (isset($GLOBALS['popuplarTxtFile'])
+    && strlen($GLOBALS['popuplarTxtFile'])
+    && !file_exists($GLOBALS['popuplarTxtFile'])
+) {
+    error('Popular games file does not exist: ' . $GLOBALS['popuplarTxtFile']);
+}
+
+if (isset($GLOBALS['featuredFile'])
+    && strlen($GLOBALS['featuredFile'])
+    && !file_exists($GLOBALS['featuredFile'])
+) {
+    error('Featured games file does not exist: ' . $GLOBALS['featuredFile']);
+}
+
+$cacheDir = __DIR__ . '/../cache/';
+$appsCacheFile         = $cacheDir . 'connect-apps.min.json';
+$featuredAgesCacheFile = $cacheDir . 'connect-featured-ages.min.json';
+
+if (!file_exists($appsCacheFile)) {
+    error('Apps cache file does not exist. Run bin/generate-apps-cache.php');
+}
+if (!file_exists($featuredAgesCacheFile)) {
+    error('Featured ages cache file does not exist. Run bin/generate-apps-cache.php');
+}
+
+
+//FIXME: api tests
+
+header('HTTP/1.0 200 OK');
+header('Content-Type: text/plain');
+echo "Everything looks fine\n";
+?>