#!/bin/bash
if [ "$(id -u)" != "0" ]; then
    echo "当前用户不是 root 用户，退出"
    exit
fi

curdir=`realpath $0`
parent_dir=`dirname $curdir`
pparent_dir=`dirname $parent_dir`
ppparent_dir=`dirname $pparent_dir`
PKGNAME=`basename $ppparent_dir`
export PACKAGE_NAME=$PKGNAME
chrootEnvPath=/opt/apps/$PKGNAME/files/bookworm-env


if [ ! -e $chrootEnvPath/finish.flag ];then

if [ "$(id -u)" = "0" ]; then
    `dirname $chrootEnvPath`/bin/bookworm-init
else
pkexec `dirname $chrootEnvPath`/bin/bookworm-init
fi


fi
non_root_user=$(who  | awk '{print $1}' | head -n 1)
uid=$(id -u $non_root_user)


#### This part is for args pharm
if [ "$1" = "" ];then
container_command="bash"
else
container_command="$1"
shift
for arg in "$@"; do
        arg="$(echo "${arg}x" | sed 's|'\''|'\'\\\\\'\''|g')"
        arg="${arg%x}"
        container_command="${container_command} '${arg}'"
done
fi


bwrap --dev-bind / / \
  bwrap \
  --setenv LANG "$LANG" \
  --setenv LC_COLLATE "$LC_COLLATE" \
  --setenv LC_CTYPE "$LC_CTYPE" \
  --setenv LC_MONETARY "$LC_MONETARY" \
  --setenv LC_MESSAGES "$LC_MESSAGES" \
  --setenv LC_NUMERIC "$LC_NUMERIC" \
  --setenv LC_TIME "$LC_TIME" \
  --setenv LC_ALL "$LC_ALL" \
  --setenv PULSE_SERVER /run/user/$uid/pulse/native \
  --setenv PATH /flamescion-container-tools/bin-override:$PATH \
  --dev-bind $chrootEnvPath/ / \
  --dev-bind /media /media \
  --dev-bind /tmp /tmp \
  --dev /dev  \
  --dev-bind /dev/dri /dev/dri  \
  --proc /proc  \
  --dev-bind /sys /sys  \
  --dev-bind /run /run  \
  --dev-bind-try /run/user/$uid/pulse /run/user/$uid/pulse  \
  --dev-bind / /host \
  --ro-bind /usr/share/themes /usr/local/share/themes  \
  --ro-bind /usr/share/icons /usr/local/share/icons  \
  --ro-bind /usr/share/fonts /usr/local/share/fonts  \
  --hostname bookworm-compatibility-mode \
  --unshare-uts \
  --dev-bind /etc/resolv.conf /etc/resolv.conf \
  --cap-add CAP_SYS_ADMIN \
  --dev-bind /home /home \
  bash -c "${container_command}"



