#!/bin/sh
IP=$(nmap -n -p 5555 --open $(LANG= /sbin/ifconfig|awk '/inet addr:/{print $2}'|sed -e 's/addr://;s/\(.*\)..*/\1/'|grep -v 127.0.0.|head -1)0/24|awk '/Nmap scan report for/{print $5}'|head -1)

if [ "$IP" ]; then
    APK=remotemouse.apk
    URL=http://ios.guidology.com/remotemouse/
    APP=com.guidology.android.remotemouseforfiretv
    wget -N "$URL$APK"
    adb kill-server
    adb start-server
    adb disconnect
    adb connect $IP
    adb wait-for-device
    adb uninstall $APP
    adb install $APK
    adb shell monkey -p $APP -c android.intent.category.LAUNCHER 1
    adb disconnect
    adb kill-server
fi
echo http://ios.guidology.com/remotemouse/
