/*
    2004.02.01
        first released source code for IOMP
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "struct.h"
#include "workbone.h"
#include <sys/types.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/cdrom.h>

#define VERSION "version 1.5"
#define MAX_TRACKS 1000

struct cdinfo * cd1;
int Delay=15;
extern int cd_fd;
extern int cur_track, cur_index, cur_lasttrack, cur_firsttrack, cur_pos_abs,	
	cur_frame, cur_pos_rel, cur_tracklen, cur_cdlen, cur_ntracks,	
	cur_nsections, cur_cdmode, cur_listno, cur_stopmode, exit_on_eject,
	cur_balance;

#define CMD_PLAY	0
#define CMD_STOP	1
#define CMD_QUIT	2
#define CMD_NEXT	3
#define CMD_PREV	4
#define CMD_PAUSE	5
#define CMD_RESUME	6
#define CMD_NEXTTRACK	7
#define CMD_PREVTRACK	8
#define CMD_READINFO	9
#define CMD_SEEK_FORWARD	10
#define CMD_SEEK_BACKWARD	11
#define CMD_STATUS			12

char *CommandSet[] = { 
	"play",
	"stop",
	"quit",
	"next",
	"prev",
	"pause",
	"resume",
	"next_track",
	"prev_track",
	"read_info",
	"seek_forward",
	"seek_backward",
	NULL
};

extern void read_info();
extern void seek_forward();
extern void seek_backward();
static void RunCommand(int cmd)
{
//	fprintf(stderr,"RunCommand %d\n",cmd);
	switch (cmd) {
		case CMD_PLAY : {
			play_cd(1,0,9999);
		} break;
		case CMD_QUIT : 
		case CMD_STOP : {
			stop_cd();
		} break;
		case CMD_NEXTTRACK : {
			play_to_next();
		} break;
		case CMD_NEXT : {
		} break;
		case CMD_PREVTRACK : {
			play_to_prev();
		} break;
		case CMD_PREV : {
		} break;
		case CMD_PAUSE : {
			pause_cd();
		} break;
		case CMD_RESUME : {
			resume_cd();
		} break;
		case CMD_READINFO: {
			read_info();
		} break;
		case CMD_SEEK_FORWARD: {
			seek_forward();
		} break;
		case CMD_SEEK_BACKWARD: {
			seek_backward();
		} break;
	}
	if (cmd != CMD_STOP)
		cd_status();
}

extern char *cd_device;
#if 1
int main(int argc, char *argv[])
{
//	int drv_status;
	int endcount = 0;
	char command[255];
	
	if (argc > 1) {
		cd_device=strdup(argv[1]);
	} else 
		cd_device = strdup("/dev/cdrom");

//	drv_status=CDmounted();
	switch(cd_status()){
		case 2 :{
		    break;
		      }
		case 1:{sleep(6);
		       break;
		      }
		case 3:
		case 0:{
		    printf("No CD found\n");
		    exit(1);
		     }
		default:
		break;
	}
	read_toc();
//	RunCommand(CMD_TRACKNUM,0);
//	RunCommand(CMD_PLAY);
	
	while(1) {
		int cmdindex = 0;
		fgets(command,255,stdin);
			
		while (CommandSet[cmdindex]) {
			if (strncasecmp(CommandSet[cmdindex],command,strlen(command)-1) == 0) {
				RunCommand(cmdindex);
				endcount = 0;
				if (cmdindex == CMD_QUIT)
					exit(1);
				break;
			} 
			cmdindex++; 
		}	
		
		if (strncmp("test",command,strlen(command)-1) == 0) {
			struct cdrom_subchnl		sc;
			sc.cdsc_format = CDROM_MSF;
			if (ioctl(cd_fd, CDROMSUBCHNL, &sc)) {
				exit(-1);
			}
			if ((sc.cdsc_audiostatus == CDROM_AUDIO_COMPLETED) || (sc.cdsc_audiostatus == CDROM_AUDIO_INVALID)){
				exit(-2);
			}
		} else if (strstr(command,"track") == command) {
			int tr = atoi(command+6);
			play_cd(tr,0,999);
		}
	}
}

#else
#endif

