#!/bin/bash

DIRECTORY="$(dirname "$(dirname "$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )")")"

function error {
  echo -e "\\e[91m$1\\e[39m"
  exit 1
}

echo "Install Scrcpy"
echo -e "==============\n"
echo " · More info scrcpy --help or visiting https://github.com/Genymobile/scrcpy"
echo " · The Android device requires at least API 21 (Android 5.0)."
echo " · Make sure you enabled adb debugging on your device(s)."
echo " · On some devices, you also need to enable an additional option to control it using keyboard and mouse."
echo " · If you have issues, try to run the app a couple of times through Terminal."
echo

sudo rm -rf ~/scrcpy /usr/local/share/scrcpy ~/.local/share/applications/scrcpy.desktop 2>/dev/null

"${DIRECTORY}/pkg-install" "adb ffmpeg libsdl2-2.0-0" "$(dirname "$0")" || exit 1

wget https://misapuntesde.com/rpi_share/scrcpy-1.13.tar.gz || error "Failed to download!"
tar -xf scrcpy-1.13.tar.gz || error "Failed to extract!"
rm scrcpy-1.13.tar.gz

ln -s /usr/bin/adb $HOME/scrcpy/

sudo mkdir -p /usr/local/share/scrcpy
sudo cp -af $HOME/scrcpy/scrcpy-server /usr/local/share/scrcpy/scrcpy-server

echo "[Desktop Entry]
Name=Scrcpy
Exec=$HOME/scrcpy/android.sh
Icon=$(dirname "$0")/icon-64.png
Path=$HOME/scrcpy/
Type=Application
Comment=Display and control of Android devices connected on USB
Categories=ConsoleOnly;Utility;System;" > ~/.local/share/applications/scrcpy.desktop

