/* 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	<string.h>
#include	<unistd.h>
#include    <errno.h>
#include	<sys/types.h>

#include	"getcap.h"
#include	"defs.h"
#include	"vc.h"
#include	"term.h"
#include 	"font.h"

#ifdef HAVE_SYS_WAIT_H
#include    <sys/wait.h>
#endif

static char *startupStr, *execProg;

static char *execProgArgs[256];
int ConfigExecProg(const char *string, char *args[])
{
    int i=0;
    execProg = strdup(string);
    for (i = 0; i < 256; i++) {
      if (args[i])
          execProgArgs[i] = args[i];
      else
          break;
    }
    execProgArgs[i]=NULL;
    return SUCCESS;
}

static int	ConfigStartup(const char *string)
{
	if (string) startupStr = strdup(string);
	return SUCCESS;
}

void    RunStartupCmd(void)
{
#ifdef HAVE_WAITPID
    char *p = NULL;
    int StartupPid;
    int stat;

    if (startupStr) p = strtok(startupStr, "\n");
    while(p) 
	{

      	if ((StartupPid = fork()) < 0) 
		{
        	PerrorExit("fork");
      	}
      
		if (StartupPid != 0) 	/* I'm parent. */
		{
			waitpid(StartupPid, &stat, 0);
        	if(WIFEXITED(stat) == 0)
          		PerrorExit("StartupCmd");
      	} 
	  	else /* I'm child */
	  	{
        	char *argv[4];

        	setgid(getgid());
        	setuid(getuid());

        	argv[0] = "sh";
        	argv[1] = "-c";
        	argv[2] = p;
        	argv[3] = NULL;
        	execv("/bin/sh", argv);
        	_exit(0);
      	}
      	p = strtok(NULL, "\n");
    }
#endif
}

static bool startupMessage;

static int	ConfigMessage(const char *confstr)
{
	startupMessage = BoolConf(confstr);
	return SUCCESS;
}

void	ChildInit(void)
{
	DefineCap("StartupMessage", ConfigMessage, "On");
	DefineCap("StartUp", ConfigStartup, NULL);
}

void	ChildCleanup(void)
{
	if (startupStr) 
	{
		free(startupStr);
		startupStr = NULL;
	}
}

void	ChildStart(FILE *errfp)
{
	char	*tail, *tcap;
	char	buff[256];

#ifdef HAVE_GETUID

#if !defined(__BEOS__) && !defined(__Minix__)

	seteuid(getuid());
	setegid(getgid());
#endif

	setgid(getgid());
	setuid(getuid());

#endif

	RunStartupCmd();

	buff[0] = '\0';  /* empty string */
	
	if (dispInfo.type == TYPE_GGI || dispInfo.type == TYPE_SDL)
		sprintf(buff, "TERM=ansi");
	else
	{
#if defined(linux)
		sprintf(buff, "TERMCAP=:co#%d:li#%d:tc=console:",
			dispInfo.txmax, dispInfo.tymax);

#endif
	}

	if (buff[0])  /* non-empty */
	{
		tcap = strdup(buff);
		putenv(tcap);
	}

	if (startupMessage)
    {
	    printf("\rCCE Console/X11 CJK Environment " CCE_VERSION_STR
		   "\r\n"
		   "%s\r\n\r\n",

   "Copyright (C) 1998-2003  Rui He (rhe@3eti.com)");

    }

	fflush(stdout);

	if (execProg)
	    execlp(execProg, execProg, 0);
	else 
        {

	    if ((execProg = getenv("SHELL")) == NULL)
		execProg = "/bin/sh";
 		
	    if ((tail = rindex(execProg, '/')) == NULL)
			tail = " sh";
		snprintf(buff, sizeof(buff), "-%s", tail + 1);
	    execl(execProg, buff, 0);
	}
	fprintf(errfp, "CCE> couldn't exec shell\r\n");
	fprintf(errfp, "%s: %s\r\n", execProg, strerror(errno));
	exit(EXIT_FAILURE);
}
