int mainService(int argc, char *argv[])
{
	char	sourceURL[1024] = "";
	char	configFile[1024] = "";
	int		c = 0;
	int 	numEncoders = 1;


    char    currentlogFile[1024] = "";
	char	logPrefix[255] = "streamTranscoder";

    sprintf(configFile, "%s", logPrefix);
    sprintf(currentlogFile, "%s", logPrefix);

    setgLogFile(&gMain, currentlogFile);
    setConfigFileName(&gMain, configFile);

	addConfigVariable(&gMain, "SourceURL");
	addConfigVariable(&gMain, "NumEncoders");
	addConfigVariable(&gMain, "AutomaticReconnectSecs");
	addConfigVariable(&gMain, "AutoConnect");
	addConfigVariable(&gMain, "LogLevel");
	addConfigVariable(&gMain, "LogFile");


    readConfigFile(&gMain, 0);

	strcpy(sourceURL, gMain.gSourceURL);

    for(int i = 0; i < gMain.gNumEncoders; i++) {
        if(!g[i]) {
            g[i] = (edcastGlobals *) malloc(sizeof(edcastGlobals));
            memset(g[i], '\000', sizeof(edcastGlobals));
        }

        g[i]->encoderNumber = i + 1;
        setgLogFile(g[i], gMain.gLogFile);
        setConfigFileName(g[i], gMain.gConfigFileName);
		addBasicEncoderSettings(g[i]);
        initializeGlobals(g[i]);
        edcast_init(g[i]);
		connectToServer(g[i]);
    }

	pthread_create(&sourceThread, NULL, &startSourceThread, sourceURL);
	pthread_create(&reconnectThread, NULL, &startReconnectThread, NULL);
	pthread_create(&decodeThread, NULL, &startDecodeThread, NULL);
	pthread_join(sourceThread, NULL);
	pthread_join(decodeThread, NULL);
	return 1;
}
int main(int argc, char *argv[])
{
	char	sourceURL[1024] = "";
	char	configFile[1024] = "";
	int		c = 0;
	int 	numEncoders = 1;
	bool	background = false;

	while ((c = getopt(argc, argv, "bnh")) != -1) {
		switch (c) {
			case 'h':
				usage();
				break;
			case 'b':
				background = true;
				nostdout = true;
			case 'n':
				nostdout = true;
				break;
			default:
				usage();
		}
	}

	if (background) {
		fprintf(stderr, "Backgrounding process...\n");
		/* Shamelessly ripped from ICES */
		int ret = 0;
		/* Start up new session, to lose old session and process group */
		switch (fork()) {
			case 0: 	break; /* child continues */
			case -1: 	perror ("fork"); 
						ret = -1;
			default:
						exit (ret);
		}

		/* Disassociate process group and controlling terminal */
		setsid();

		/* Become a NON-session leader so that a */
		/* control terminal can't be reacquired */
		switch (fork()) {
			case 0: 	break; /* child continues */
			case -1: 	perror ("fork"); 
						ret = -1;
			default:
						exit (ret);
		}
	}

    char    currentlogFile[1024] = "";
    char    logPrefix[255] = "streamTranscoder";

	int		configFilesCreated = 0;
	int		encoderconfigFilesCreated = 0;

    
    sprintf(configFile, "%s", logPrefix);
    sprintf(currentlogFile, "%s", logPrefix);

    setDefaultLogFileName(currentlogFile);
    setgLogFile(&gMain, currentlogFile);
    setConfigFileName(&gMain, configFile);
    addConfigVariable(&gMain, "SourceURL");
    addConfigVariable(&gMain, "NumEncoders");
    addConfigVariable(&gMain, "AutomaticReconnectSecs");
    addConfigVariable(&gMain, "AutoConnect");
    addConfigVariable(&gMain, "LogLevel");
    addConfigVariable(&gMain, "LogFile");

    char    tmpLog[1024] = "";
	sprintf(tmpLog, "%s_%d.cfg", logPrefix, 0);
	if (!file_exists(tmpLog)) {
		printf("Creating config file %s\n", tmpLog);
		configFilesCreated = 1;
	}
	
    readConfigFile(&gMain, 0);


    for(int i = 0; i < gMain.gNumEncoders; i++) {
        if(!g[i]) {
            g[i] = (oddcastGlobals *) malloc(sizeof(oddcastGlobals));
            memset(g[i], '\000', sizeof(oddcastGlobals));
        	initializeGlobals(g[i]);
            addBasicEncoderSettings(g[i]);
        }
		else {
        	initializeGlobals(g[i]);
		}
    

        g[i]->encoderNumber = i + 1;
        char    currentlogFile[1024] = "";
        char    logPrefix[255] = "streamTranscoder";

        sprintf(currentlogFile, "%s_%d", logPrefix, g[i]->encoderNumber);

        setDefaultLogFileName(currentlogFile);
        setgLogFile(g[i], currentlogFile);
        setConfigFileName(g[i], gMain.gConfigFileName);

		sprintf(tmpLog, "%s.cfg", currentlogFile);
		if (!file_exists(tmpLog)) {
			printf("Creating config file %s\n", tmpLog);
			configFilesCreated = 1;
			encoderconfigFilesCreated = 1;
		}

        oddcastv3_init(g[i]);
    }

	if (configFilesCreated) {
		printf("One or more config files were created.\n");
		printf("Source settings are contained in %s_0.cfg\n", logPrefix);
		printf("All encoder settings are contained in %s_X.cfg, where X is the encoder number.\n", logPrefix);
		printf("\nYou must manually edit these config files before continuing.\n");
		if (encoderconfigFilesCreated) {
			printf("Your next step should probably be to ****edit %s_X.cfg where X = 1 to number of encoders**** you've defined, then rerun streamTranscoder\n", logPrefix);
		}
		else {
			printf("Your next step should probably be to ****edit %s_0.cfg and change the number of encoders****, then rerun streamTranscoder\n", logPrefix);
		}
		exit(1);
	}
    outputStatusCallback(&gMain, (void *)"Ready to connect");

	for(int i = 0; i < gMain.gNumEncoders; i++) {
		if (!connectToServer(g[i])) {
			char buf[255] = "";
			sprintf(buf, "Disconnected from server");
			g[i]->forcedDisconnect = true;
			g[i]->forcedDisconnectSecs = time(&(g[i]->forcedDisconnectSecs));
			g[i]->serverStatusCallback(g[i], (void *) buf);
		}
	}


	pthread_create(&sourceThread, NULL, &startSourceThread, gMain.gSourceURL);
	pthread_create(&reconnectThread, NULL, &startReconnectThread, NULL);
	pthread_create(&decodeThread, NULL, &startDecodeThread, NULL);

	sleep(2);
	if (!nostdout) {
		printf("streamTranscoder started.\n");
	}
	pthread_join(sourceThread, NULL);
	pthread_join(decodeThread, NULL);
	if (!nostdout) {
		fprintf(stdout, "Done...\n");
	}
	
}