{ godcc - A command-line front-end to Compiler Explorer.
  Because the web is lame and sucks so much it hurts.

  Copyright (C) 2026 Jose E. Marchesi

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see
  <https://www.gnu.org/licenses/>. }

access Globals,
       JSON,
       Argp
begin string godcc_version = "1.0";

      { Diagnostics. }

      bool be_quiet := false;

      proc info = (string msg) void:
         (NOT be_quiet | puts(msg));

      proc godcc_error = (string msg) void:
      begin puts("godcc: error: " + msg + "'n");
            posix_exit(1)
      end;

      error := godcc_error;
      json_error := godcc_error;
      
      { --help and --version  }

      proc do_help = (string arg) bool:
      begin puts ("\
Usage: godcc [ARG...] list (languages | compilers [-l LANGUAGE] | formats)'n\
   or: godcc [ARG...] compile [-c COMPILER] [-o OUTFILE] [SRCFILE] [-- [CARG...]]'n\
   or: godcc [ARG...] format [-f FORMATTER] [-s STYLE] [-o OUTFILE] [SRCFILE]'n\
Command-line front-end to Compiler Explorer.'n'n\
Arguments for list compilers:'n\
      -l, --language=LANGUAGE         list compilers for the specified language'n\
                                        only.'n'n\
Arguments for compile:'n\
      SRCFILE                         source file. Defaults to -, the standard input.'n\
      -c, --compiler=COMPILER         compiler to use.'n\
      -- [CARG...]                    options to send to compiler verbatim.'n'n\
      Note that if the program is invoked under a name like godcc-COMPILER'n\
      then COMPILER is the default for -c|--compiler.'n'n\
Arguments for format:'n\
      SRCFILE                         source file. Defaults to -, the standard input.'n\
      -f, --formatter=FORMATTER       formatter to use. Defaults to clangformat.'n\
      -s, --style=STYLE               style to use. Defaults to GNU.'n\
      -o                              output file. Defaults to -, the standard output.'n'n\
General arguments:'n\
      -q, --quiet                     be quiet.  Useful for scripting'n\
          --help                      print a help message and exit.'n\
          --version                   show version and exit.'n'n\
Environment variables:'n\
      GODCC_CEHOST                    hostname where to connect to CE'n\
      GODCC_CEPORT                    port to use for connecting to CE'n'n\
Report bugs to: algol68@gcc.gnu.org'n");
            false
      end;

      proc do_version = (string arg) bool:
      begin puts("godcc " + godcc_version + "'n'n");
            puts("Copyright (C) 2026 Jose E. Marchesi.'n");
            puts("License GPLv3+: GNU GPL version 3 or later.'n");
            puts("This is free software: you are free to change and redistribute it.'n");
            puts("There is NO WARRANTY, to the extent permitted by law.'n");
            false
      end;

      { Main program. }

      argp (2, []ArgOpt ((" ", "help", false, do_help),
                         (" ", "version", false, do_version),
                         ("q", "quiet", false, (string arg) bool:
                                                  (be_quiet := true; true))),
            (int pos, string no_opt_arg) bool:
               if no_opt_arg = "list"
               then access List (godcc_list (pos)); false
               elif no_opt_arg = "compile"
               then access Compile (godcc_compile (pos)); false
               elif no_opt_arg = "format"
               then access Format (godcc_format (pos)); false
               else godcc_error("unknown subcommand " + no_opt_arg); skip
               fi,
            godcc_error)
end
