#include "main.ih"

namespace   // the anonymous namespace can be used here
{
    ArgConfig::LongOption longOptions[] =
    {
        ArgConfig::LongOption{"append",         ArgConfig::Required}, 
        ArgConfig::LongOption{"configfile",     ArgConfig::Required}, 
        ArgConfig::LongOption{"cc",             ArgConfig::Required}, 
        ArgConfig::LongOption{"debug",          ArgConfig::NoArg}, 
        ArgConfig::LongOption{"error-dir",      'e'},
        ArgConfig::LongOption{"error-url",      'u'},
        ArgConfig::LongOption{"from",           ArgConfig::Required},
        ArgConfig::LongOption{"help",           'h'},
        ArgConfig::LongOption{"keepdir",        ArgConfig::NoArg},
        ArgConfig::LongOption{"log",            'l'},
        ArgConfig::LongOption{"mail-zip",       'm'}, 
                                                
        ArgConfig::LongOption{"gpg",            ArgConfig::NoArg},
        ArgConfig::LongOption{"no-cc",          ArgConfig::NoArg},
        ArgConfig::LongOption{"no-compilation", ArgConfig::NoArg}, 
        ArgConfig::LongOption{"no-mail",        ArgConfig::NoArg},
        ArgConfig::LongOption{"no-pdf",         ArgConfig::NoArg},

        ArgConfig::LongOption{"rotate",         'r'},
        ArgConfig::LongOption{"skeleton",       'S'},
        ArgConfig::LongOption{"specs",          's'},

        ArgConfig::LongOption{"spool-path",     'p'},
        ArgConfig::LongOption{"spool-zip",      ArgConfig::NoArg},

        ArgConfig::LongOption{"std",            ArgConfig::Required},
        ArgConfig::LongOption{"subject",        ArgConfig::Required},
        ArgConfig::LongOption{"tempdir",        't'},
        ArgConfig::LongOption{"version",        'v'},

// DROPPED:        ArgConfig::LongOption{"zip-check", 'z'},
// the zip is always checked.

    };
    auto longEnd = longOptions + size(longOptions);
}

string g_pid;           // used by log(), initialized by Options::openLog
FBB::Log g_log;

FBB::TempStream g_tempStream;
Mailer g_mailer;
Options g_options;

int main(int argc, char **argv)
try
{
    // --> CCs are automatically set by handler/verifyMetadata

    ArgConfig &arg = ArgConfig::initialize("e:hl:m:p:r:s:S:t:u:v", 
                                longOptions, longEnd, argc, argv);
    arg.versionHelp(usage, Icmbuild::version, 2);

//cerr << __FILE__ " 1\n";
    g_options.init();

    g_mailer.setSubject(g_options.subject());

    Handler handler;

    handler.run();
}
catch (exception const &exc)
{
    logWhat(exc.what());

    switch (FBB::g_errno)
    {
        case Types::REPLY_FROM:
            g_mailer.noCC();
        [[fallthrough]];

        case Types::REPLY:
            g_mailer.sendmail("error(s) encountered", exc.what() + "\n"
                   "Unfortunately, your solution has not been submitted."s);
        break;

        case Types::PANIC:
            g_mailer.addTo(Common::s_fbbrokken);
            g_mailer.sendmail("internal error", 
                "Due to an internal error your mail can currently "
                                                "not be handled.\n"
                "Reason: "s + exc.what());
        [[fallthrough]];

        case Types::NO_MAIL:
        break;
    }
}
catch (...)
{
    return 0;                       // 0 because the handler should return 0
}


