From 60f4c8f517445fd2e2ab11677bfad992132686df Mon Sep 17 00:00:00 2001
From: nicoo <nicoo@mur.at>
Date: Wed, 1 May 2024 13:17:52 +0000
Subject: [PATCH 1/2] Rely on mpv to know where to store application data &
 cache

This is the most platform-independent way, and fixes things
for users who have a global scripts directory. (see #66)

Closes #17
---
 sponsorblock.lua | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/sponsorblock.lua b/sponsorblock.lua
index 96bfb24..91b2c31 100644
--- a/sponsorblock.lua
+++ b/sponsorblock.lua
@@ -92,11 +92,16 @@ end
 options.local_database = false
 
 local utils = require "mp.utils"
-scripts_dir = mp.find_config_file("scripts")
+local function expand_path(path)
+   return mp.command_native({"expand-path", path})
+end
+
+local sponsorblock = mp.find_config_file("scripts/sponsorblock_shared/sponsorblock.py")
+local uid_path = expand_path("~~state/sponsorblock.txt")
+local database_file = options.local_database and expand_path("~~cache/sponsorblock.db") or ""
+mp.msg.debug("uid_path: " .. uid_path)
+mp.msg.debug("database_file: " .. database_file)
 
-local sponsorblock = utils.join_path(scripts_dir, "sponsorblock_shared/sponsorblock.py")
-local uid_path = utils.join_path(scripts_dir, "sponsorblock_shared/sponsorblock.txt")
-local database_file = options.local_database and utils.join_path(scripts_dir, "sponsorblock_shared/sponsorblock.db") or ""
 local youtube_id = nil
 local ranges = {}
 local init = false

From cce913bf3887c2b4a1bd862c6204b13640d5df96 Mon Sep 17 00:00:00 2001
From: nicoo <nicoo@mur.at>
Date: Wed, 1 May 2024 13:25:09 +0000
Subject: [PATCH 2/2] Automatically migrate files to new locations

---
 sponsorblock.lua | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/sponsorblock.lua b/sponsorblock.lua
index 91b2c31..4ac5f54 100644
--- a/sponsorblock.lua
+++ b/sponsorblock.lua
@@ -562,6 +562,18 @@ function submit_segment(category)
     end
 end
 
+-- Automatically migrate files if needed
+for new, old in pairs({
+      [uid_path]      = expand_path("~~/scripts/sponsorblock_shared/sponsorblock.txt"),
+      [database_file] = expand_path("~~/scripts/sponsorblock_shared/sponsorblock.db"),
+}) do
+   mp.msg.debug(old .. " → " .. new)
+   if file_exists(old) and not file_exists(new) and new ~= "" then
+      mg.msg.info("Migrating " .. old)
+      os.rename(old, new)
+   end
+end
+
 mp.register_event("file-loaded", file_loaded)
 mp.add_key_binding("g", "set_segment", set_segment)
 mp.add_key_binding("G", "submit_segment", submit_segment)
