#!/bin/sh
set -e
SCRIPT_PATH="$0"
case "$SCRIPT_PATH" in
  /*) : ;;
  *) SCRIPT_PATH="$(pwd)/$SCRIPT_PATH" ;;
esac
SCRIPT_DIR=$(dirname "$SCRIPT_PATH")
cd "$SCRIPT_DIR"

if uname -m | grep -E -q 'aarch64|arm64'; then
  TOOLDIR=aarch64
else
  TOOLDIR=x86_64
fi

if [ "$(id -u)" -eq 0 ]; then
  if [ -z "$XDG_RUNTIME_DIR" ] || [ ! -d "$XDG_RUNTIME_DIR" ] || [ ! -O "$XDG_RUNTIME_DIR" ]; then
    export XDG_RUNTIME_DIR=/tmp/runtime-root
    mkdir -p "$XDG_RUNTIME_DIR"
    chmod 700 "$XDG_RUNTIME_DIR" 2>/dev/null || true
  fi
  exec ./tool/$TOOLDIR/Hiperiso2Disk.qt5 "$@"
fi
if command -v pkexec >/dev/null 2>&1; then
  exec pkexec env DISPLAY="$DISPLAY" XAUTHORITY="$XAUTHORITY" XDG_RUNTIME_DIR=/tmp/runtime-root "$SCRIPT_DIR/tool/$TOOLDIR/Hiperiso2Disk.qt5" "$@"
fi
if command -v sudo >/dev/null 2>&1; then
  exec sudo env DISPLAY="$DISPLAY" XAUTHORITY="$XAUTHORITY" XDG_RUNTIME_DIR=/tmp/runtime-root "$SCRIPT_DIR/tool/$TOOLDIR/Hiperiso2Disk.qt5" "$@"
fi
printf '%s\n' 'Need pkexec or sudo to start the Qt GUI with root privileges.' >&2
exit 1
