#!/bin/sh -e

TARGET="$(realpath "$0")"
PREFIX="${TARGET%/*/*}"
LOCALBASE="${PREFIX}"

I386_ROOT="${WINE_i386_ROOT:-$HOME/.i386-wine-pkg}"

if [ ! -f "$I386_ROOT/$PREFIX/bin/wine" ]
then
  printf "%s doesn't exist!\n\n" "$I386_ROOT/$PREFIX/bin/wine"
  printf "Try installing 32-bit Wine with\n\t%s\n\n" "$PREFIX/share/wine/pkg32.sh install wine-devel mesa-dri"

  printf "On FreeBSD 15, use wine64.bin if 32-bit support is not needed.\n"
  printf "If 32-bit is needed, either use the repository from FreeBSD 14 with this command:\n\t%s\n" "$PREFIX/share/wine/pkg32.sh --old install -r FreeBSD-ports wine-devel mesa-dri"
  printf "or use Poudriere to build your own packages.\n"

  ABI=$(pkg config ABI | sed s/amd64/i386/)
  FREEBSD_VERSION_MAJOR=`uname -r | sed "s/\..*//"`
  cat <<- HERE

	If using Poudriere, please make sure your repo is setup to use ${ABI}
	and create symlinks for
	  FreeBSD:$FREEBSD_VERSION_MAJOR:amd64 and
	  FreeBSD:$FREEBSD_VERSION_MAJOR:i386
	to the relevant output directories. See pkg.conf(5) for more info.
HERE

  exit 1
fi

# Export early so that external 32-bit libs are found.
export LD_32_LIBRARY_PATH="${LD_32_LIBRARY_PATH:+$LD_32_LIBRARY_PATH:}$I386_ROOT/$LOCALBASE/lib"

WINE32_VERSION=$(env -u WINELOADERNOEXEC "$I386_ROOT/$PREFIX/bin/wine" --version)
WINE64_VERSION=$(env -u WINELOADERNOEXEC "${TARGET}64" --version)
if [ "$WINE32_VERSION" != "$WINE64_VERSION" ]
then
  printf "wine [%s] and wine64 [%s] versions do not match!\n\n" "$WINE32_VERSION" "$WINE64_VERSION"
  printf "Try updating 32-bit wine with\n\t%s\n" "$PREFIX/share/wine/pkg32.sh upgrade"
  printf "If you are on FreeBSD 15, you can use the old repository\n\t%s\n" "$PREFIX/share/wine/pkg32.sh --old upgrade -r FreeBSD-ports"
  exit 1
fi

export LIBGL_DRIVERS_PATH="${LIBGL_DRIVERS_PATH:+$LIBGL_DRIVERS_PATH:}$LOCALBASE/lib/dri-devel:$LOCALBASE/lib32/dri-devel:$I386_ROOT/$LOCALBASE/lib/dri-devel:$LOCALBASE/lib/dri:$LOCALBASE/lib32/dri:$I386_ROOT/$LOCALBASE/lib/dri"
export LD_32_LIBRARY_PATH="${LD_32_LIBRARY_PATH:+$LD_32_LIBRARY_PATH:}$I386_ROOT/$PREFIX/lib/wine:$LOCALBASE/lib32:$I386_ROOT/$LOCALBASE/lib"
for d in "$I386_ROOT/$LOCALBASE"/llvm*/lib
do
  if [ -d "$d" ]
  then
    export LD_32_LIBRARY_PATH="$LD_32_LIBRARY_PATH:$d"
  fi
done
export LD_32_LIBRARY_PATH_RPATH=y
LD_32_LIBMAP_CONF=$(sed "s,$LOCALBASE,$I386_ROOT&,2" "$I386_ROOT/$LOCALBASE"/etc/libmap.d/*.conf 2>/dev/null || true)
export LD_32_LIBMAP="
libgcc_s.so.1 /usr/lib32/libgcc_s.so.1
$LOCALBASE/lib/libvulkan_intel.so  $I386_ROOT/$LOCALBASE/lib/libvulkan_intel.so
$LOCALBASE/lib/libvulkan_radeon.so $I386_ROOT/$LOCALBASE/lib/libvulkan_radeon.so
$LOCALBASE/lib/alsa-lib/libasound_module_pcm_oss.so $I386_ROOT/$LOCALBASE/lib/alsa-lib/libasound_module_pcm_oss.so
$LD_32_LIBMAP_CONF
$LD_32_LIBMAP"

if [ -z "$WINE_NO_WOW64" ]
then
  export WINESERVER="${TARGET}server"
fi

exec "$I386_ROOT/$PREFIX/bin/wine" "$@"
