// list modules and actors in packages
//
// Usage:
//   cell ls [<package>]                   List modules and programs
//   cell ls --all                         List across all packages
//   cell ls --modules|-m [<package>]      Modules only
//   cell ls --programs|-p [<package>]     Programs only
//   cell ls --paths [<package>]           One absolute path per line

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

var show_all = false
var show_modules = true
var show_programs = true
var show_paths = false
var filter_modules = false
var filter_programs = false
var pkg_arg = null
var show_help = false
var i = 0

for (i = 0; i < length(args); i++) {
  if (args[i] == '--all' || args[i] == '-a') {
    show_all = true
  } else if (args[i] == '--modules' || args[i] == '-m') {
    filter_modules = true
  } else if (args[i] == '--programs' || args[i] == '-p') {
    filter_programs = true
  } else if (args[i] == '--paths') {
    show_paths = true
  } else if (args[i] == '--help' || args[i] == '-h') {
    show_help = true
  } else if (!starts_with(args[i], '-')) {
    pkg_arg = args[i]
  }
}

if (filter_modules || filter_programs) {
  show_modules = filter_modules
  show_programs = filter_programs
}

var list_one_package = function(pkg) {
  var pkg_dir = null
  var modules = null
  var programs = null
  var j = 0

  if (starts_with(pkg, '/')) {
    pkg_dir = pkg
  } else {
    pkg_dir = shop.get_package_dir(pkg)
  }

  if (show_modules) {
    modules = sort(package.list_modules(pkg))
    if (show_paths) {
      for (j = 0; j < length(modules); j++) {
        log.console(pkg_dir + '/' + modules[j] + '.cm')
      }
    } else {
      if (!filter_modules || show_all) {
        log.console("Modules in " + pkg + ":")
      }
      if (length(modules) == 0) {
        log.console("  (none)")
      } else {
        for (j = 0; j < length(modules); j++) {
          log.console("  " + modules[j])
        }
      }
    }
  }

  if (show_programs) {
    programs = sort(package.list_programs(pkg))
    if (show_paths) {
      for (j = 0; j < length(programs); j++) {
        log.console(pkg_dir + '/' + programs[j] + '.ce')
      }
    } else {
      if (!show_paths && show_modules && !filter_programs) {
        log.console("")
      }
      if (!filter_programs || show_all) {
        log.console("Programs in " + pkg + ":")
      }
      if (length(programs) == 0) {
        log.console("  (none)")
      } else {
        for (j = 0; j < length(programs); j++) {
          log.console("  " + programs[j])
        }
      }
    }
  }
}

var packages = null
var pkg = null

if (show_help) {
  log.console("Usage: cell ls [options] [<package>]")
  log.console("")
  log.console("Options:")
  log.console("  --all, -a        List across all installed packages")
  log.console("  --modules, -m    Show modules only")
  log.console("  --programs, -p   Show programs only")
  log.console("  --paths          Output one absolute path per line")
} else if (show_all) {
  packages = shop.list_packages()
  if (find(packages, function(p) { return p == 'core' }) == null) {
    packages[] = 'core'
  }
  packages = sort(packages)
  for (i = 0; i < length(packages); i++) {
    if (!show_paths && i > 0) {
      log.console("")
    }
    if (!show_paths) {
      log.console("--- " + packages[i] + " ---")
    }
    list_one_package(packages[i])
  }
} else {
  pkg = pkg_arg || package.find_package_dir('.')
  list_one_package(pkg)
}

$stop()
