#!/bin/bash
#set -x

WHO=${0##*/}
WHO=${WHO/hotpot/wine}

if [ -f "$HOTPOT_WINE" ]; then
	WINE_PATH=$(dirname "$(dirname ${HOTPOT_WINE})")
fi

if [ "$WINE_PATH" == "" ]; then
    if [ -f "/opt/winux/hotpot9/usr/local/bin/wine" ]; then
        WINE_PATH=/opt/winux/hotpot9/usr/local
    fi
fi

if [ "$WINE_PATH" == "" ]; then
    if [ -f "/opt/winux/hotpot8/usr/local/bin/wine" ]; then
        WINE_PATH=/opt/winux/hotpot8/usr/local
    fi
fi

if [ "$WINE_PATH" == "" ]; then
    if [ -f "/opt/winux/hotpot7/usr/local/bin/wine" ]; then
        WINE_PATH=/opt/winux/hotpot7/usr/local
    fi
fi

if [ "$WINE_PATH" == "" ]; then
    if [ -f "/opt/winux/hotpot6/usr/local/bin/wine" ]; then
        WINE_PATH=/opt/winux/hotpot6/usr/local
    fi
fi

if [ "$WINE_PATH" == "" ]; then
    echo "未安装火锅"
    exit
fi

WINEPREFIX=$(realpath ${WINEPREFIX:-${HOME}/.wine})
if [ -L /opt ]; then
    OPT_REAL_PATH=$(readlink -f /opt)
    case "$WINEPREFIX" in
        ${OPT_REAL_PATH}*)
            WINEPREFIX=/opt${WINEPREFIX#$OPT_REAL_PATH}
            ;;
        *)
            ;;
    esac
fi

if mountpoint -q /run/hotpot ; then
(exec bwrap --ro-bind /run/hotpot  /                      \
      --ro-bind /etc/passwd /etc/passwd                   \
      --ro-bind /etc/group  /etc/group                    \
      --ro-bind /etc/shadow  /etc/shadow                  \
      --ro-bind /etc/resolv.conf /etc/resolv.conf         \
      --ro-bind /etc/localtime   /etc/localtime           \
      --ro-bind /usr/lib/x86_64-linux-gnu/libhotpot-reg.so /usr/lib/x86_64-linux-gnu/libhotpot-reg.so         \
      --ro-bind /usr/bin/yuanyanghotpot-sudoer /usr/bin/yuanyanghotpot-sudoer         \
      --ro-bind /usr/bin/yuanyanghotpot /usr/bin/yuanyanghotpot         \
      --ro-bind /var/cache/fontconfig/ /var/cache/fontconfig/         \
      --ro-bind /usr/share/fonts /usr/share/fonts         \
      --bind /home /home                                  \
      --bind /tmp /tmp                                    \
      --bind /opt /opt                                    \
      --proc /proc                                        \
      --dev-bind /dev /dev                                \
      --dev-bind /dev/pts /dev/pts                        \
      --bind /dev/shm /dev/shm                            \
      --ro-bind /sys /sys                                 \
      --ro-bind /run /run                                 \
      --ro-bind /etc/resolv.conf /etc/resolv.conf         \
      --ro-bind /etc/nsswitch.conf /etc/nsswitch.conf     \
      --ro-bind /etc/hosts /etc/hosts                     \
      --ro-bind /etc/passwd /etc/passwd                   \
      --ro-bind /etc/group /etc/group                     \
      --bind /run/user/"$(id -u)" /run/user/"$(id -u)"    \
      --bind /run/dbus /run/dbus                          \
      --setenv XDG_RUNTIME_DIR "/run/user/`id -u`"        \
      --setenv DISPLAY "${DISPLAY:-:0}"                   \
      --setenv XAUTHORITY "${XAUTHORITY:-${HOME}/.Xauthority}"                 \
"${WINE_PATH}/bin/${WHO}" "$@")
else
"${WINE_PATH}/bin/${WHO}" "$@"
fi
