#!/bin/bash

# Script to create a portable bundle for GTK2 Media Player
# Gathering all shared library dependencies into a single folder.

# Ensure we are in the project root
cd "$(dirname "$0")/.."

APP_NAME="gtk2-mpv-player"
BUNDLE_DIR="${APP_NAME}-portable"

echo "Creating portable bundle in ${BUNDLE_DIR}..."

# 1. Clean and Build
make clean
make

if [ ! -f "$APP_NAME" ]; then
    echo "Build failed. Exiting."
    exit 1
fi

# 2. Create directory structure
rm -rf "$BUNDLE_DIR"
mkdir -p "$BUNDLE_DIR/bin"
mkdir -p "$BUNDLE_DIR/lib"

# 3. Copy executable
cp "$APP_NAME" "$BUNDLE_DIR/bin/"

# 4. Gather dependencies
echo "Gathering dependencies (Lean Edition)..."

# Aggressively exclude everything not strictly required for basic playback.
# Pruning: Encoders (x265, rav1e, svt), Math (lapack, blas), TTS (flite), 
# Niche codecs (codec2, sphinx), and heavy rendering (placebo, caca).
EXCLUDES="libc\.so|libm\.so|libdl\.so|librt\.so|libpthread\.so|libutil\.so|libnsl\.so|libresolv\.so|libgcc_s\.so|libX11\.so|libXext\.so|libXrender\.so|libXinerama\.so|libXi\.so|libXrandr\.so|libXcursor\.so|libXcomposite\.so|libXdamage\.so|libXfixes\.so|libXau\.so|libXdmcp\.so|libxcb.*|libz\.so|libGL.*|libEGL\.so|libdrm\.so|libwayland.*|libasound\.so|libpulse.*|libdbus-1\.so|libudev\.so|libsystemd\.so|libgpg-error\.so|libgcrypt\.so|liblzma\.so|libbz2\.so|libexpat\.so|libuuid\.so|libfontconfig\.so|libfreetype\.so|libcap\.so|libflite.*|liblapack.*|libgfortran.*|libcodec2.*|libsphinx.*|libpocketsphinx.*|librav1e.*|libSvtAv1Enc.*|libblas.*|libdav1d.*|libcrypto\.so|libssl\.so|libgnutls\.so|libidn2\.so|libtasn1\.so|libunistring\.so|libgmp\.so|libhogweed\.so|libnettle\.so|libp11-kit\.so|libxml2\.so|libdb-.*\.so|libarchive\.so|libzstd\.so|liblzma\.so|libbz2\.so|libpcre.*|libselinux.*|libmount\.so|libblkid\.so|libffi\.so|libreadline\.so|libtinfo\.so|libncurses.*|libplacebo.*|libcaca.*|libopenmpt.*|libvulkan.*|libOpenCL.*|libvpl.*|libmysofa.*|librsvg.*|libx265.*"

TEMP_LIBS=$(mktemp)

# Initial list from the binary and libmpv
ldd "$APP_NAME" | grep "=> /" | awk '{print $3}' >> "$TEMP_LIBS"
if [ -d "lib" ]; then
    cp -nv lib/*.so* "$BUNDLE_DIR/lib/" 2>/dev/null
    find lib -name "*.so*" -exec ldd {} \; | grep "=> /" | awk '{print $3}' >> "$TEMP_LIBS"
fi

# Function to filter and copy libraries
copy_libs() {
    sort -u "$1" | while read -r LIB; do
        if [ -f "$LIB" ]; then
            LIB_BASE=$(basename "$LIB")
            if ! echo "$LIB_BASE" | grep -qE "$EXCLUDES"; then
                cp -nv "$LIB" "$BUNDLE_DIR/lib/" 2>/dev/null
            fi
        fi
    done
}

# First pass
copy_libs "$TEMP_LIBS"

# Second pass: check dependencies of the copied libraries
TEMP_LIBS_2=$(mktemp)
find "$BUNDLE_DIR/lib" -name "*.so*" -exec ldd {} \; | grep "=> /" | awk '{print $3}' >> "$TEMP_LIBS_2"
copy_libs "$TEMP_LIBS_2"

rm "$TEMP_LIBS" "$TEMP_LIBS_2"



# 5. Strip symbols to save space
echo "Stripping symbols..."
strip --strip-unneeded "$BUNDLE_DIR/bin/$APP_NAME"
find "$BUNDLE_DIR/lib" -name "*.so*" -exec strip --strip-unneeded {} \; 2>/dev/null




# 5. Create launch script
cat > "$BUNDLE_DIR/launch.sh" <<EOF
#!/bin/bash
DIR="\$(cd "\$(dirname "\${BASH_SOURCE[0]}")" && pwd)"
export LD_LIBRARY_PATH="\$DIR/lib:\$LD_LIBRARY_PATH"
# Ensure GTK2 can find its modules if needed, though most are included in libs
exec "\$DIR/bin/$APP_NAME" "\$@"
EOF

chmod +x "$BUNDLE_DIR/launch.sh"

echo "------------------------------------------------"
echo "Portable bundle created in ${BUNDLE_DIR}"
echo "To run, use: cd ${BUNDLE_DIR} && ./launch.sh"
echo "------------------------------------------------"
