
#include "driver.h"
#include "initval.h"

#define MAX_DETECT	32
#define FALSE		0
#define TRUE		1
#define CMD_START	"start"
#define AUDIOTRAP_OPTIONS	"qvn:aX:F:"
#define OPT_QUIET	'q'
#define OPT_VERBOSE	'v'
#define OPT_NUMBER	'n'
#define OPT_ALL		'a'
#define OPT_XCLUDE	'X'
#define OPT_FORCE	'F'

typedef int SndDetectCmdlineF_t( int *count, char **cmdline, int verbose );

SndDetectCmdlineF_t snd_audiopci_cmdline;
SndDetectCmdlineF_t snd_sblive_cmdline;
SndDetectCmdlineF_t snd_supersouth_cmdline;
SndDetectCmdlineF_t snd_esssolo_cmdline;
SndDetectCmdlineF_t snd_cs461x_cmdline;
SndDetectCmdlineF_t snd_4dwave_cmdline;
SndDetectCmdlineF_t snd_sb_cmdline;

struct snd_stru_detect {
	char *name;
	SndDetectCmdlineF_t *cmdline;
} detect[] = 	{ \
			{ "audiopci", snd_audiopci_cmdline }, \
			{ "sblive", snd_sblive_cmdline }, \
			{ "supersouth", snd_supersouth_cmdline }, \
			{ "4dwave", snd_4dwave_cmdline }, \
			{ "esssolo", snd_esssolo_cmdline }, \
			{ "cs461x", snd_cs461x_cmdline }, \
			{ "sb", snd_sb_cmdline }, \
			/* Add new entries above this line */ \
			{ NULL, NULL } \
		}; 
int verbose = 1;
int max_detect = 1;		// maximum number of cards to detect before returning
int start_cmds = FALSE;

int parse_options( int argc, char *argv[] )
{
	int c;

	if( argv[1] && strncmp( argv[1], CMD_START, sizeof(CMD_START)) == 0 )
		start_cmds = TRUE;

	while(( c = getopt( argc, argv, AUDIOTRAP_OPTIONS )) != -1 ) {
		switch(c) {
		    case OPT_QUIET:
			verbose = 0;
			break;
		    case OPT_VERBOSE:
			verbose++;
			break;
		    case OPT_NUMBER:
			max_detect = atoi(optarg);
			break;
		    case OPT_ALL:
			max_detect = MAX_DETECT;
			break;
		    case OPT_XCLUDE:
			break;
		    case OPT_FORCE:
			break;
		    default:
			break;
		};
	}
	return 0;
}

#define MAX_ARGS 100

int run_cmd( char *cmdline )
{
	char *argv[MAX_ARGS];
	char *arg = cmdline,
	     *arg_end = NULL;
	int idx = 0;

	memset( argv, 0, sizeof(argv));
	while( arg && ( idx < MAX_ARGS-1 )) {
		argv[idx++] = arg;
		if(( arg_end = strchr( arg, ' ' )) != NULL ) {
			*arg_end = '\0';
			arg = arg_end++;	
		} else
			break;	
	}
	argv[idx++] = NULL;
	return spawnvp( P_NOWAITO, argv[0], argv );
}

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

	int idx = 0,			// index into snd_stru_detect list 
	    nfound = 0,			// number of cards found so far
	    count = 0;
	char *cmd[MAX_DETECT];		// storage for found commandlines 

	parse_options( argc, argv);

#ifdef __QNXNTO__
	if (ThreadCtl(_NTO_TCTL_IO,0) < 0) {
		perror ("ThreadCtl failed\n");
		return (-1);
	}
#endif
	while(( detect[idx].name ) && ( nfound < max_detect )) {
		int cnt = 0;
		if ( detect[idx].cmdline( &cnt, &cmd[nfound], verbose ) == EOK ) {
			for( count = 0; count < cnt; count++ )
				if( verbose ) printf( "%s &\n", cmd[nfound + count] );
			nfound += cnt;
		}
		idx++;
	}

			

	if( start_cmds ) {
		for( count = 0; count < nfound; count++ ) {
			if( run_cmd( cmd[count] ) < 0 ) {
				char errmsg[PATH_MAX];
				sprintf( errmsg, "Unable to start cmd: %s\n", cmd[count] );
				perror( errmsg );
			}
		}
	} 

	return 0;
}

