/* Author: Ram Samudrala (me@ram.org).
 *
 * January 27, 1995.
 *
 * tmplayer: Expects a song name and format which can be parsed
 *           by get_url(), and a flag  whose # indicates the mirror
 *           location.  All definitions are given in the header.  
 *           Without arguments, it invokes the form that is the TMP.
 *           It needs be to linked with util.c.
 *           
 */

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

/* Location of first mirror */
#define MIRROR_LOCATION1 "http://www.nvg.unit.no/sounds/songs/purple_tribute/"

/* Default location */
#define REGULAR_LOCATION "http://www.thehighwaystar.com/rosas/tribute/"

/* Location of the Twisted Music Player (the form that calls this program) */
#define ISSPLAYER_LOCATION "/music/purple_tribute/iss.html"

/* Location of log file for people accessing your songs */
#define ISSPLAYER_LOG "/home/ram/www/music/purple_tribute/iss_log"

/******************************************************************/

#define MAX_ENTRIES 10000

#define TRUE 1
#define FALSE 0

typedef struct 
{
  char *name;
  char *val;
} entry;

char *makeword(char *line, char stop);
char x2c(char *what);
char *fmakeword(FILE *f, char stop, int *len);
void unescape_url(char *url);
void plustospace(char *str);

void get_url(char song_name[], char format[], int mirror_flag, char location[]);

int content_type_displayed = FALSE;

/******************************************************************/

void main(int argc, char *argv[]) 
{
  entry entries[MAX_ENTRIES];
  register int x,m=0;
  int cl, MIRROR_FLAG = FALSE;
  char url[300], song_name[100], format[50];

  if (strcmp(getenv("REQUEST_METHOD"), "POST") != 0) 
    {
      printf("Location: %s%c%c", ISSPLAYER_LOCATION, 10, 10);
      return;
    }
  
  if(strcmp(getenv("CONTENT_TYPE"),"application/x-www-form-urlencoded"))
    {
      printf("Content-type: text/html%c%c",10,10);  
      printf("This script can only be used to decode form results. \n");
      exit(1);
    }

  cl = atoi(getenv("CONTENT_LENGTH"));  
  for(x=0;cl && (!feof(stdin));x++) 
    {
      m=x;
      entries[x].val = fmakeword(stdin,'&',&cl);
      plustospace(entries[x].val);
      unescape_url(entries[x].val);
      entries[x].name = makeword(entries[x].val, '=');

      if (strcmp(entries[x].name, "mirror") == 0)
	if (strcmp(entries[x].val, "1") == 0)
	  MIRROR_FLAG = 1;
      if (strcmp(entries[x].name, "song") == 0)
	strcpy(song_name, entries[x].val);
      if (strcmp(entries[x].name, "format") == 0)
	strcpy(format, entries[x].val);
    }

  get_url(song_name, format, MIRROR_FLAG, url);  

  printf("Location: %s%c%c", url, 10, 10);
  return;
}

/******************************************************************/

void get_url(char song_name[], char format[], int mirror_flag, char location[])
{
  char site[100], song_string[50];
  
  if (mirror_flag == 1)
    strcpy(site, MIRROR_LOCATION1);
  else
    strcpy(site, REGULAR_LOCATION);

  if (strstr(song_name, "Wring that Neck") != NULL)
    strcpy(song_string, "wn");
  else
    if (strstr(song_name, "Child in Time") != NULL)
      strcpy(song_string, "ct");
    else
      if (strstr(song_name, "Living Wreck") != NULL)
	strcpy(song_string, "lw");
      else
	if (strstr(song_name, "Fools") != NULL)
	  strcpy(song_string, "fo");
	else
	  if (strstr(song_name, "Highway Star") != NULL)
	    strcpy(song_string, "hs");
	  else
	    if (strstr(song_name, "Smoke on the Water") != NULL)
	      strcpy(song_string, "sm");
	    else
	      if (strstr(song_name, "When a Blind Man Cries") != NULL)
		strcpy(song_string, "bm");
	      else
		if (strstr(song_name, "Burn") != NULL)
		  strcpy(song_string, "bu");
		else
		  if (strstr(song_name, "Woman from Gaza") != NULL)
		    strcpy(song_string, "wg");
		  else
		    if (strstr(song_name, "Stormbringer") != NULL)
		      strcpy(song_string, "st");
  
  if (strstr(format, ".au") != NULL)
    sprintf(location, "%s%s%s", site, song_string, "1.au");
  else
    if (strstr(format, ".mp2 excerpt") != NULL)
      sprintf(location, "%s%s%s", site, song_string, "2.mp2");
    else
      if (strstr(format, ".mp2 full song") != NULL)
	sprintf(location, "%s%s%s", site, song_string, "3.mp2");
      else
	if (strstr(format, "lyrics") != NULL)
	  sprintf(location, "%s%s%s", "/music/purple_tribute/", song_string, ".html");

#ifdef ISSPLAYER_LOG
  {
    FILE *log_fp;
    char user[200]; char time_string[100];
    time_t tp;
    
    if ((log_fp = fopen(ISSPLAYER_LOG, "a")) != NULL)
      {
	user[0] = '\0';
	if (getenv("REMOTE_IDENT"))
	  sprintf(user, "%s@", getenv("REMOTE_IDENT"));
	if (getenv("REMOTE_HOST"))
	  strcat(user, getenv("REMOTE_HOST"));
	else
	  if (getenv("REMOTE_ADDR"))
	    strcat(user, getenv("REMOTE_ADDR"));
	tp = time(NULL);
	strcpy(time_string, ctime(&tp));
	time_string[strlen(time_string)-1] = '\0';
	fprintf(log_fp, "%s - %s - %s\n", time_string, user, location);
	fclose(log_fp);
      }
  }
#endif

}
  
/******************************************************************/
   
