/*
 * freescope - Free source browser
 * Copyright (C) 2001  Olivier Deme
 * 
 * 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 2
 * 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */

// FILE:        main.cpp

/************/
/* INCLUDES */
/************/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif // HAVE_CONFIG_H

#include <strstream>
#include <string>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif // HAVE_GETOPT_H
#include "cli.h"
#include "defs.h"
#include "fconfig.h"


/*********************/
/* NAMESPACE SYMBOLS */
/*********************/

using std::string;


/*************************/
/* FUNCTION DECLARATIONS */
/*************************/

void init_trace ();



/************************/
/* FUNCTION DEFINITIONS */
/************************/

/*
 * FUNCTION:    usage
 *
 * DESCRIPTION: Display the program arguments usage
 *
 * THROW:
 * IN:
 * IN-OUT:
 * RETURN CODE:
 */

void usage ()
{
    cerr << "Usage: freescope [options]" << endl;
    cerr << "Options:" << endl;
    cerr << "    -b                    Build database and exit" << endl;
    cerr << "    -d                    Do not update database" << endl;
    cerr << "    -f <cross-ref file>   Use this file instead of freescope.out"
         << endl;
    cerr << "    -h                    Display this help" << endl;
    cerr << "    -l                    Command line interface" << endl;
    cerr << "    -V                    Display the program version" << endl;
    cerr << "    -Z                    Dump database" << endl;
}

/*
 * FUNCTION:    main
 *
 * DESCRIPTION: main routine
 *
 * THROW:
 * IN:          argc    nbr of command line arguments
 *              argv    arguments
 * IN-OUT:
 * RETURN CODE: 0 if success
 */


int main (int argc, char* argv[])
{
    int                  c;
    DbUser::ui_type_t    ui = DbUser::UI_CURSES; // Default mode = curses
    string               symbol;
    DbUser*              dbuser;
    char*                path;
    string               init_file;

    init_trace();

    // Let's see if the FREESCOPERC variable is set
    if ((path = getenv("FREESCOPERC")) != NULL)
    {
        init_file = path;
    }
    else if ((path = getenv("HOME")) != NULL)
    {
        init_file = path;
        init_file += "/.freescoperc";
    }
    else
        init_file = "/.freescoperc";


    // Read configuration file
    FCONFIG->init_config(init_file);

    // Pars command line arguments
    while ((c = getopt(argc, argv, "bdf:hlVZ")) != EOF)
    {
        switch (c)
        {
        case 'b':       ui = DbUser::UI_NONE;
                        break;
        case 'd':       FCONFIG->set_update_on_startup(0);
                        break;
        case 'f':       FCONFIG->set_db_file(optarg);
                        break;
        case 'h':       usage();
                        return 0;
        case 'l':       ui = DbUser::UI_CLI;
                        break;
        case 'V':       cerr << "version: " << PROGRAM_VERSION << endl;
                        return 0;
        case 'Z':       ui = DbUser::UI_CLI;
                        FCONFIG->set_dumping_db();
                        break;
        default: usage();
                 return -1;
        }
    }

    // Create UI and start processing loop
    dbuser = DbUser::create(ui);
    dbuser->do_loop();

    return 0;
}

/*
 * FUNCTION:    init_trace
 *
 * DESCRIPTION: This function initializes the trace module
 *
 * IN:
 * IN-OUT:
 * RETURN CODE:
 */

void init_trace ()
{
    Trace::create();

    TRACE_CFG.set_mask(ALL, NORMAL | WARNING | ERROR | FATAL);

    TRACE_CFG.set_level_description(ALL, NORMAL, "Normal");
    TRACE_CFG.set_level_description(ALL, WARNING, "Warning");
    TRACE_CFG.set_level_description(ALL, ERROR, "Error");
    TRACE_CFG.set_level_description(ALL, FATAL, "Fatal");

    TRACE_CFG.enable(Trace::FLAGS_CALLER            |
                     Trace::FLAGS_ALIGNMENT         |
                     Trace::FLAGS_LEVEL_DESCRIPTION);
}


syntax highlighted by Code2HTML, v. 0.9.1