#! /usr/bin/env ruby

require 'um'
require 'pathname'

usage = %{usage: um <page name>
       um <sub-command> [ARGS...]

The first form is equivalent to `um read <page name>`.

Subcommands:
  um (l)ist                 List the available pages for the current topic.
  um (r)ead <page name>     Read the given page under the current topic.
  um (e)dit <page name>     Create or edit the given page under the current topic.
  um rm <page name>         Remove the given page.
  um (t)opic [topic]        Get or set the current topic.
  um topics                 List all topics.
  um (c)onfig [config key]  Display configuration environment.
  um (h)elp [sub-command]   Display this help message, or the help message for a sub-command.}

command = ARGV.first

if command.to_s.empty?
  $stderr.puts usage
  exit 1
end

script_path = Pathname.new(__FILE__).realpath

if ARGV.length == 1
  case command
  when 'help', '--help', '-h'
    puts usage
    exit 0
  when 'version', '--version', '-v'
    version_path =
      File.expand_path('../version.txt', File.dirname(script_path))

    if File.exist?(version_path)
      puts File.read(version_path).strip
    else
      puts 'unknown'
    end

    exit 0
  end
end

libexec_dir = File.expand_path('../libexec', File.dirname(script_path))
Commands.libexec(libexec_dir, command)
