/* Author: Ram Samudrala (me@ram.org)
 * Version: O1.0
 * Detail: <http://www.ram.org/computing/player/player.html>
 * January 27, 1995. 
 *
 * See the URL above for more information.
 *
 * player: Expects a song name and format which can be parsed
 *         by get_location(), and a flag  whose # indicates the mirror
 *         location.  All definitions are given in the header.  
 *         Without arguments, it invokes the form that is the player.
 */

#include "cgi_common.h"
#include "cgi_defines.h"
#include "cgi_error_handlers.h"

/******************************************************************/
/* Stuff you probably need to change  */

/* Default location */
/* #define REGULAR_LOCATION "www.twisted-helices.com/th/sound/" */
#define REGULAR_LOCATION "www.ram.org/th/sound/"

/* Location of first mirror */
#define MIRROR_LOCATION1 REGULAR_LOCATION

/* Location of the player (the form that calls this program) */
/* #define PLAYER_LOCATION "http://www.twisted-helices.com/th/" */
#define PLAYER_LOCATION "http://www.ram.org/th/"

/* Location of log file for people accessing your songs */
#define PLAYER_LOG "/home/ram/tmp/player.log"

/* String to identify real audio format */
#define REAL_AUDIO_FORMAT "real"

/* Default directory to look in when queried with GET */
#define DEFAULT_DIRECTORY "tatp/"

int content_type_displayed = FALSE;

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

void get_location(char item_name[], char format[], int mirror_flag, char location[])
{
  char site[100], item_string[50];
  
  if (mirror_flag == 1)
    strcpy(site, MIRROR_LOCATION1);
  else
    strcpy(site, REGULAR_LOCATION);

  item_string[0] = '\0';
  /* All the items from Traversing a Twisted Path */
  if (strstr(item_name, "Tick in My Head 1") != NULL)
    strcpy(item_string, "tatp/timh1");
  if (strstr(item_name, "Emanon") != NULL)
    strcpy(item_string, "tatp/emanon");
  if (strstr(item_name, "Noises") != NULL)
    strcpy(item_string, "tatp/noises");
  if (strstr(item_name, "I, The State, am The People") != NULL)
    strcpy(item_string, "tatp/its");
  if (strstr(item_name, "Hate Session 1") != NULL)
    strcpy(item_string, "tatp/sih");
  if (strstr(item_name, "Let us Blame God") != NULL)
    strcpy(item_string, "tatp/lubg");
  if (strstr(item_name, "Tick in My Head 2-3") != NULL)
    strcpy(item_string, "tatp/timh23");
  if (strstr(item_name, "Morals are Arbitrary") != NULL)
    strcpy(item_string, "tatp/maa");
  if (strstr(item_name, "The Lie") != NULL)
    strcpy(item_string, "tatp/lie");	  
  if (strstr(item_name, "Lynno and Reeno") != NULL)
    strcpy(item_string, "tatp/lar");
  if (strstr(item_name, "Don't You Believe in Me") != NULL)
    strcpy(item_string, "tatp/dybim");
  if (strstr(item_name, "4 Primes") != NULL)
    strcpy(item_string, "tatp/4primes");
  if (strstr(item_name, "I saw Kobain in a 7-11") != NULL)
    strcpy(item_string, "tatp/iskia711");
  if (strstr(item_name, "Kill 'em All") != NULL)
    strcpy(item_string, "tatp/kea");  
  if (strstr(item_name, "Elise") != NULL)
    strcpy(item_string, "tatp/fe");
  if (strstr(item_name, "The Rap of the Morons") != NULL)
    strcpy(item_string, "tatp/rotm");
  if (strstr(item_name, "All the Things in My Room") != NULL)
    strcpy(item_string, "tatp/attimr");
  if (strstr(item_name, "Hate Session 2: Furball in Throat") != NULL)
    strcpy(item_string, "tatp/fit");
  if (strstr(item_name, "Walking in the Dark") != NULL)
    strcpy(item_string, "tatp/witd");
  if (strstr(item_name, "The Last Prime") != NULL)
    strcpy(item_string, "tatp/tlp");

  /* All the items from Under the Covers with Twisted Helices */
  if (strstr(item_name, "Stormbringer") != NULL)
    strcpy(item_string, "tutc/st");
  if (strstr(item_name, "Love is a Weed") != NULL)
    strcpy(item_string, "tutc/liaw");

  /* All the items from Twisting in the Wind  */
  if (strstr(item_name, "Renaissance") != NULL)
    strcpy(item_string, "titw/rena");
  if ((strstr(item_name, "Love") != NULL) && (strstr(item_name, "Weed") == NULL))
    strcpy(item_string, "titw/love");
  if (strstr(item_name, "Relationships") != NULL)
    strcpy(item_string, "titw/rela");
  if (strstr(item_name, "Technology") != NULL)
    strcpy(item_string, "titw/tech");
  if (strstr(item_name, "Oppression") != NULL)
    strcpy(item_string, "titw/oppr");

  /* All the items from Proteomusic: Twisted Music Inspired by Genomes and Proteomes  */
  if (strstr(item_name, "translation") != NULL)
    strcpy(item_string, "proteomusic/translation");
  if (strstr(item_name, "design") != NULL)
    strcpy(item_string, "proteomusic/design");
  if (strstr(item_name, "twisted helices") != NULL)
    strcpy(item_string, "proteomusic/twisted_helices");

  if (item_string[0] == '\0')
    display_error("get_location(): unknown item encountered!\n");
  
  if (strstr(format, ".au") != NULL)
    sprintf(location, "http://%s%s%s", site, item_string, "1.au");
  else
    if (strstr(format, ".mp2 excerpt") != NULL)
      sprintf(location, "http://%s%s%s", site, item_string, "2.mp2");
    else
      if (strstr(format, ".mp2 full song") != NULL)
	sprintf(location, "http://%s%s%s", site, item_string, "3.mp2");
      else
	if (strstr(format, REAL_AUDIO_FORMAT) != NULL)
	  sprintf(location, "http://%s%s%s", site, item_string, "4.ra");
	else
	  if (strstr(format, ".mp3 download") != NULL)
	    sprintf(location, "http://%s%s%s", site, item_string, "5.mp3");
	  else
	    if (strstr(format, ".mp3 stream") != NULL)
	      sprintf(location, "http://%s%s%s", site, item_string, "6.m3u");
	    else
	      if (strstr(format, ".mid (MIDI) download") != NULL)
		sprintf(location, "http://%s%s%s", site, item_string, "7.mid");
	      else
		if (strstr(format, "lyrics") != NULL)
		  sprintf(location, "%s%s%s", "/th/", item_string, ".html");
		else
		  display_error("get_location(): unknown format encountered!");
  
  return;
}
  
