#!/usr/bin/bash

if [ "$*" = "" ]
then
    echo "usage: $0 [command [arguments ...]]"
    echo "    The script is part of additional-base-lib. The package provides a"
    echo "    simple way to solve the compatible problem between application and"
    echo "    glibc, powered by bubblewrap."
    echo "    All the library files, which distributed with additional-base-lib,"
    echo "    are taken from debian bookworm. The script itself create by enforcee"
    echo "    from deepin forum <https://bbs.deepin.org/>. There's no lisence"
    echo "    nor copyright restriction with The script. Feel free to deal with."
    echo
    echo "    If you're going to run appimage with additional-base-lib, you may"
    echo "    need ablrun-appimage."
    echo "    There is a simpler script to handle both normal executable and"
    echo "    appimage, called ablrun. usage: ablrun [command [arguments ...]]"
    exit
fi

ABL_LDSO_PATH=`readlink -e /lib64/ld-linux-x86-64.so.2`
ABL_LIBC_PATH=`readlink -e /usr/lib/x86_64-linux-gnu/libc.so.6`

if [ "$LD_LIBRARY_PATH" = "" ]
then
    ABL_LIBRARY_PATH="/usr/lib/x86_64-linux-gnu/additional-base-lib/"
else
    ABL_LIBRARY_PATH="$ABL_LIBRARY_PATH;/usr/lib/x86_64-linux-gnu/additional-base-lib"
fi

exec bwrap \
--dev-bind / / \
--bind /usr/lib/x86_64-linux-gnu/additional-base-lib/ld-linux-x86-64.so.2 "$ABL_LDSO_PATH" \
--bind /usr/lib/x86_64-linux-gnu/additional-base-lib/libc.so.6 "$ABL_LIBC_PATH" \
--bind /usr/lib/x86_64-linux-gnu/additional-base-lib/ldd /usr/bin/ldd \
--setenv LD_LIBRARY_PATH "$ABL_LIBRARY_PATH" \
-- "$@"
