From 7d73690bd77d19b814787e300fe55385f792b052 Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Tue, 20 Oct 2020 20:30:13 +0200
Subject: [PATCH] [build] trying to make the AppImage works on other computers

---
 Aegisub/AppRun                 |  6 ++-
 Aegisub/{ => usr}/lib/.gitkeep |  0
 Makefile.target                |  2 +-
 tools/copy-libs.lua            | 77 ++++++++++++++++++++++++++++++++++
 tools/copy_libs.bash           | 42 -------------------
 5 files changed, 82 insertions(+), 45 deletions(-)
 rename Aegisub/{ => usr}/lib/.gitkeep (100%)
 mode change 100644 => 100755
 create mode 100755 tools/copy-libs.lua
 delete mode 100755 tools/copy_libs.bash

diff --git a/Aegisub/AppRun b/Aegisub/AppRun
index 58a33fc7e..22016b74a 100755
--- a/Aegisub/AppRun
+++ b/Aegisub/AppRun
@@ -1,5 +1,7 @@
 #!/bin/sh
 
-SELF_DIR=$(dirname "$0")
-export LD_LIBRARY_PATH="$SELF_DIR/lib"
+SELF_DIR=$(dirname $(readlink -f "$0"))
+export LC_ALL="en_US.UTF-8"
+export LD_LIBRARY_PATH="$SELF_DIR/usr/lib:$LD_LIBRARY_PATH"
+ldd "$SELF_DIR"/aegisub
 exec "$SELF_DIR"/aegisub
diff --git a/Aegisub/lib/.gitkeep b/Aegisub/usr/lib/.gitkeep
old mode 100644
new mode 100755
similarity index 100%
rename from Aegisub/lib/.gitkeep
rename to Aegisub/usr/lib/.gitkeep
diff --git a/Makefile.target b/Makefile.target
index 38d429f07..b06f6993a 100644
--- a/Makefile.target
+++ b/Makefile.target
@@ -97,7 +97,7 @@ tags:
 
 appimage: src/aegisub
 	cp $^ Aegisub/aegisub
-	./tools/copy_libs.bash --exec=Aegisub/aegisub --dest=Aegisub/lib
+	./tools/copy-libs.lua Aegisub/aegisub Aegisub/usr/lib
 	./tools/appimagetool.AppImage Aegisub
 
 # The actual build rules
diff --git a/tools/copy-libs.lua b/tools/copy-libs.lua
new file mode 100755
index 000000000..0f2118b47
--- /dev/null
+++ b/tools/copy-libs.lua
@@ -0,0 +1,77 @@
+#!/usr/bin/env lua
+-- Needs awk and a shell (usually bash / sh) and ldd, which is a Linux utility.
+-- Thus run only this script on a Linux env or where the .so files can be found
+-- with the ldd command.
+
+if not arg or #arg ~= 2 then os.exit(1) end
+
+io.stdout:write(string.format('Running for file %s\n', arg[1]))
+io.stdout:write(string.format('Dest folder is %s\n',   arg[2]))
+
+-- Libs tables
+LIBS_WRITE = {}
+LIBS_READ  = {}
+
+-- Capture the output of a command
+function os.capture(cmd)
+    local f = assert(io.popen(cmd, 'r'))
+    local s = assert(f:read('*a'))
+    f:close()
+    return s
+end
+
+-- Simple copy of a table, it doesn't work with metatables and recursive tables
+function copy_simple(obj)
+    if type(obj) ~= 'table' then return obj end
+    local res = {}
+    for k, v in pairs(obj) do res[copy_simple(k)] = copy_simple(v) end
+    return res
+end
+
+-- Add the content of t2 in t1
+function concatenate(t1, t2)
+    for name, file in pairs(t2) do
+        t1[name] = file
+    end
+end
+
+-- Get the number of elements in a table
+function tablelength(T)
+    local count = 0
+    for _ in pairs(T) do count = count + 1 end
+    return count
+end
+
+-- The ldd command line utility, but in lua
+function ldd(file)
+    local libs = {}
+    local cmd  = string.format('ldd %s | awk \'$2 = "=>" && $3 { print $1 " " $3 }\'', arg[1])
+    local out  = os.capture(cmd)
+
+    for name, file in out:gmatch("([^\n ]*) ([^\n ]*)[\n]") do
+        libs[name] = file
+    end
+
+    return libs
+end
+
+-- Add NEEDS for each lib while there are some added. It's like a dataflow algorithme.
+LIBS_WRITE = ldd(arg[1])
+while tablelength(LIBS_READ) ~= tablelength(LIBS_WRITE) do
+    LIBS_READ = copy_simple(LIBS_WRITE)
+
+    for name, file in pairs(LIBS_READ) do
+        concatenate(LIBS_WRITE, ldd(file))
+    end
+end
+
+io.stdout:write(string.format("Cleaning %s", arg[2]))
+os.execute(string.format("rm -f %s/*", arg[2]))
+
+for name, file in pairs(LIBS_WRITE) do
+    io.stdout:write(string.format("%s => %s/%s\n", file, arg[2], name))
+    local cmd = string.format('cp %s %s/%s && chmod 555 %s/%s',
+                              file, arg[2], name, arg[2], name)
+    os.execute(cmd)
+end
+io.stdout:write(string.format("Copied %d libraries to %s\n", tablelength(LIBS_WRITE), arg[2]))
diff --git a/tools/copy_libs.bash b/tools/copy_libs.bash
deleted file mode 100755
index ab133c61b..000000000
--- a/tools/copy_libs.bash
+++ /dev/null
@@ -1,42 +0,0 @@
-#!/bin/bash
-
-EXEC=""
-DEST=""
-
-function print_help {
-cat << EOF
-Usage: $0 --exec=* --dest=* [--help]
-    --help  Display this help message
-    --exec  Specify which dynamic executable to inspect and get the .so files from
-    --dest  In which folder the .so files must be copied
-EOF
-}
-
-for arg in "$@"
-do
-    case $arg in
-        --help|-h)
-            print_help
-            exit
-            ;;
-        --exec=*)
-            EXEC=`echo A$arg | sed -e 's/^A--exec=//g'`
-            ;;
-        --dest=*)
-            DEST=`echo A$arg | sed -e 's/^A--dest=//g'`
-            ;;
-    esac
-done
-
-if test "x$EXEC" = "x" -o "x$DEST" = "x"
-then
-    print_help
-    exit
-fi
-
-ldd "$EXEC" \
-    | awk '$2 = "=>" && $3 { print $1 " " $3 }' 2>&1 \
-    | while read NAME FILE
-do
-    cp $FILE $DEST/`basename $FILE`
-done
-- 
GitLab