#!/bin/bash
ABL_TARGET_LD_SO_PATH=/lib64/ld-linux-x86-64.so.2
ABL_ARCH_DIR=x86_64-linux-gnu
#some contents is generated when build package

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
    echo "    All the library files, which packed with additional-base-lib,"
    echo "    are taken from one GNU/Linux distribution. You may found information"
    echo "    in package control files. The scripts theirselves were created by"
    echo "    CongTianKong <https://gitee.com/CongTianKong>. 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
    echo "    There is a more convenient script to handle both normal executable"
    echo "    and appimage, called ablrun."
    echo "    usage: ablrun [command [arguments ...]]"
    exit
fi

ABL_LD_SO_PATH=`readlink -e $ABL_TARGET_LD_SO_PATH`
ABL_LIBC_SO_PATH=`readlink -e /lib/${ABL_ARCH_DIR}/libc.so.6`

ABL_LIB_ROOT="/opt/apps/com.gitee.congtiankong.additional-base-lib/files/additional-base-lib"

if [ "$LD_LIBRARY_PATH" = "" ]
then
    ABL_LIBRARY_PATH="$ABL_LIB_ROOT"
else
    ABL_LIBRARY_PATH="$LD_LIBRARY_PATH;$ABL_LIB_ROOT"
fi

exec bwrap \
--dev-bind / / \
--bind $ABL_LIB_ROOT/"$ABL_TARGET_LD_SO_PATH" "$ABL_LD_SO_PATH" \
--bind $ABL_LIB_ROOT/libc.so.6 "$ABL_LIBC_SO_PATH" \
--bind $ABL_LIB_ROOT/ldd /usr/bin/ldd \
--setenv LD_LIBRARY_PATH "$ABL_LIBRARY_PATH" \
-- "$@"
