#!/bin/bash
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
PY="$SCRIPT_DIR/buddy.py"
TITLE="Buddy Manager"
if [[ "$OSTYPE" == "darwin"* ]]; then
HAS_WIN=$(osascript 2>/dev/null <<'EOF'
tell application "Terminal"
set found to false
repeat with w in windows
if name of w contains "Buddy Manager" then
set found to true
exit repeat
end if
end repeat
return found
end tell
EOF
)
if [ "$HAS_WIN" = "true" ]; then
osascript 2>/dev/null <<'EOF'
tell application "Terminal"
set frontmost of (first window whose name contains "Buddy Manager") to true
activate
end tell
EOF
else
osascript 2>/dev/null <<APPLESCRIPT
tell application "Terminal"
do script "python3 '$PY'; exit"
set w to front window
set custom title of (selected tab of w) to "$TITLE"
activate
end tell
APPLESCRIPT
fi
else
if command -v wmctrl &>/dev/null; then
if wmctrl -l 2>/dev/null | grep -q "$TITLE"; then
wmctrl -a "$TITLE"
exit 0
fi
fi
if command -v gnome-terminal &>/dev/null; then
gnome-terminal --title="$TITLE" -- python3 "$PY"
elif command -v konsole &>/dev/null; then
konsole --title "$TITLE" -e python3 "$PY" &
elif command -v xfce4-terminal &>/dev/null; then
xfce4-terminal --title="$TITLE" -e "python3 '$PY'" &
elif command -v xterm &>/dev/null; then
xterm -title "$TITLE" -e python3 "$PY" &
else
python3 "$PY"
fi
fi