#!/bin/bash # Script to set up automatic Thorium browser launch with Fitatu website on startup # Opens https://www.fitatu.com/ in Thorium browser every time the system boots set -e # Exit on any error # Source common library for shared functions SCRIPT_DIR="$(dirname "$(readlink -f "$0")")" # shellcheck source=lib/common.sh source "$SCRIPT_DIR/lib/common.sh" # Initialize setup script (parse args, require root, print header) init_setup_script "Thorium Browser Auto-Startup Setup" "$@" # Target URL TARGET_URL="https://www.fitatu.com/app/planner" BROWSER_COMMAND="thorium-browser" USER_HOME="/home/$(get_actual_user)" echo "" echo "Target URL: $TARGET_URL" echo "Browser: $BROWSER_COMMAND" echo "User home: $USER_HOME" # Function to check if Thorium browser is installed check_thorium_browser() { echo "" echo "1. Checking Thorium Browser Installation..." echo "==========================================" if ! command -v "$BROWSER_COMMAND" &>/dev/null; then echo "Warning: Thorium browser not found in PATH" echo "Checking alternative locations..." # Check common installation paths local alt_paths=( "/opt/thorium/thorium" "/usr/bin/thorium" "/usr/local/bin/thorium" "/opt/thorium-browser/thorium-browser" "${USER_HOME}/.local/bin/thorium-browser" ) local found=false for path in "${alt_paths[@]}"; do if [[ -x $path ]]; then BROWSER_COMMAND="$path" echo "✓ Found Thorium browser at: $path" found=true break fi done if [[ $found != true ]]; then echo "Error: Thorium browser not found!" echo "Please install Thorium browser first or ensure it's in your PATH." echo "" echo "You can install Thorium browser from:" echo "https://thorium.rocks/" echo "" local continue_anyway=false if [[ $INTERACTIVE_MODE == "true" ]]; then read -p "Continue anyway? The service will be created but may fail to start (y/N): " -n 1 -r echo if [[ $REPLY =~ ^[Yy]$ ]]; then continue_anyway=true fi else echo "Auto-continuing anyway - service will be created but may fail to start (use --interactive to prompt)" continue_anyway=true fi if [[ $continue_anyway != true ]]; then exit 1 fi fi else echo "✓ Thorium browser found: $(which $BROWSER_COMMAND)" fi } # Function to create the browser launcher script create_launcher_script() { echo "" echo "2. Creating Browser Launcher Script..." echo "=====================================" local launcher_script="/usr/local/bin/thorium-fitatu-launcher.sh" cat >"$launcher_script" <&2 # Wait for X11 server while [[ \$attempt -lt \$max_attempts ]]; do if xset q &>/dev/null 2>&1; then echo "X11 server is ready" >&2 break fi sleep 1 ((attempt++)) done if [[ \$attempt -eq \$max_attempts ]]; then echo "Timeout waiting for X11 server" >&2 return 1 fi # Quick check for window manager (no waiting loop) if pgrep -x i3 >/dev/null 2>&1; then echo "i3 window manager detected and running" >&2 elif pgrep -x "i3wm" >/dev/null 2>&1; then echo "i3wm window manager detected and running" >&2 elif wmctrl -m >/dev/null 2>&1; then echo "Window manager detected via wmctrl" >&2 else echo "Window manager not detected, proceeding anyway" >&2 fi return 0 } # Function to launch browser launch_browser() { echo "Launching Thorium browser with Fitatu..." >&2 # Try to launch browser as the original user if command -v sudo &>/dev/null && [[ -n "${SUDO_USER}" ]]; then sudo -u "${SUDO_USER}" env DISPLAY=:0 HOME="$USER_HOME" "$BROWSER_COMMAND" "$TARGET_URL" & else "$BROWSER_COMMAND" "$TARGET_URL" & fi local browser_pid=\$! echo "Browser launched with PID: \$browser_pid" >&2 return 0 } # Main execution echo "\$(date): Starting Thorium-Fitatu launcher" >&2 if wait_for_desktop; then launch_browser echo "\$(date): Thorium browser launch completed" >&2 else echo "\$(date): Failed to launch - desktop environment not ready" >&2 exit 1 fi EOF chmod +x "$launcher_script" echo "✓ Created launcher script: $launcher_script" } # Function to create systemd service for user session create_user_systemd_service() { echo "" echo "3. Creating User Systemd Service..." echo "==================================" local user_systemd_dir="$USER_HOME/.config/systemd/user" local service_file="$user_systemd_dir/thorium-fitatu-startup.service" # Create user systemd directory sudo -u "${SUDO_USER}" mkdir -p "$user_systemd_dir" # Create the service file sudo -u "${SUDO_USER}" tee "$service_file" >/dev/null <"$service_file" </dev/null <