#!/bin/bash get_target_elf_name() { if [ -f "./Makefile" ]; then grep -oP '\S+\.elf' Makefile | head -n 1 else echo "" fi } find_pcsx2() { if [ -n "$CUSTOM_PCSX2_PATH" ]; then echo "$CUSTOM_PCSX2_PATH" return 0 fi # Check flatpak if command -v flatpak >/dev/null 2>&1 && flatpak list | grep -q "net.pcsx2.PCSX2"; then echo "flatpak run net.pcsx2.PCSX2" return 0 fi # Check if pcsx2-qt is in PATH if command -v pcsx2-qt >/dev/null 2>&1; then echo "pcsx2-qt" return 0 fi # Check if pcsx2 is in PATH if command -v pcsx2 >/dev/null 2>&1; then echo "pcsx2" return 0 fi echo "Error: PCSX2 not found! Please install it, put it in PATH, or specify CUSTOM_PCSX2_PATH in this script." >&2 return 1 } run_pcsx2() { local pcsx2_cmd if ! pcsx2_cmd=$(find_pcsx2); then # Handle the error here, at the top level exit 1 fi local -a pcsx2_args read -ra pcsx2_args <<< "$pcsx2_cmd" local elf_name elf_name=$(get_target_elf_name) if [ -z "$elf_name" ]; then echo "Error: Could not find target ELF name in Makefile!" >&2 exit 1 fi local target_file target_file="$(pwd)/bin/$elf_name" if [ ! -f "$target_file" ]; then echo "Error: ELF file not found at $target_file. Did you build the project?" >&2 exit 1 fi # Kill existing PCSX2 instances if [[ "$pcsx2_cmd" == *"flatpak"* ]]; then pkill -f "net.pcsx2.PCSX2" >/dev/null 2>&1 || true pkill -f "pcsx2-qt" >/dev/null 2>&1 || true else local bin_name bin_name=$(basename "$pcsx2_cmd") pkill "$bin_name" >/dev/null 2>&1 || true fi echo "Running PCSX2: $pcsx2_cmd -elf $target_file" nohup "${pcsx2_args[@]}" -elf "$target_file" > /dev/null 2>&1 & disown } run_pcsx2