Files
BackTunnel/scripts/backtunnel-access-tui

69 lines
2.2 KiB
Bash

#!/usr/bin/env bash
# SPDX-License-Identifier: GPL-3.0-or-later
# Copyright (c) 2025 LUXIM d.o.o., Slovenia
# Author: Matjaž Mozetič
#
# Name: backtunnel-access-tui
# Summary: Minimal TUI to mount a BackTunnel share into a chosen directory.
# Description:
# Interactive, terminal-based helper that asks for remote, tunnel port, and mount point,
# then invokes backtunnel-access to perform the actual sshfs mount. Defaults to mounting
# into the selected directory; if non-empty, proposes a "backtunnel" subdirectory.
#
# Usage:
# backtunnel-access-tui <selected-dir>
#
# Examples:
# backtunnel-access-tui ~/Downloads
#
# Dependencies:
# - bash
# - backtunnel-access (invoked via exec at the end)
#
# Exit codes:
# 0 success (exec replaces the shell on success)
# 1 invalid usage or mountpoint not writable
#
# Notes:
# - Expands leading "~" in the chosen mount point.
# - This tool only gathers inputs; mounting is performed by backtunnel-access.
set -euo pipefail
SEL_DIR="${1:-}"
[[ -n "$SEL_DIR" ]] || { echo "Usage: backtunnel-access-tui <selected-dir>"; exit 1; }
# Defaults: mount INTO the selected folder
DEFAULT_MP="$SEL_DIR"
# If selected folder is not empty, propose subdir
if [[ -d "$SEL_DIR" ]] && [[ -n "$(ls -A -- "$SEL_DIR" 2>/dev/null || true)" ]]; then
DEFAULT_MP="$SEL_DIR/backtunnel"
fi
# ---- Interactive prompts (with sensible defaults) ----
read -r -p "Remote (user@host or user:host) [user@vps.example.com]: " REMOTE
REMOTE="${REMOTE:-user@vps.example.com}"
read -r -p "Tunnel port on remote [2222]: " PORT
PORT="${PORT:-2222}"
read -r -p "Mount point [${DEFAULT_MP}]: " MP
MP="${MP:-$DEFAULT_MP}"
# Expand leading ~ if user typed it
if [[ "$MP" == "~"* ]]; then
MP="${MP/#\~/$HOME}"
fi
# Ensure mountpoint exists & is writable
mkdir -p -- "$MP"
if [[ ! -w "$MP" ]]; then
echo "Mount point '$MP' is not writable"; exit 1
fi
echo
echo "Running: backtunnel-access '<remote-folder>' from '$REMOTE' -p '$PORT' -m '$MP'"
echo "Note: you'll be prompted on the remote for the exact folder (as per your workflow)."
# Replace this process with backtunnel-access (no return to this script after exec)
exec backtunnel-access "$MP" from "$REMOTE" -p "$PORT" -m "$MP"