/* vi: set sw=4 ts=4: */
/*
 * KON2 - Kanji ON Console -
 * Copyright (C) 1992-1996 Takashi MANABE (manabe@papilio.tutics.tut.ac.jp)
 *
 * CCE - Console Chinese Environment -
 * Copyright (C) 1998-2003 Rui He (rhe@3eti.com)
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE TERRENCE R. LAMBERT BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * 
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>

#include <defs.h>
#include <getcap.h>
#include <errors.h>
#include <term.h>
#include <vc.h>
#include <child.h>
#ifdef SUPPORT_FREETYPE_FONT
#include <ftfont.h>
#endif
#ifdef SUPPORT_ENCODING_FILTER
#include <encfilter.h>
#endif
#ifdef SUPPORT_SDLLIB
#include <SDL/SDL.h> 

#endif

static void     ProcessArgs(int argc, char *argv[]);
extern int cceconvert_main(int argc, char *argv[]);

int main(int argc, char *argv[])
{
	char *progname;

#ifndef HAVE_BASENAME
	progname = argv[0];  /* if you have prefix in argv[0], symlink checking won't work! */
#else
   	progname = (char *)basename(argv[0]);
#endif

	if (!progname) progname = "cce"; /* just make it not-NULL */

	if (strncasecmp(progname, "cceconv", 7) == 0)
		return cceconvert_main(argc, argv);

	OverrideSysCoding = -1;
	if (strncasecmp(progname, "cceb5", 5) == 0 || strncasecmp(progname, "big5cce", 7)==0)
		OverrideSysCoding = CODE_BIG5;
	else if (strncasecmp(progname, "ccegbk", 6) == 0)
		OverrideSysCoding = CODE_GBK;
	else if (strncasecmp(progname, "ccegb", 5) == 0)
		OverrideSysCoding = CODE_GB;
	else if (strncasecmp(progname, "ccejis", 6) == 0)
		OverrideSysCoding = CODE_EUC; /* CODE_SJIS;  */
	else if (strncasecmp(progname, "cceksc", 6) == 0)
		OverrideSysCoding = CODE_KSC; 

    fprintf(stderr,"\r\nCCE> Console/X11 CJK Environment " CCE_VERSION_STR "\n");
    ProcessArgs(argc-1, argv+1);

    TermInit();

    if (ReadConfig( CONFIG_NAME ) < 0) 
    {
		fprintf(stderr, "CCE> Error reading %s\n", CONFIG_NAME );
		exit(EXIT_FAILURE);
    }

    TermStart();

    return 0;
}

static void PrintUsage(void)
{
	printf("\nOptions:\n");
	printf("  -e              Specify the program to execute, default is shell\n");
	printf("  -help           Print CCE's usage information\n");
	printf("  -version        Print CCE's version information\n");
    printf("  -gb|-gb2312     Use GB2312 system encoding, support simplified Chinese\n");
    printf("  -gbk            Use GBK system encoding, support simplified Chinese\n");
    printf("  -b5|-big5       Use Big5 system encoding, support traditional Chinese\n");
    printf("  -jis|-eucjp     Use EUC-JP system encoding, support Japanese\n");
    printf("  -sjis|-shiftjis Use Shift-JIS system encoding, support Japanese\n");
    printf("  -ksc            Use KSC system encoding, support Korean\n");
#ifdef SUPPORT_GGILIB
	printf("  -ggi            Try to use GGI library first\n");
#endif
#ifdef SUPPORT_SDLLIB
	printf("  -sdl            Try to use SDL library first\n");
#endif
#ifdef SUPPORT_FREETYPE_FONT
	printf("  -ttfont         Specify the TrueType Font filename\n");
	printf("  -ttsize         Specify the desired TrueType Font size\n");
#endif
#ifdef SUPPORT_ENCODING_FILTER
	printf("  -filter         Specify the encoding filter such as UTF8\n");
#endif
    printf("\nConfiguration File:\n");
    printf("  %s\n\n", CONFIG_NAME);

}

static void ProcessArgs(int argc, char *argv[])
{
    int i = 0;

    while (i < argc)
    {
        const char      *arg;

        if (argv[i][0] != '-')
        {
            arg = (char *) argv[i];
        } 
		else
		{
            arg = (char *) argv[i] + 1;
			if (arg[0] == '-') arg++;
		}
        i++;

        if (!strcasecmp(arg, "e"))
		{
		    if (i >= argc) 
			{
            	error("no value for `%s'\r\n", arg);
            	break;	
			}
			ConfigExecProg(argv[i],&(argv[i]));
			i++;
		}
		else if (!strcasecmp(arg, "eucjp") || !strcasecmp(arg, "jis"))  /* -eucjp, EUCJP */
            OverrideSysCoding = CODE_EUC;
		else if (!strcasecmp(arg, "sjis") || !strcasecmp(arg, "shiftjis"))  /* -jis, go to Shift-JIS encoding */
			OverrideSysCoding = CODE_SJIS;
		else if (!strncasecmp(arg, "ksc", 3) )   /* KSC */
			OverrideSysCoding = CODE_KSC;
		else if (!strncasecmp(arg, "gbk", 3))   /* GBK */
			OverrideSysCoding = CODE_GBK;
		else if (!strncasecmp(arg, "big", 3) || !strncasecmp(arg, "b5", 2)) /* Big5 */
			OverrideSysCoding = CODE_BIG5;
        else if (!strncasecmp(arg, "gb", 2) )  /* -jis, go to Shift-JIS encoding */
            OverrideSysCoding = CODE_GB;

		else if (!strcasecmp(arg, "help"))
		{
			PrintUsage();
			exit(0);
		}
		else if (!strcasecmp(arg, "version"))
		{
			exit(0);
		}
#ifdef SUPPORT_GGILIB
		else if (!strcasecmp(arg, "ggi"))
		{
			UseGGIFirst = 1;  /* Use GGI first even in console */
		}
#endif
#ifdef SUPPORT_SDLLIB
		else if (!strcasecmp(arg, "sdl"))
		{
			UseSDLFirst = 1;  /* Use SDL first even in console */
		}
#endif
#ifdef SUPPORT_FREETYPE_FONT
		else if (!strcasecmp(arg, "ttfont"))
		{
		    if (i >= argc) 
			{
            	error("You need to specify a Unicoded TrueType font.\r\n");
            	break;	
			}
   			UseTTFont = 1;
			TTFontPathName = argv[i];
			i++;
		}
		else if (!strcasecmp(arg, "ttsize"))
		{
			if (i >= argc)
			{
				error("You need to specify the desired TrueType font size.\r\n");
				break;
			}
			TTFontSize = atoi(argv[i]);
			i++;
		}
#endif
#ifdef SUPPORT_ENCODING_FILTER
		else if (!strcasecmp(arg, "filter"))
		{
			if (i >= argc)
			{
				error("You need to specify the encoding filter such as UTF-8.\r\n");
				break;
			}
			EncodingFilter = argv[i];
			UseEncodingFilter = 1;
			i++;
		}
#endif
		else
		{
			warn("Unknown argument `%s'ignored.\r\n", arg);
		}
    }
}

