#!/usr/bin/env bun
process.env.FORCE_COLOR = '1'

import { program } from './setup'

const isCliUser = process.env.USER === 'cli'
const noArgs = process.argv.length <= 2
const isTTY = !!process.stdin.isTTY

// SSH login shell passes commands as: toes -c "command args"
// With shebang, argv is [bun, script, -c, cmd]; compiled, it's [toes, -c, cmd]
const cIndex = process.argv[1] === '-c' ? 1 : process.argv[2] === '-c' ? 2 : -1
const shellExec = cIndex !== -1 ? process.argv.slice(cIndex + 1).join(' ') : null

if (shellExec) {
  const tokens = shellExec.split(/\s+/).filter(Boolean)
  program.parse(['node', 'toes', ...tokens])
} else if (isCliUser && noArgs && isTTY) {
  const { shell } = await import('./shell')
  await shell()
} else {
  program.parse()
}
