#!/usr/bin/env node
const { execSync } = require('child_process')
const path = require('path')

const projectRoot = path.resolve(__dirname, '..')
const adbPath = path.join(projectRoot, 'exe', 'adb', 'adb.exe')

const deviceIp = process.argv[2]
const devicePort = process.argv[3] || '5555'

if (!deviceIp) {
  process.exit(1)
}

const connectCommand = `"${adbPath}" connect ${deviceIp}:${devicePort}`
const connectOutput = execSync(connectCommand, { encoding: 'utf-8', timeout: 500 })
const connectSuccess = connectOutput.trim().includes('connected') || connectOutput.trim().includes('already connected')

if (!connectSuccess) {
  process.exit(1)
}
else
{
  process.exit(0)
}
