#!/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.stdout.write('false\n')
  process.exit(1)
}

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

process.stdout.write(isConnected ? 'true\n' : 'false\n')
process.exit(isConnected ? 0 : 1)
