#!/usr/bin/env lua5.4
local fennel = require("fennel")
debug.traceback = fennel.traceback
local _local_1_ = require("fnlfmt")
local format_file = _local_1_["format-file"]
local version = _local_1_.version
local _local_2_ = require("indentation")
local indent_file = _local_2_["indent-file"]
local function help()
  print("Usage: fnlfmt [--no-comments] [--fix] [--check] FILENAME...")
  print("With the --fix or --check argument, multiple files can be specified.")
  print("With the --fix argument, formats all the files in-place;")
  print("with the --check argument, only checks if all the files are formatted;")
  return print("otherwise prints the formatted file to stdout.")
end
local options = {}
for i = #arg, 1, -1 do
  if ("--no-comments" == arg[i]) then
    options["no-comments"] = true
    table.remove(arg, i)
  else
  end
end
local function check_file(filename, f)
  if select(2, f(filename, options)) then
    return io.stderr:write(string.format("Not formatted: %s\n", filename))
  else
    return true
  end
end
local function check_files(filenames, f)
  local function _5_(_241)
    if _241 then
      return 0
    else
      return 1
    end
  end
  local function _7_()
    local ok_3f = true
    for _, filename in ipairs(filenames) do
      ok_3f = (check_file(filename, f) and ok_3f)
    end
    return ok_3f
  end
  return os.exit(_5_(_7_()))
end
local function fix(filename, f)
  local new, changed_3f = f(filename, options)
  if changed_3f then
    local f0 = assert(io.open(filename, "w"))
    f0:write(new)
    return f0:close()
  else
    return nil
  end
end
if ((_G.type(arg) == "table") and (arg[1] == "--version")) then
  return print(("fnlfmt version " .. version))
elseif ((_G.type(arg) == "table") and (arg[1] == "--fix")) then
  local filenames = {select(2, (table.unpack or _G.unpack)(arg))}
  for _, filename in pairs(filenames) do
    fix(filename, format_file)
  end
  return nil
elseif ((_G.type(arg) == "table") and (arg[1] == "--check")) then
  local filenames = {select(2, (table.unpack or _G.unpack)(arg))}
  return check_files(filenames, format_file)
elseif ((_G.type(arg) == "table") and (arg[1] == "--indent") and (nil ~= arg[2]) and (arg[3] == nil)) then
  local filename = arg[2]
  return print((indent_file(filename)))
elseif ((_G.type(arg) == "table") and (arg[1] == "--fix-indent")) then
  local filenames = {select(2, (table.unpack or _G.unpack)(arg))}
  for _, filename in pairs(filenames) do
    fix(filename, indent_file)
  end
  return nil
elseif ((_G.type(arg) == "table") and (arg[1] == "--check-indent")) then
  local filenames = {select(2, (table.unpack or _G.unpack)(arg))}
  return check_files(filenames, indent_file)
elseif (((_G.type(arg) == "table") and (arg[1] == "--help")) or ((_G.type(arg) == "table") and (arg[1] == "-?")) or ((_G.type(arg) == "table") and (arg[1] == "-h"))) then
  return help()
elseif ((_G.type(arg) == "table") and (nil ~= arg[1]) and (arg[2] == nil)) then
  local filename = arg[1]
  return io.stdout:write((format_file(filename, options)))
else
  local _ = arg
  return help()
end
