#!/bin/sh
set -eu

pkg=ltrsift
export LC_ALL=C.UTF-8
TMP="${AUTOPKGTEST_TMP:-$(mktemp -d /tmp/${pkg}-repro.XXXXXX)}"
export HOME="$TMP/home"
export XDG_CONFIG_HOME="$HOME/.config"
export XDG_CACHE_HOME="$HOME/.cache"
export GDK_BACKEND=x11
mkdir -p "$XDG_CONFIG_HOME" "$XDG_CACHE_HOME"
cp -a /usr/share/doc/${pkg}/sample_data "$TMP/"
cd "$TMP/sample_data"
if [ -f dmel58.ltrd.gff3.gz ]; then gzip -dk dmel58.ltrd.gff3.gz; fi
sh ./prepare_encseqs
GFF="$TMP/sample_data/dmel58.ltrd.gff3"
IDX="$TMP/sample_data/dmel58"
PROJ="$TMP/sample_data/testproject"

cat > "$TMP/drive.sh" <<EOF
#!/bin/sh
set -eu
export DISPLAY=:99
Xvfb :99 -screen 0 1280x1024x24 > "$TMP/xvfb.log" 2>&1 &
XPID=\$!
sleep 1
openbox > "$TMP/openbox.log" 2>&1 &
WMPID=\$!
sleep 1
ltrsift > "$TMP/ltrsift.log" 2>&1 &
LPID=\$!
trap 'kill \$LPID \$XPID \$WMPID 2>/dev/null || true' EXIT
sleep 5

active_name() {
  W=\$(xdotool getactivewindow)
  xdotool getwindowname \$W
}

click_chooser_ok() {
  W=\$(xdotool getactivewindow)
  NAME=\$(xdotool getwindowname \$W)
  echo "chooser active: \$NAME"
  case "\$NAME" in
    Select*)
      eval \$(xdotool getwindowgeometry --shell \$W)
      echo "clicking chooser OK at \$((WIDTH-60)),\$((HEIGHT-20)) in \$WIDTH x \$HEIGHT"
      xdotool mousemove --window \$W \$((WIDTH-60)) \$((HEIGHT-20)) click 1
      sleep 2
      echo "active after chooser OK: \$(active_name)"
      if ! kill -0 \$LPID 2>/dev/null; then set +e; wait \$LPID; ST=\$?; set -e; echo "LTRSIFT DIED DURING CHOOSER status=\$ST"; cat "$TMP/ltrsift.log"; exit 42; fi
      ;;
    *) echo "chooser already closed" ;;
  esac
}

advance_assistant() {
  i=0
  while [ \$i -lt 5 ]; do
    i=\$((i+1))
    W=\$(xdotool getactivewindow)
    NAME=\$(xdotool getwindowname \$W)
    echo "assistant page: \$NAME"
    case "\$NAME" in
      "General settings"|"Matching/Clustering"|"Classification"|"Summary") ;;
      *) echo "assistant closed or other window: \$NAME"; break ;;
    esac
    eval \$(xdotool getwindowgeometry --shell \$W)
    echo "clicking assistant forward at \$((WIDTH-60)),\$((HEIGHT-20)) in \$WIDTH x \$HEIGHT"
    xdotool mousemove --window \$W \$((WIDTH-60)) \$((HEIGHT-20)) click 1
    sleep 3
    if ! kill -0 \$LPID 2>/dev/null; then set +e; wait \$LPID; ST=\$?; set -e; echo "LTRSIFT DIED DURING ASSISTANT status=\$ST"; cat "$TMP/ltrsift.log"; exit 43; fi
  done
}

MAIN=\$(xdotool search --sync --onlyvisible --pid \$LPID --name '^LTRsift\$' | head -1)
xdotool windowactivate --sync \$MAIN
echo "main active: \$(active_name)"
xdotool key --clearmodifiers ctrl+n
sleep 2
echo "after ctrl+n active: \$(active_name)"

# Project file
xdotool key --clearmodifiers alt+w
sleep 1
echo "project chooser active: \$(active_name)"
xdotool key --clearmodifiers ctrl+l
sleep 1
xdotool type --delay 20 "$PROJ.ltrsift"
xdotool key --clearmodifiers Return
sleep 1
echo "after project path active: \$(active_name)"
click_chooser_ok

# Index name (using a .esq suffix; the widget strips it)
xdotool key --clearmodifiers alt+s
sleep 1
echo "index chooser active: \$(active_name)"
xdotool key --clearmodifiers ctrl+l
sleep 1
xdotool type --delay 20 "$IDX.esq"
xdotool key --clearmodifiers Return
sleep 1
echo "after index path active: \$(active_name)"
click_chooser_ok

# Add GFF3 file
xdotool key --clearmodifiers alt+a
sleep 1
echo "gff3 chooser active: \$(active_name)"
xdotool key --clearmodifiers ctrl+l
sleep 1
xdotool type --delay 20 "$GFF"
xdotool key --clearmodifiers Return
sleep 1
echo "after gff3 path active: \$(active_name)"
click_chooser_ok

advance_assistant
sleep 20

if kill -0 \$LPID 2>/dev/null; then
  echo "ALIVE after project creation"
  kill \$LPID || true
  wait \$LPID || true
  exit 0
else
  wait \$LPID || STATUS=\$?
  echo "CRASHED (status \$STATUS)"
  exit 1
fi
EOF
chmod +x "$TMP/drive.sh"
set +e
sh "$TMP/drive.sh"
drive_status=$?
set -e
echo "drive status: $drive_status" >&2
echo '--- ltrsift log ---' >&2
cat "$TMP/ltrsift.log" >&2 || true
exit $drive_status
