// cell fetch - Sync packages from remote sources
//
// Ensures all packages are fetched, extracted, compiled, and ready to use.
// For local packages, this is a no-op (symlinks only).
//
// Usage:
//   cell fetch           - Sync all packages
//   cell fetch <package> - Sync a specific package

var shop = use('internal/shop')

var target_pkg = null
var i = 0
var packages = null
var count = 0

var run = function() {
  for (i = 0; i < length(args); i++) {
    if (args[i] == '--help' || args[i] == '-h') {
      log.console("Usage: cell fetch [package]")
      log.console("Sync packages from remote sources.")
      log.console("")
      log.console("Arguments:")
      log.console("  package  Optional package to sync. If omitted, syncs all.")
      return
    } else if (!starts_with(args[i], '-')) {
      target_pkg = args[i]
    }
  }

  if (target_pkg) {
    target_pkg = shop.resolve_locator(target_pkg)
    log.console("Syncing " + target_pkg + "...")
    shop.sync(target_pkg)
    log.console("Done.")
  } else {
    packages = shop.list_packages()
    count = 0
    arrfor(packages, function(pkg) {
      if (pkg == 'core') return
      shop.sync(pkg)
      count = count + 1
    })
    log.console("Synced " + text(count) + " package(s).")
  }
}
run()

$stop()
