// cell help - Display help information for cell commands

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

var command = length(args) > 0 ? args[0] : null
var core_dir = shop.get_package_dir('core')
var man_file = null
var stat = null
var content = null
var lines = null
var line = null
var block = null
var ce_path = null
var i = 0
var programs = null
var top_level = []
var descriptions = []
var max_len = 0
var first_line = null
var desc = null
var sep = null
var padding = null
var j = 0

// Display specific command help
if (command) {
  man_file = 'scripts/man/' + command + '.man'
  stat = fd.stat(man_file)
  if (stat && stat.isFile) {
    content = text(fd.slurp(man_file))
    log.console(content)
  } else {
    ce_path = core_dir + '/' + command + '.ce'
    stat = fd.stat(ce_path)
    if (stat && stat.isFile) {
      content = text(fd.slurp(ce_path))
      lines = array(content, '\n')
      block = ""
      for (i = 0; i < length(lines); i++) {
        line = lines[i]
        if (starts_with(line, '// ')) {
          block = block + text(line, 3, length(line)) + '\n'
        } else if (line == '//') {
          block = block + '\n'
        } else if (starts_with(line, '//')) {
          block = block + text(line, 2, length(line)) + '\n'
        } else {
          i = length(lines)
        }
      }
      if (length(block) > 0) {
        log.console(trim(block))
      } else {
        log.error("No help available for command: " + command)
        log.console("Run 'cell help' to see available commands.")
      }
    } else {
      log.error("No help available for command: " + command)
      log.console("Run 'cell help' to see available commands.")
    }
  }
} else {
// Display general help — dynamic listing
programs = sort(package.list_programs('core'))
for (i = 0; i < length(programs); i++) {
  if (search(programs[i], '/') == null) {
    top_level[] = programs[i]
  }
}

for (i = 0; i < length(top_level); i++) {
  ce_path = core_dir + '/' + top_level[i] + '.ce'
  desc = ""
  stat = fd.stat(ce_path)
  if (stat && stat.isFile) {
    content = text(fd.slurp(ce_path))
    lines = array(content, '\n')
    first_line = length(lines) > 0 ? lines[0] : ""
    if (starts_with(first_line, '//')) {
      if (starts_with(first_line, '// ')) {
        first_line = text(first_line, 3, length(first_line))
      } else {
        first_line = text(first_line, 2, length(first_line))
      }
      sep = search(first_line, ' - ')
      if (sep != null) {
        desc = text(first_line, sep + 3, length(first_line))
      } else {
        sep = search(first_line, ' — ')
        if (sep != null) {
          desc = text(first_line, sep + 3, length(first_line))
        } else {
          desc = first_line
        }
      }
    }
  }
  descriptions[] = desc
  if (length(top_level[i]) > max_len) {
    max_len = length(top_level[i])
  }
}

log.console("cell - the cell package manager")
log.console("")
log.console("usage: cell <command> [arguments]")
log.console("")
log.console("available commands:")
for (i = 0; i < length(top_level); i++) {
  padding = ""
  for (j = 0; j < max_len - length(top_level[i]); j++) {
    padding = padding + " "
  }
  if (length(descriptions[i]) > 0) {
    log.console("  " + top_level[i] + padding + "   " + descriptions[i])
  } else {
    log.console("  " + top_level[i])
  }
}
log.console("")
log.console("Run 'cell help <command>' for more information.")
}

$stop()
