/*
 * Copyright (C) 2003-2004 Vladimir A. Smirnov
 *
 * 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.
 */

#include <v_lib.h>
#include "cspace.h"
#include "eval.h"
#include "efunc.h"
#include "util.h"
#include "decomp.h"
#include "ipltrans.h"
#include "messages.h"
#include "istrings.h"

int main(int argc,char *argv[])
{
    CALLER_NAME(main)
    vhandle_t script;
    vboolean result = true;
    u32_t nbytes = 0, nbytes_out;
    logStartConsoleLog();
    logUpperLevel(LOG_INFO);
    if(argc != 2)
    {
        conPuts(ipl_usage);
        return -1;
    }
    if(STR_EQ(argv[1],"--help"))
    {
        conPuts(ipl_usage);
        goto normal_exit;
    }
    if(STR_EQ(argv[1],"--help-ipl"))
    {
        iplHelp();
        goto normal_exit;
    }
    script = lexParseFile(argv[1],IPL_OPEN_BRACE IPL_CLOSE_BRACE \
        IPL_OPEN_BRACKET IPL_CLOSE_BRACKET IPL_COMMA IPL_SEMICOLON);
    if(script == LIBV_INVALID_HANDLE)
    {
        ERROR_GENERIC_2ARG(ipl_unable_to_open,argv[1]);
        return -1;
    }
    lexSetInteger(script,LEX_COMMENT_CHAR,IPL_COMMENT_CHAR);
    lexEnable(script,LEX_ONE_CHAR_COMMENTS);
    lexEnable(script,LEX_ALIAS_TRANSLATION);
    result = evalGlobals(script);
    if(result)
    {
        random_gen = ranCreateGenerator(random_seed);
        tic_start = timeTickCount();
        V_ASSERT(random_gen);
        result = evalTargets(script);
    }
    memStats(&nbytes,NULL,NULL);
    ranDestroyGenerator(random_gen);
    cspaceCollectGarbage();
    evalCollectGarbage();
    evalFreeFunctions();
    lexDestroy(script);
    iplFreeTransforms();
normal_exit:;
    logStopConsoleLog();
    memStats(&nbytes_out,NULL,NULL);
    printf(ipl_exit_status,
        result ? ipl_success : ipl_error, nbytes, nbytes_out);
    return (result) ? 0 : -1;
}
