#!/bin/bash

set -e

version="0.8"

linux=0
windows=0
macos=0

device=$(uname)

if [[ "$device" == "Linux" ]]; then
    #echo "linux detected"
    linux=1
elif [[ "$device" == "Darwin"  ]]; then
    macos=1
else
    windows=1
fi

adb_path=""

adb_darwin="https://github.com/madhavth/pip_packages/raw/master/adb_darwin"
adb_windows="https://github.com/madhavth/pip_packages/raw/master/adb.exe"
adb_linux="https://github.com/madhavth/pip_packages/raw/master/adb_linux"

home=~
mkdir -p "$home/.ngrok_server/"
my_path="$home/.ngrok_server"
cd $my_path

function download_adb()
{
  if [[ "$linux" == "1" ]]; then
    wget -O adb $adb_linux
  elif [[ "$macos" == "1" ]]; then
    wget -O adb $adb_darwin
  else
    wget -O adb $adb_windows
  fi

  adb_path="$my_path/adb"
  chmod +x $adb_path
}

if [[ -f "$adb_path" ]]; then
  echo "adb already downloaded file found"
elif [[ ! $(which adb) ]]; then
    echo "adb not found... attempting to download adb"
    download_adb
else
  adb_path=$(which adb)
fi

function adb()
{
  $adb_path $@
}


down_linux="https://github.com/madhavth/pip_packages/raw/master/ngrok_linux"
down_windows="https://github.com/madhavth/pip_packages/raw/master/ngrok_windows.exe"
down_darwin="https://github.com/madhavth/pip_packages/raw/master/ngrok_darwin"

ngrok_path=""

function download_ngrok()
{
  if [[ "$linux" == "1" ]]; then
    wget -O ngrok $down_linux
  elif [[ "$macos" == "1" ]]; then
    wget -O ngrok $down_darwin
  else
    wget -O ngrok $down_windows
  fi

  ngrok_path="$my_path/ngrok"
  chmod +x ngrok_path
}

if [[ -f "$ngrok_path" ]]; then
  echo "ngrok already downloaded file found"
elif [[ ! $(which ngrok) ]]; then
     echo "ngrok not found... attempting to download ngrok"
     download_ngrok
else
  ngrok_path=$(which ngrok)
fi

function ngrok()
{
  $ngrok_path $@
}


function advl_get()
{
  select=0

  output=$(adb devices)

  devices=(${output/"List of devices attached"/""})

  if [[ -z $devices ]]; then
    echo "-itsover9000wtf"
    exit 1
  fi

  devicesLength=${#devices[@]}

  final=()

  count=0

  for i in ${devices[@]}; do
    if [[ $i != "device" ]]; then
  #    echo "device == $i"
      final+=" $i"
    fi
  done

  	echo ${final[@]}
}

function adb_device_select()
{
  title=""
  first=0
  devices=($(advl_get))

  my_device_count=${#devices[@]}

  if [[ "$my_device_count" == "1" ]]; then
    echo ${devices[0]}
  else
  count=0
  for device in ${devices[@]}; do
  	((++count))
  	echo "$count) $device" >&2
  done

  read -p "Enter a device number:- " number
  echo ${devices[((--number))]}
fi
}

selected_device=$(adb_device_select)

if [[ $selected_device == "-itsover9000wtf" ]]; then
  echo "No device connected. Connect a device to forward IP"
  exit 1
fi

wifi_device_selected=0

function connect_by_ip()
{
  my_device=$1
  routes=($(adb -s $my_device shell ip route))
  len=${#routes[@]}
  ((--len))
  con_device=${routes[$len]}
  echo "trying to connect device $con_device by ip"
  adb -s $my_device tcpip 5555
  sleep 0.3
  selected_device=${con_device}:5555
  adb connect $selected_device
}

function is_device_selected_wifi()
{
  if [[ ! $selected_device =~ "192.168" ]]; then
    connect_by_ip $selected_device
  else
    echo "device already connected by wifi"
  fi
}

is_device_selected_wifi
ngrok tcp $selected_device -region in
