#!/bin/bash
set -e

wine_executable_path="~/.local/share/Steam/steamapps/common/Proton\ 9.0\ \(Beta\)/proton" # Change me!!!
prefix_exact_path=$(readlink -f ~/HoyoPrefix)
envs="WINEPREFIX=$prefix_exact_path STEAM_COMPAT_DATA_PATH=$prefix_exact_path STEAM_COMPAT_CLIENT_INSTALL_PATH=\"$prefix_exact_path/not-needed\""

echo "Wine/Proton location: $wine_executable_path"
echo "HoYo prefix location: $prefix_exact_path"
echo "Envs: $envs"
echo ""

if [ ! -d "$prefix_exact_path" ]; then # Run installer
    echo "Prefix hasn't been created; assuming the launcher hasn't been installed yet, doing that now..."

    echo "Creating prefix directory..."
    mkdir $prefix_exact_path

    echo "Starting hoyo.exe installer..."
    eval "$envs $wine_executable_path run ~/Downloads/hoyo.exe" # awful :3
    exit
fi

launcher_location=$prefix_exact_path/pfx/drive_c/Program\ Files/HoYoPlay/launcher.exe
echo "HoYo launcher executable: $launcher_location"

if [ ! -d "$launcher_location" ]; then
    echo "The prefix exists but the launcher executable does not exist!"
    exit 1
fi

echo "Prefix found! Launching..."
eval "$envs $wine_executable_path run $prefix_exact_path/pfx/drive_c/Program\ Files/HoYoPlay/launcher.exe"