/******************************************************************/

int main(int argc, char *argv[]) 
{
  entry entries[MAX_ENTRIES];
  int i, number_of_entries, MIRROR_FLAG = FALSE;
  char location[300], item_name[100], format[50];

  if (strcmp(getenv("REQUEST_METHOD"), "POST") != 0) 
    {
      if (argc == 2)
	{
	  if (strstr(argv[1], "4.ra") != NULL)
	    {
	      printf("Content-type: audio/x-pn-realaudio%s", HEADER_DELIMITER);
	      printf("%s", HEADER_DELIMITER);
	      printf("http://%s%s\n", REGULAR_LOCATION, argv[1]);
	    }
	  else
	    {
	      if (strstr(argv[1], "/") != NULL)
		printf("Location: http://%s%s%s", REGULAR_LOCATION, argv[1], HEADER_DELIMITER);
	      else /* Default is the tatp album */
		printf("Location: http://%s%s%s%s", REGULAR_LOCATION, DEFAULT_DIRECTORY,
		       argv[1], HEADER_DELIMITER);
	      printf("%s", HEADER_DELIMITER);
	    }
	}
      else
	{
	  printf("Location: %s%s", PLAYER_LOCATION, HEADER_DELIMITER);
	  printf("%s", HEADER_DELIMITER);
	}
      return TRUE;
    }

  check_content_type("main");
  number_of_entries = read_entries(entries);
  
  for(i = 0; i <= number_of_entries; i++) 
    {
      if (strcmp(entries[i].name, "mirror") == 0)
        if (strcmp(entries[i].val, "1") == 0)
          MIRROR_FLAG = 1;
      if (strcmp(entries[i].name, "item") == 0)
        strcpy(item_name, entries[i].val);
      if (strcmp(entries[i].name, "format") == 0)
        strcpy(format, entries[i].val);
    }
  
  get_location(item_name, format, MIRROR_FLAG, location);  
  
  if ((strstr(format, "real") == NULL) && (strstr(format, "RA") == NULL) 
      && (strstr(format, "ra") == NULL)) /* If format is not Real Audio */
    {
      printf("Location: %s%s", location, HEADER_DELIMITER);
      printf("%s", HEADER_DELIMITER);
    }
  else
    {
      printf("Content-type: audio/x-pn-realaudio%s", HEADER_DELIMITER);
      printf("%s", HEADER_DELIMITER);
      printf("%s\n", location);
    }
  
#ifdef LOG_USERS
  {
    FILE *log_fp;
    char user[200]; char time_string[100];
    time_t tp;
    
    if ((log_fp = fopen(PLAYER_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
  
  return TRUE;
}

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

