/* XMMS Announcer
   by Josh Kearney
   http://jk0.org
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <xmms/xmmsctrl.h>

#define VERSION "0.09.5"

/* Print version information */
void printVersion()
{
	printf("XMMS Announcer v%s\n"
	       "by Josh Kearney\n"
	       "http://jk0.org\n",VERSION);
}

/* Print help information */
void printHelp(char argv[])
{
	printf("Usage: %s [OPTION] [<args>]\n"
	       "List the song currently playing in XMMS.\n"
	       "  -v\tPrint version information.\n"
	       "  -h\tPrint this help message.\n"
	       "  -a\tPrint detailed information about the current track.\n"
	       "  -f <format>\tDefine your own output string format.\n"
                   "\t\t\t%%s = Artist - Song\n"
                   "\t\t\t%%t = Time (0:00)\n"
                   "\t\t\t%%p = Position [0/000]\n"
                   "\t\t\tEx: %s -f \"[%%p] %%s (%%t)\"\n",argv,argv);
}

/* Replace given text in a string with provided replacement */
char *str_replace(const char *original, char *destination, const char *token, const char *replaceWith)
{
	char *pt1=NULL;
	char *pt2=NULL;
	int origLen=-1;

	origLen=strlen(original);

	pt1=(char*)original;
	pt2=strstr(pt1,token);
	
	while(pt1!=NULL)
	{
		if((pt1!=NULL) && (pt2!=NULL))
		{
			strncat(destination,pt1,pt2-pt1);
			strcat(destination,replaceWith);
			pt1=pt2+strlen(token);

			if((pt1-original)<origLen)
				pt2=strstr(pt1,token);
			else
				break;
		} else if((pt1!=NULL) && (pt2==NULL))
		{
			strcat(destination,pt1);
			break;
		}
	}

	return destination;
}

/* Concantinates two ints to the left/right of the separator, returning char */
char *catInt(int a, int b, const char c[])
{
	char *endresult=malloc(10);
	sprintf(endresult,"%1d%s%02d",a,c,b);
	return endresult;
}

/* Print current track */
void printCurrent(const char i[], const char f[])
{
        int pos,listLen,currentTime,totalTime,session=0;
	char *title;

        pos=xmms_remote_get_playlist_pos(session);
	listLen=xmms_remote_get_playlist_length(session);
	currentTime=xmms_remote_get_output_time(session);
	totalTime=xmms_remote_get_playlist_time(session,pos);
        title=xmms_remote_get_playlist_title(session,pos);

	if (xmms_remote_is_running(session)==0)
	{
		printf("XMMS is not currently running.\n");
	} else if(xmms_remote_is_playing(session)==0)
        {
                printf("%s [stopped]\n",title);
        } else if (xmms_remote_is_paused(session)==1)
        {
                printf("%s [paused]\n",title);
        } else
        {
		int currentPos=pos+1;
		int currentMinutes=currentTime/1000/60;
		int currentSeconds=(currentTime/1000)%60;
		int totalMinutes=totalTime/1000/60;
		int totalSeconds=(totalTime/1000)%60;
			
		if(strcmp(i,"all")==0)
		{
                	printf("Track:\t\t%s\nTime:\t\t%d:%02d/%d:%02d\nPosition:\t%d/%d\n",title,currentMinutes,currentSeconds,totalMinutes,totalSeconds,currentPos,listLen);
		} else if(strcmp(i,"formatted")==0)
		{
			int buffSize=1024;
			char originalBuff[1024]="";
			char parsedBuff[1024]="";
			
			/* Parse for track */
			strcpy(originalBuff,f);
			memset(parsedBuff,0,buffSize);
			str_replace(originalBuff,parsedBuff,"%s",title);

			/* Parse for time */
			char *time=malloc(10);
			char *ct=catInt(currentMinutes,currentSeconds,":");
			char *tt=catInt(totalMinutes,totalSeconds,":");
			sprintf(time,"%s/%s",ct,tt);
			free(ct);
			free(tt);
			strcpy(originalBuff,parsedBuff);
			memset(parsedBuff,0,buffSize);
			str_replace(originalBuff,parsedBuff,"%t",time);
			free(time);

			/* Parse for position */
			char *p=catInt(currentPos,listLen,"/");
			strcpy(originalBuff,parsedBuff);
			memset(parsedBuff,0,buffSize);
			str_replace(originalBuff,parsedBuff,"%p",p);
			free(p);

			printf("%s\n",parsedBuff);
		} else
		{
			printf("%s\n",title);
		}
        }
}

/* Main loop */
int main(int argc, char *argv[])
{
	int args;
	opterr=0;

	if(argc<2)
	{
		printCurrent("normal","");
		exit(0);
	}

	while((args=getopt(argc,argv,"vahf:"))!=-1)
	{
		switch(args)
		{
			case 'v':
				printVersion();
				break;
			case 'h':
				printHelp(argv[0]);
				break;
			case 'a':
				printCurrent("all","");
				break;
			case 'f':
				printCurrent("formatted",optarg);
				break;
			default:
				printHelp(argv[0]);
				break;
		}
	}	

	return 0;
}
