#!/usr/bin/guile3.0 \
-e main
!#

(use-modules (ice-9 getopt-long)
             (ice-9 pretty-print)
             (ice-9 eval-string)
             (smc core log)
             (smc fsm)
             (smc version)
             (smc cli common)
             (smc cli command-compile)
             (smc cli command-context)
             (smc cli command-run)
             (smc cli command-profile))



(define (print-help-and-exit)
  (display "\
Usage: smc <command> [options]

Commands:
  compile        Compile a PlantUML state diagram to a Guile finite-state machine.
  context        Analyze or generate a context stub based on a given PlantUML file.
  run            Read a finite-state machine from a specified PlantUML file and run
                 it right away.
  profile        Run the state machine profiler.
  help           Print this help message.

For each command there's '--help' option (or '-h' for short) that prints a
help message for the given command.

")
  (exit 0))

(define (print-version)
  (format #t "smc ~a~%" (smc-version/string))
  (display "\
Copyright (C) 2021-2022 Artyom V. Poptsov <poptsov.artyom@gmail.com>
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Artyom V. Poptsov.
"))




(define %commands
  `((("compile")                ,command-compile)
    (("run")                    ,command-run)
    (("context")                ,command-context)
    (("profile")                ,command-profile)
    (("--version" "version")    ,(lambda (rest)
                                   (print-version)))
    (("help", "--help", "-h")   ,(lambda (rest)
                                   (print-help-and-exit)))))


(define (main args)

  (when (< (length args) 2)
    (print-help-and-exit))

  (let* ((command (cadr args))
         (rest    (cons (car args) (cddr args)))
         (handler (command-match command %commands)))
    (if handler
        (handler rest)
        (begin
          (format (current-error-port) "Unknown command: ~a~%" command)
          (print-help-and-exit)))))

;;;

;; Local Variables:
;; mode: Scheme
;; End:

;;; smc.in ends here
