Advertisement
Azzar_budiyanto

Scrcpy_install

May 19th, 2025 (edited)
538
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 8.81 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Script to clean up, check, and reinstall scrcpy on Ubuntu-based systems
  4. # Version: 1.1 (Updated: 2025-05-19)
  5.  
  6. # Color Codes
  7. RED='\033[0;31m'
  8. GREEN='\033[0;32m'
  9. YELLOW='\033[0;33m'
  10. BLUE='\033[0;34m'
  11. NC='\033[0m' # No Color
  12.  
  13. # Function to print messages
  14. log_info() {
  15.     echo -e "${BLUE}[INFO $(date +'%Y-%m-%d %H:%M:%S')] $1${NC}"
  16. }
  17.  
  18. log_success() {
  19.     echo -e "${GREEN}[SUCCESS $(date +'%Y-%m-%d %H:%M:%S')] $1${NC}"
  20. }
  21.  
  22. log_warning() {
  23.     echo -e "${YELLOW}[WARNING $(date +'%Y-%m-%d %H:%M:%S')] $1${NC}"
  24. }
  25.  
  26. log_error() {
  27.     echo -e "${RED}[ERROR $(date +'%Y-%m-%d %H:%M:%S')] $1${NC}"
  28. }
  29.  
  30. # --- 0. SCRIPT HEADER AND INITIALIZATION ---
  31. clear
  32. log_info "=================================================="
  33. log_info "    SCRCPY Management Script for Ubuntu-based Systems"
  34. log_info "=================================================="
  35. echo
  36.  
  37. # --- 1. PRELIMINARY CHECKS ---
  38. log_info "Phase 0: Preliminary Checks..."
  39.  
  40. # Check for root/sudo privileges
  41. if [[ $EUID -ne 0 ]]; then
  42.    log_error "This script must be run as root or with sudo."
  43.    log_error "Example: sudo ./manage_scrcpy.sh"
  44.    exit 1
  45. fi
  46. log_success "Running with sufficient privileges."
  47. echo
  48.  
  49. # --- 2. CLEANUP PHASE ---
  50. log_info "Phase 1: Cleaning up existing scrcpy installations..."
  51.  
  52. # Attempt to remove scrcpy installed via apt
  53. log_info "Checking for scrcpy (apt)..."
  54. if dpkg -s scrcpy &> /dev/null; then
  55.     log_warning "scrcpy (apt) found. Attempting removal..."
  56.     if apt-get remove --purge -y scrcpy && apt-get autoremove -y; then
  57.         if ! dpkg -s scrcpy &> /dev/null; then
  58.             log_success "scrcpy (apt) removed successfully."
  59.         else
  60.             log_error "scrcpy (apt) still detected after removal attempt. Please check manually."
  61.         fi
  62.     else
  63.         log_error "Failed to execute apt remove for scrcpy. Please check apt logs."
  64.     fi
  65. else
  66.     log_info "scrcpy (apt) not found. No action needed."
  67. fi
  68. echo
  69.  
  70. # Attempt to remove scrcpy installed via snap
  71. log_info "Checking for scrcpy (snap)..."
  72. if command -v snap &> /dev/null && snap list scrcpy &> /dev/null; then
  73.     log_warning "scrcpy (snap) found. Attempting removal..."
  74.     if snap remove scrcpy; then
  75.         if ! snap list scrcpy &> /dev/null; then
  76.             log_success "scrcpy (snap) removed successfully."
  77.         else
  78.             log_error "scrcpy (snap) still detected after removal attempt. Please check manually."
  79.         fi
  80.     else
  81.         log_error "Failed to execute snap remove for scrcpy. Please check snap logs."
  82.     fi
  83. else
  84.     log_info "scrcpy (snap) not found or snap is not available. No action needed for snap."
  85. fi
  86. echo
  87.  
  88. # Check for manually installed scrcpy (basic check)
  89. # This is harder to automate perfectly, but we can look in common places.
  90. if command -v scrcpy &> /dev/null; then
  91.     SCRCPY_PATH=$(command -v scrcpy)
  92.     # Check if the path is a common manual installation path and not a system path managed by apt/snap
  93.     if [[ "$SCRCPY_PATH" == "/usr/local/bin/scrcpy" ]] || [[ "$SCRCPY_PATH" == "$HOME/.local/bin/scrcpy" ]]; then
  94.         log_warning "Found a potentially manually installed scrcpy at $SCRCPY_PATH."
  95.         log_warning "This script does not automatically remove manually compiled/placed binaries."
  96.         log_warning "If issues persist, please ensure this path is cleared manually if it's not desired."
  97.     fi
  98. else
  99.     log_info "No other scrcpy command found in PATH after apt/snap cleanup attempts."
  100. fi
  101.  
  102. log_success "Cleanup phase completed."
  103. echo
  104.  
  105. # --- 3. SYSTEM CHECK AND PRE-CHECK PHASE ---
  106. log_info "Phase 2: System and Pre-installation Checks..."
  107.  
  108. # Update package lists
  109. log_info "Updating package lists (apt update)..."
  110. if apt-get update; then
  111.     log_success "Package lists updated successfully."
  112. else
  113.     log_error "Failed to update package lists. Please check your internet connection and repository configuration."
  114.     log_error "This might affect the ability to install packages."
  115.     # Decide if to exit or continue with a warning
  116.     # exit 1
  117. fi
  118. echo
  119.  
  120. # Check for ADB (Android Debug Bridge)
  121. log_info "Checking for ADB (Android Debug Bridge)..."
  122. if ! command -v adb &> /dev/null; then
  123.     log_warning "ADB is not installed. It's required for scrcpy."
  124.     read -r -p "Do you want to install ADB (android-tools-adb) now? (y/N): " install_adb
  125.     if [[ "$install_adb" =~ ^[Yy]$ ]]; then
  126.         log_info "Installing ADB (android-tools-adb)..."
  127.         if apt-get install -y android-tools-adb; then
  128.             log_success "ADB installed successfully."
  129.         else
  130.             log_error "Failed to install ADB. scrcpy might not work."
  131.             log_warning "You might need to install 'adb' manually (e.g., sudo apt install adb android-sdk-platform-tools-common)."
  132.         fi
  133.     else
  134.         log_warning "ADB not installed by user choice. scrcpy will likely not function."
  135.     fi
  136. else
  137.     ADB_VERSION_FULL=$(adb --version 2>&1)
  138.     ADB_VERSION_LINE1=$(echo "$ADB_VERSION_FULL" | head -n 1)
  139.     log_success "ADB is already installed ($ADB_VERSION_LINE1)."
  140. fi
  141. echo
  142.  
  143. log_success "System check and pre-check phase completed."
  144. echo
  145.  
  146. # --- 4. SCRCPY REINSTALLATION PHASE ---
  147. log_info "Phase 3: Installing scrcpy..."
  148.  
  149. # Ensure scrcpy is not somehow present after cleanup
  150. if command -v scrcpy &> /dev/null; then
  151.     log_warning "scrcpy command still found in PATH before targeted installation: $(command -v scrcpy)"
  152.     log_warning "This is unexpected if cleanup was thorough for managed packages."
  153.     log_warning "Proceeding with installation attempt..."
  154. fi
  155.  
  156. log_info "Attempting to install scrcpy using apt..."
  157. if apt-get install -y scrcpy; then
  158.     log_success "scrcpy installed successfully via apt."
  159. else
  160.     log_error "Failed to install scrcpy via apt."
  161.     log_warning "This could be due to various reasons (package not found, conflicts, etc.)."
  162.     log_info "Alternative installation methods:"
  163.     log_info "  - Via Snap: sudo snap install scrcpy"
  164.     log_info "  - From GitHub releases (manual): Check the official scrcpy repository."
  165.     exit 1
  166. fi
  167.  
  168. log_success "scrcpy reinstallation phase completed."
  169. echo
  170.  
  171. # --- 5. POST-INSTALLATION CHECK ---
  172. log_info "Phase 4: Post-installation Checks..."
  173.  
  174. # Verify scrcpy installation
  175. log_info "Verifying scrcpy installation..."
  176. if command -v scrcpy &> /dev/null; then
  177.     log_success "scrcpy command is available in PATH: $(command -v scrcpy)"
  178.     SCRC_VERSION_OUTPUT=$(scrcpy --version 2>&1) # scrcpy outputs version to stderr on some versions
  179.     if [[ -z "$SCRC_VERSION_OUTPUT" ]]; then
  180.         SCRC_VERSION_OUTPUT=$(scrcpy -v 2>&1) # try -v for older versions or if --version gives no output
  181.     fi
  182.    
  183.     if [[ -n "$SCRC_VERSION_OUTPUT" ]]; then
  184.         log_info "scrcpy version information: $SCRC_VERSION_OUTPUT"
  185.     else
  186.         log_warning "Could not retrieve scrcpy version, but the command is present."
  187.     fi
  188. else
  189.     log_error "scrcpy command not found after installation. This indicates a critical failure in the installation process."
  190.     exit 1
  191. fi
  192. echo
  193.  
  194. log_info "=================================================="
  195. log_info "               IMPORTANT NEXT STEPS               "
  196. log_info "=================================================="
  197. log_info "1. Ensure USB Debugging is ENABLED on your Android device."
  198. log_info "   - Go to Settings > About phone."
  199. log_info "   - Tap 'Build number' rapidly (usually 7 times) until you see 'You are now a developer!'."
  200. log_info "   - Go back to Settings > System (or search for 'Developer options')."
  201. log_info "   - In Developer options, enable 'USB debugging'."
  202. log_info "   - (Optional but recommended) Enable 'Disable adb authorization timeout'."
  203. log_info "2. Connect your Android device to your computer via a quality USB cable."
  204. log_info "3. Authorize the USB debugging connection on your Android device when prompted."
  205. log_info "   - A dialog will appear on your phone/tablet. Check 'Always allow from this computer' and tap 'OK' or 'Allow'."
  206. log_info "4. Test the ADB connection by opening a new terminal and typing:"
  207. log_info "   ${YELLOW}adb devices${NC}"
  208. log_info "   - Your device should be listed with 'device' next to its ID (e.g., 'emulator-5554 device')."
  209. log_info "   - If it says 'unauthorized', re-check the authorization prompt on your device."
  210. log_info "   - If it's not listed, try a different USB cable/port, or ensure drivers are correct (usually not an issue on Linux)."
  211. log_info "5. Once ADB sees your device, run scrcpy by typing in a terminal:"
  212. log_info "   ${YELLOW}scrcpy${NC}"
  213. log_info "   - For wireless connection (after initial USB setup and pairing): scrcpy --tcpip (or scrcpy -e for specific device)"
  214. log_info "   - For more options: ${YELLOW}scrcpy --help${NC}"
  215. echo
  216.  
  217. log_success "scrcpy setup script finished!"
  218. log_info "If you encounter issues, check the output above for errors or warnings."
  219. log_info "Current date and time: $(date)"
  220.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement