Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Script to clean up, check, and reinstall scrcpy on Ubuntu-based systems
- # Version: 1.1 (Updated: 2025-05-19)
- # Color Codes
- RED='\033[0;31m'
- GREEN='\033[0;32m'
- YELLOW='\033[0;33m'
- BLUE='\033[0;34m'
- NC='\033[0m' # No Color
- # Function to print messages
- log_info() {
- echo -e "${BLUE}[INFO $(date +'%Y-%m-%d %H:%M:%S')] $1${NC}"
- }
- log_success() {
- echo -e "${GREEN}[SUCCESS $(date +'%Y-%m-%d %H:%M:%S')] $1${NC}"
- }
- log_warning() {
- echo -e "${YELLOW}[WARNING $(date +'%Y-%m-%d %H:%M:%S')] $1${NC}"
- }
- log_error() {
- echo -e "${RED}[ERROR $(date +'%Y-%m-%d %H:%M:%S')] $1${NC}"
- }
- # --- 0. SCRIPT HEADER AND INITIALIZATION ---
- clear
- log_info "=================================================="
- log_info " SCRCPY Management Script for Ubuntu-based Systems"
- log_info "=================================================="
- echo
- # --- 1. PRELIMINARY CHECKS ---
- log_info "Phase 0: Preliminary Checks..."
- # Check for root/sudo privileges
- if [[ $EUID -ne 0 ]]; then
- log_error "This script must be run as root or with sudo."
- log_error "Example: sudo ./manage_scrcpy.sh"
- exit 1
- fi
- log_success "Running with sufficient privileges."
- echo
- # --- 2. CLEANUP PHASE ---
- log_info "Phase 1: Cleaning up existing scrcpy installations..."
- # Attempt to remove scrcpy installed via apt
- log_info "Checking for scrcpy (apt)..."
- if dpkg -s scrcpy &> /dev/null; then
- log_warning "scrcpy (apt) found. Attempting removal..."
- if apt-get remove --purge -y scrcpy && apt-get autoremove -y; then
- if ! dpkg -s scrcpy &> /dev/null; then
- log_success "scrcpy (apt) removed successfully."
- else
- log_error "scrcpy (apt) still detected after removal attempt. Please check manually."
- fi
- else
- log_error "Failed to execute apt remove for scrcpy. Please check apt logs."
- fi
- else
- log_info "scrcpy (apt) not found. No action needed."
- fi
- echo
- # Attempt to remove scrcpy installed via snap
- log_info "Checking for scrcpy (snap)..."
- if command -v snap &> /dev/null && snap list scrcpy &> /dev/null; then
- log_warning "scrcpy (snap) found. Attempting removal..."
- if snap remove scrcpy; then
- if ! snap list scrcpy &> /dev/null; then
- log_success "scrcpy (snap) removed successfully."
- else
- log_error "scrcpy (snap) still detected after removal attempt. Please check manually."
- fi
- else
- log_error "Failed to execute snap remove for scrcpy. Please check snap logs."
- fi
- else
- log_info "scrcpy (snap) not found or snap is not available. No action needed for snap."
- fi
- echo
- # Check for manually installed scrcpy (basic check)
- # This is harder to automate perfectly, but we can look in common places.
- if command -v scrcpy &> /dev/null; then
- SCRCPY_PATH=$(command -v scrcpy)
- # Check if the path is a common manual installation path and not a system path managed by apt/snap
- if [[ "$SCRCPY_PATH" == "/usr/local/bin/scrcpy" ]] || [[ "$SCRCPY_PATH" == "$HOME/.local/bin/scrcpy" ]]; then
- log_warning "Found a potentially manually installed scrcpy at $SCRCPY_PATH."
- log_warning "This script does not automatically remove manually compiled/placed binaries."
- log_warning "If issues persist, please ensure this path is cleared manually if it's not desired."
- fi
- else
- log_info "No other scrcpy command found in PATH after apt/snap cleanup attempts."
- fi
- log_success "Cleanup phase completed."
- echo
- # --- 3. SYSTEM CHECK AND PRE-CHECK PHASE ---
- log_info "Phase 2: System and Pre-installation Checks..."
- # Update package lists
- log_info "Updating package lists (apt update)..."
- if apt-get update; then
- log_success "Package lists updated successfully."
- else
- log_error "Failed to update package lists. Please check your internet connection and repository configuration."
- log_error "This might affect the ability to install packages."
- # Decide if to exit or continue with a warning
- # exit 1
- fi
- echo
- # Check for ADB (Android Debug Bridge)
- log_info "Checking for ADB (Android Debug Bridge)..."
- if ! command -v adb &> /dev/null; then
- log_warning "ADB is not installed. It's required for scrcpy."
- read -r -p "Do you want to install ADB (android-tools-adb) now? (y/N): " install_adb
- if [[ "$install_adb" =~ ^[Yy]$ ]]; then
- log_info "Installing ADB (android-tools-adb)..."
- if apt-get install -y android-tools-adb; then
- log_success "ADB installed successfully."
- else
- log_error "Failed to install ADB. scrcpy might not work."
- log_warning "You might need to install 'adb' manually (e.g., sudo apt install adb android-sdk-platform-tools-common)."
- fi
- else
- log_warning "ADB not installed by user choice. scrcpy will likely not function."
- fi
- else
- ADB_VERSION_FULL=$(adb --version 2>&1)
- ADB_VERSION_LINE1=$(echo "$ADB_VERSION_FULL" | head -n 1)
- log_success "ADB is already installed ($ADB_VERSION_LINE1)."
- fi
- echo
- log_success "System check and pre-check phase completed."
- echo
- # --- 4. SCRCPY REINSTALLATION PHASE ---
- log_info "Phase 3: Installing scrcpy..."
- # Ensure scrcpy is not somehow present after cleanup
- if command -v scrcpy &> /dev/null; then
- log_warning "scrcpy command still found in PATH before targeted installation: $(command -v scrcpy)"
- log_warning "This is unexpected if cleanup was thorough for managed packages."
- log_warning "Proceeding with installation attempt..."
- fi
- log_info "Attempting to install scrcpy using apt..."
- if apt-get install -y scrcpy; then
- log_success "scrcpy installed successfully via apt."
- else
- log_error "Failed to install scrcpy via apt."
- log_warning "This could be due to various reasons (package not found, conflicts, etc.)."
- log_info "Alternative installation methods:"
- log_info " - Via Snap: sudo snap install scrcpy"
- log_info " - From GitHub releases (manual): Check the official scrcpy repository."
- exit 1
- fi
- log_success "scrcpy reinstallation phase completed."
- echo
- # --- 5. POST-INSTALLATION CHECK ---
- log_info "Phase 4: Post-installation Checks..."
- # Verify scrcpy installation
- log_info "Verifying scrcpy installation..."
- if command -v scrcpy &> /dev/null; then
- log_success "scrcpy command is available in PATH: $(command -v scrcpy)"
- SCRC_VERSION_OUTPUT=$(scrcpy --version 2>&1) # scrcpy outputs version to stderr on some versions
- if [[ -z "$SCRC_VERSION_OUTPUT" ]]; then
- SCRC_VERSION_OUTPUT=$(scrcpy -v 2>&1) # try -v for older versions or if --version gives no output
- fi
- if [[ -n "$SCRC_VERSION_OUTPUT" ]]; then
- log_info "scrcpy version information: $SCRC_VERSION_OUTPUT"
- else
- log_warning "Could not retrieve scrcpy version, but the command is present."
- fi
- else
- log_error "scrcpy command not found after installation. This indicates a critical failure in the installation process."
- exit 1
- fi
- echo
- log_info "=================================================="
- log_info " IMPORTANT NEXT STEPS "
- log_info "=================================================="
- log_info "1. Ensure USB Debugging is ENABLED on your Android device."
- log_info " - Go to Settings > About phone."
- log_info " - Tap 'Build number' rapidly (usually 7 times) until you see 'You are now a developer!'."
- log_info " - Go back to Settings > System (or search for 'Developer options')."
- log_info " - In Developer options, enable 'USB debugging'."
- log_info " - (Optional but recommended) Enable 'Disable adb authorization timeout'."
- log_info "2. Connect your Android device to your computer via a quality USB cable."
- log_info "3. Authorize the USB debugging connection on your Android device when prompted."
- log_info " - A dialog will appear on your phone/tablet. Check 'Always allow from this computer' and tap 'OK' or 'Allow'."
- log_info "4. Test the ADB connection by opening a new terminal and typing:"
- log_info " ${YELLOW}adb devices${NC}"
- log_info " - Your device should be listed with 'device' next to its ID (e.g., 'emulator-5554 device')."
- log_info " - If it says 'unauthorized', re-check the authorization prompt on your device."
- log_info " - If it's not listed, try a different USB cable/port, or ensure drivers are correct (usually not an issue on Linux)."
- log_info "5. Once ADB sees your device, run scrcpy by typing in a terminal:"
- log_info " ${YELLOW}scrcpy${NC}"
- log_info " - For wireless connection (after initial USB setup and pairing): scrcpy --tcpip (or scrcpy -e for specific device)"
- log_info " - For more options: ${YELLOW}scrcpy --help${NC}"
- echo
- log_success "scrcpy setup script finished!"
- log_info "If you encounter issues, check the output above for errors or warnings."
- log_info "Current date and time: $(date)"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement