// RMP
// random play mp3 files
//
// this file is from Peter Jodda, and under the GPL


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <time.h>

#define HISTORY_SIZE 10
int history[HISTORY_SIZE];

class stringList
{
   public:
   char *m_pc_String;
   class stringList *m_pc_Next;

   stringList::stringList(char *string);
};


stringList::stringList(char *string)
{
   int i_Number;

   i_Number =strlen(string);

   m_pc_String = new char [i_Number+1];
   if(m_pc_String != NULL)
   {
      strcpy(m_pc_String,string);
   }
   m_pc_Next = NULL;
} // stringList() -- constructor



int main(int argc, char *argv[])
{
   stringList *pc_Root;
   FILE       *fp_MusikNameFile;

   char       ca_TempName[999];
   char       ca_ExecString[999];
   char       ca_InLine[999];

   int        i_NumberOfStrings=0;
   int        i;

   // create a temporary file name
   strcpy(ca_TempName,"/tmp/rmp_tempfile.tmp");

   sprintf(ca_ExecString,"find . -name '*.mp3' >%s",ca_TempName);

   //fprintf(stderr,"<%s>\n",ca_ExecString);

   system(ca_ExecString);

   fp_MusikNameFile = fopen(ca_TempName,"r");

   pc_Root = NULL;
   while( ! feof(fp_MusikNameFile) )
   {
      char *pc_ReadLine;

      // read in a line with the filenames
      pc_ReadLine =fgets(ca_InLine,999,fp_MusikNameFile);

      if(   pc_ReadLine != NULL
         && strlen(pc_ReadLine)>1)
      {
         stringList *pc_NewElement;
         char       *pc_EndChar;

         pc_EndChar=strchr(ca_InLine,(int) '\n');
         if(pc_EndChar != NULL)
         {
            *pc_EndChar ='\0';
         }

         pc_NewElement = new stringList(ca_InLine);
         if(pc_NewElement != NULL)
         {
            pc_NewElement->m_pc_Next = pc_Root;
            pc_Root = pc_NewElement;
            i_NumberOfStrings += 1;
         }
      }

   }
   fclose(fp_MusikNameFile);
   unlink(ca_TempName);

   // now the strings are read in

   // set random number generator by the current date
   srand(time(NULL));


   // no song available, then terminate
   if(i_NumberOfStrings<=0)
   {
      return 1;
   }

   for(i=0;i<HISTORY_SIZE;i++)
   {
       history[i] = -1; // unused
   }

   // loop for playing the songs randomly
   while(1)
   {
      stringList *pc_CurrentName;
      int        i_CurrentSong;
      int        i_Result;

      // calculte the index of the next song
      if(i_NumberOfStrings<HISTORY_SIZE)
      {
         i_CurrentSong = rand() % i_NumberOfStrings;
      }
      else
      {
         bool b_JustPlayed;

         i_CurrentSong = rand() % i_NumberOfStrings;
         b_JustPlayed  = true;

         while( b_JustPlayed)
         {
            // select a new song
            i_CurrentSong = rand() % i_NumberOfStrings;
            b_JustPlayed = false;

            // look whether new song is in history
            for(i=0;i<HISTORY_SIZE;i++)
            {
               if(i_CurrentSong == history[i])
               {
                  b_JustPlayed = true;
                  break;
               }
            }

         } // while b_JustPlayed

         // shift history down
         for(i=0; i< HISTORY_SIZE -1 ; i++)
         {
            history[i] = history[i +1];
         }

         // add new song as last element in history
         history[HISTORY_SIZE -1] = i_CurrentSong;
      } // else history wird benutzt


      // look for the name of the song
      pc_CurrentName =pc_Root;
      for(i=0; i<i_CurrentSong; i++)
      {
         pc_CurrentName = pc_CurrentName->m_pc_Next;
      }
      fprintf(stderr,"Now playing Number %d File:%s\n",
                      i,
		      pc_CurrentName->m_pc_String);

      sprintf(ca_ExecString,"mpg123 %s",pc_CurrentName->m_pc_String);
      i_Result = system(ca_ExecString);

      // wait a small time before starting the next one
      usleep(500000);
      usleep(500000);
      usleep(500000);
      usleep(500000);
      usleep(1);

   } // forever

} // main()


