#!/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
    APP=paulscode.android.mupen64plusae
    URL=http://www.paulscode.com/source/Mupen64Plus-AE/19JAN2014/Mupen64Plus.apk
    APK=${URL##*/}
    wget -N "$URL"
    adb kill-server
    adb start-server
    adb disconnect
    adb connect $IP
    adb wait-for-device
    adb uninstall $APP
    adb install $APK
    [ -x ROMS ] && adb push ROMS /sdcard/mupen64plus
    adb shell monkey -p $APP -c android.intent.category.LAUNCHER 1
    adb disconnect
    adb kill-server
fi
echo http://www.paulscode.com/forum/index.php?topic=96.msg2243
