From 488721e62e3c0b390768fa71060517411178495d Mon Sep 17 00:00:00 2001 From: zwim <36999612+zwim@users.noreply.github.com> Date: Fri, 19 Jun 2020 21:23:47 +0200 Subject: [PATCH] [Android] Add support for custom startup scripts (#6275) Add the possibility to run *.sh scripts: After an update of koreader all *.sh scripts in /sdcard/koreader/scripts.afterupdate are executed. On every start of koreader run all *.sh scripts in /sdcard/koreader/scripts.always --- Makefile | 4 +++- platform/android/llapp_main.lua | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index e09e08c8f..726216039 100644 --- a/Makefile +++ b/Makefile @@ -94,6 +94,8 @@ endif ifdef ANDROID cd $(INSTALL_DIR)/koreader && \ ln -sf ../../$(ANDROID_DIR)/*.lua . + @echo "[*] Install afterupdate marker" + @echo "# If this file is here, there are no afterupdate scripts in /sdcard/koreader/scripts/afterupdate." > $(INSTALL_DIR)/koreader/afterupdate.marker endif ifdef WIN32 @echo "[*] Install runtime libraries for win32..." @@ -105,7 +107,7 @@ endif @# purge deleted plugins for d in $$(ls $(INSTALL_DIR)/koreader/plugins); do \ test -d plugins/$$d || rm -rf $(INSTALL_DIR)/koreader/plugins/$$d ; done - @echo "[*] Installresources" + @echo "[*] Install resources" $(RCP) -pL resources/fonts/. $(INSTALL_DIR)/koreader/fonts/. install -d $(INSTALL_DIR)/koreader/{screenshots,data/{dict,tessdata},fonts/host,ota} ifeq ($(IS_RELEASE),1) diff --git a/platform/android/llapp_main.lua b/platform/android/llapp_main.lua index 8ad60ae3e..698596908 100644 --- a/platform/android/llapp_main.lua +++ b/platform/android/llapp_main.lua @@ -1,6 +1,7 @@ local android = require("android") android.dl.library_path = android.dl.library_path .. ":" .. android.dir .. "/libs" +local lfs = require("libs/libkoreader-lfs") local ffi = require("ffi") local dummy = require("ffi/posix_h") local C = ffi.C @@ -15,6 +16,37 @@ end -- path to primary external storage partition local path = android.getExternalStoragePath() +-- run user shell scripts +local function runUserScripts(dir) + for entry in lfs.dir(dir) do + if entry ~= "." and entry ~= ".." then + local fullpath = dir .. "/" .. entry + local mode = lfs.attributes(fullpath).mode + if mode == "file" and fullpath:match(".sh$") then + android.execute("sh", fullpath, path .. "/koreader", android.dir) + elseif mode == "directory" then + runUserScripts(fullpath) -- recurse into next directory + end + end + end +end + +-- scripts executed once after an update of koreader +local run_once_scripts = path .. "/koreader/scripts.afterupdate" +if lfs.attributes(run_once_scripts, "mode") == "directory" then + local afterupdate_marker = android.dir .. "/afterupdate.marker" + if lfs.attributes(afterupdate_marker, "mode") ~= nil then + runUserScripts(run_once_scripts) + android.execute("rm", afterupdate_marker) + end +end + +-- scripts executed every start of koreader +local run_always_scripts = path .. "/koreader/scripts.always" +if lfs.attributes(run_always_scripts, "mode") == "directory" then + runUserScripts(run_always_scripts) +end + -- run koreader patch before koreader startup pcall(dofile, path.."/koreader/patch.lua")