#!/bin/sh
set -eu

mkdir -p /run/dbus /var/log/supervisor
rm -f /run/dbus/pid
rm -f /tmp/.X1-lock /tmp/.X11-unix/X1
if ! grep -Eq '^[0-9a-fA-F]{32}$' /etc/machine-id 2>/dev/null; then
  rm -f /etc/machine-id
fi
dbus-uuidgen --ensure=/etc/machine-id 2>/dev/null || true

mkdir -p /home/wechat/.config/wx-bot-client /home/wechat/.local/share/wx-bot-sdk
chown -R wechat:wechat /home/wechat

SDK_BINARY=/opt/wx-bot-client/wx-bot-client
if [ -x "$SDK_BINARY" ]; then
  if ! command -v setcap >/dev/null 2>&1; then
    echo "[entrypoint] setcap unavailable; install libcap2-bin" >&2
    exit 1
  fi
  setcap cap_sys_ptrace=ep "$SDK_BINARY"
fi

if [ -w /proc/sys/kernel/yama/ptrace_scope ]; then
  echo 0 > /proc/sys/kernel/yama/ptrace_scope 2>/dev/null || true
fi

exec /usr/bin/supervisord -c /etc/supervisor/supervisord.conf
