Skip to content

Instantly share code, notes, and snippets.

@Cdaprod
Last active March 23, 2025 18:14
Show Gist options
  • Save Cdaprod/c146b422c3c557915a6fcb032bcf150d to your computer and use it in GitHub Desktop.
Save Cdaprod/c146b422c3c557915a6fcb032bcf150d to your computer and use it in GitHub Desktop.
Pi loads tmux terminal to desktop on boot for mirroring shellfish iOS sessions from iPhone.
#!/usr/bin/env bash
set -euo pipefail
echo "===> Setting up auto desktop terminal for shellfish-* tmux sessions..."
# Determine user and home directory
USER_NAME="${SUDO_USER:-$USER}"
USER_HOME=$(eval echo "~$USER_NAME")
AUTOSTART_DIR="$USER_HOME/.config/autostart"
BIN_DIR="$USER_HOME/bin"
SCRIPT_PATH="$BIN_DIR/attach-latest-shellfish.sh"
AUTOSTART_FILE="$AUTOSTART_DIR/attach-latest-shellfish.desktop"
# Detect available terminal emulator
TERMINAL=$(command -v gnome-terminal || command -v x-terminal-emulator || command -v lxterminal || true)
if [[ -z "$TERMINAL" ]]; then
echo "ERROR: No terminal emulator found. Install gnome-terminal or lxterminal first."
exit 1
fi
# Ensure required directories exist
echo "===> Creating required directories..."
mkdir -p "$BIN_DIR"
mkdir -p "$AUTOSTART_DIR"
# Create tmux wrapper script
echo "===> Creating tmux attach wrapper script: $SCRIPT_PATH"
cat << 'EOF' > "$SCRIPT_PATH"
#!/usr/bin/env bash
while true; do
latest=$(tmux list-sessions -F '#S' 2>/dev/null | grep '^shellfish-' | sort -V | tail -n 1)
if [ -n "$latest" ]; then
tmux attach -t "$latest" || sleep 1
else
tmux new -s shellfish-1 || sleep 1
fi
sleep 1
done
EOF
chmod +x "$SCRIPT_PATH"
chown "$USER_NAME:$USER_NAME" "$SCRIPT_PATH"
# Create autostart entry
echo "===> Creating autostart .desktop file: $AUTOSTART_FILE"
cat << EOF > "$AUTOSTART_FILE"
[Desktop Entry]
Name=AutoAttach Shellfish Tmux
Exec=$TERMINAL -- bash -c '\$HOME/bin/attach-latest-shellfish.sh'
Type=Application
X-GNOME-Autostart-enabled=true
EOF
chown "$USER_NAME:$USER_NAME" "$AUTOSTART_FILE"
# Add ~/bin to PATH in .bashrc if not present
BASHRC="$USER_HOME/.bashrc"
if ! grep -q 'export PATH=.*\$HOME/bin' "$BASHRC"; then
echo 'export PATH="$HOME/bin:$PATH"' >> "$BASHRC"
chown "$USER_NAME:$USER_NAME" "$BASHRC"
echo "===> Added \$HOME/bin to PATH in .bashrc"
fi
echo "===> Setup complete!"
echo "On next reboot/login, a terminal window will auto-attach to the latest 'shellfish-*' tmux session."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment