// cell pack <package> [options] - Build static binary for a package and its dependencies
//
// Usage:
//   cell pack <package>              Build static binary for package (output: app)
//   cell pack <package> -o <name>    Specify output name
//   cell pack <package> -t <target>  Cross-compile for target platform

var build = use('build')
var shop = use('internal/shop')
var pkg_tools = use('package')

var target = null
var output_name = 'app'
var target_package = null
var buildtype = 'debug'
var i = 0

if (length(args) < 1) {
  log.error('Usage: cell pack <package> [options]')
  log.error('')
  log.error('Options:')
  log.error('  -o, --output <name>   Output name for binary (default: app)')
  log.error('  -t, --target <target> Cross-compile for target platform')
  log.error('  -b, --buildtype <type> Build type: release, debug, minsize (default: release)')
  log.error('')
  log.error('Available targets: ' + text(build.list_targets(), ', '))
  $stop()
}

target_package = args[0]

for (i = 1; i < length(args); i++) {
  if (args[i] == '-t' || args[i] == '--target') {
    if (i + 1 < length(args)) {
      target = args[++i]
    } else {
      log.error('-t requires a target')
      $stop()
    }
  } else if (args[i] == '-o' || args[i] == '--output') {
    if (i + 1 < length(args)) {
      output_name = args[++i]
    } else {
      log.error('-o requires an output name')
      $stop()
    }
  } else if (args[i] == '-b' || args[i] == '--buildtype') {
    if (i + 1 < length(args)) {
      buildtype = args[++i]
      if (buildtype != 'release' && buildtype != 'debug' && buildtype != 'minsize') {
        log.error('Invalid buildtype: ' + buildtype + '. Must be release, debug, or minsize')
        $stop()
      }
    } else {
      log.error('-b requires a buildtype (release, debug, minsize)')
      $stop()
    }
  } else if (args[i] == '-h' || args[i] == '--help') {
    log.console('Usage: cell pack <package> [options]')
    log.console('')
    log.console('Options:')
    log.console('  -o, --output <name>   Output name for binary (default: app)')
    log.console('  -t, --target <target> Cross-compile for target platform')
    log.console('  -b, --buildtype <type> Build type: release, debug, minsize (default: release)')
    log.console('')
    log.console('Available targets: ' + text(build.list_targets(), ', '))
    $stop()
  } else {
    log.error('Unknown option: ' + args[i])
    $stop()
  }
}

// Detect target if not specified
if (!target) {
  target = build.detect_host_target()
  if (target) log.console('Target: ' + target)
}

if (target && !build.has_target(target)) {
  log.error('Invalid target: ' + target)
  log.console('Available targets: ' + text(build.list_targets(), ', '))
  $stop()
}

// Prepare packages: core + dependencies + target package
var packages = ['core']
var deps = pkg_tools.gather_dependencies(target_package)

for (i = 0; i < length(deps); i++) {
  push(packages, deps[i])
}
push(packages, target_package)

// Remove duplicates
var unique_packages = []
var seen = {}
for (i = 0; i < length(packages); i++) {
  if (!seen[packages[i]]) {
    seen[packages[i]] = true
    push(unique_packages, packages[i])
  }
}
packages = unique_packages

log.console('Preparing packages...')
arrfor(packages, function(package) {
  if (package == 'core') return
  shop.extract(package)
})

log.console('Building static binary from ' + text(length(packages)) + ' packages: ' + text(packages, ', '))

var _build = function() {
  var result = build.build_static(packages, target, output_name, buildtype)
  log.console('Build complete: ' + result)
} disruption {
  log.error('Build failed')
  $stop()
}
_build()

$stop()
