// ================================================================ //
//                                                                  //
//   File      : WETC_main.cxx                                      //
//   Purpose   : provide minor windowed tasks for CLI               //
//                                                                  //
//   Institute of Microbiology (Technical University Munich)        //
//   http://www.arb-home.de/                                        //
//                                                                  //
// ================================================================ //

#include <app.hxx>

#include <asciiprint.hxx>

#include <aw_window.hxx>
#include <aw_root.hxx>
#include <aw_question.hxx>

#include <arbdb.h>

AW_HEADER_MAIN


int ARB_main(int argc, char *argv[]) {
    GB_ERROR error = NULp;

    if (argc < 3) {
        error = "Missing arguments";
    }
    else {
        const char *cmd      = argv[1];
        const char *param    = argv[2];
        const char *option   = NULp;
        const char *optparam = NULp;

        enum { UNKNOWN, FILEEDIT, NOTIFY } mode = UNKNOWN;

        if      (strcmp(cmd, "-fileedit") == 0) mode = FILEEDIT;
        else if (strcmp(cmd, "-notify")   == 0) {
            mode = NOTIFY;
            if (argc != 3) {
                if (argc == 5) {
                    option   = argv[3];
                    optparam = argv[4];

                    if (strcmp(option, "-button") != 0) {
                        error = GBS_global_string("unknown option '%s'", option);
                    }
                }
                else {
                    error = "invalid number of arguments";
                }
            }
        }

        if (!error) {
            if (mode == UNKNOWN) {
                error = GBS_global_string("Unexpected parameter '%s'", cmd);
            }
            else {
                GB_shell shell;
                AW_root *aw_root = AWT_create_root("ntree.arb", "ARB_WETC", new NullTracker); // no macro recording here

                switch (mode) {
                    case FILEEDIT:
                        AWT_show_file(aw_root, param);
                        break;

                    case NOTIFY: {
                        aw_question(NULp, param, option ? optparam : "Ok", true, NULp);
                        break;
                    }

                    case UNKNOWN:
                        arb_assert(0); // should not be reached!
                        break;
                }

                aw_root->window_hide(NULp);
                AWT_install_cb_guards();
                aw_root->main_loop();
            }
        }
    }

    if (error) {
        fprintf(stderr,
                "arb_wetc -- ARB Window Environment To CLI -- start GUI tasks from CLI\n"
                "\n"
                "Syntax: arb_wetc -fileedit filename\n"
                "        edit a file before sending it to printer.\n"
                "\n"
                "Syntax: arb_wetc -notify message [-button buttontext]\n"
                "        display a message. Allow user to press OK button.\n"
                "\n"
                "Error: %s\n",
                error);
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}

