#!/usr/bin/env bash
# Flora cable install for Linux and Mac. Put this file next to flora-advanced.apk and run: bash flora-cable-install.sh
# It installs Flora on a phone connected by USB cable. It contains no passwords or keys.
set -u
PKG=online.seidrlabs.floravivo
DIR="$(cd "$(dirname "$0")" && pwd)"
APK="$DIR/flora-advanced.apk"

if ! command -v adb >/dev/null 2>&1; then
  if [ -x "$DIR/platform-tools/adb" ]; then PATH="$DIR/platform-tools:$PATH"; else
    echo "adb was not found. Download SDK Platform-Tools from developer.android.com/tools/releases/platform-tools,"
    echo "unzip it so the folder platform-tools sits next to this file, then run this again."
    exit 1
  fi
fi
[ -f "$APK" ] || { echo "flora-advanced.apk is not next to this script. Put both in the same folder."; exit 1; }
adb start-server >/dev/null 2>&1
if ! adb devices | grep -q "device$"; then
  echo "No phone found. On the phone: turn on USB debugging, plug in the cable, tap Allow on the 'Allow USB debugging?' question, then run this again."
  exit 1
fi
echo "Installing Flora. This takes a minute..."
adb install -r "$APK" || { echo "Install failed. If it mentions a signature mismatch, uninstall the older Flora from the phone first."; exit 1; }
echo "Allowing Android's restricted settings for Flora so you can switch on phone control and message reading yourself..."
adb shell cmd appops set "$PKG" ACCESS_RESTRICTED_SETTINGS allow
echo "Done. Open Flora on the phone; when she asks, switch on her access in Settings (Accessibility and Notification access). You can unplug now."
