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

#include "global.h"

#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_cs_util.h"
#include "manhat-lib/shared_server_string.h"
#include "manhat-lib/shared_podcast_util.h"
#include "manhat-lib/shared_translate_url.h"


static void
read_parameters (char *course, char *key, char *title, char *sender, char *sender_realname,
		char *fname, char *infname, int *grp, char *link)
{
  char temp[30];
  
  cs_get_optional_parameter ("link", link, MAX_PATH);

  if(!strlen(link))
  {
  cs_get_required_parameter ("crs", course, MAX_FILENAME);	/* cgihtml library */
  cs_get_required_parameter ("id", key, MAX_KEY);
  cs_get_required_parameter ("grp", temp, 29);
  *grp = atoi(temp);

  cs_get_optional_parameter ("sender", sender, MAX_USERNAME);
  if(strlen(sender))
  {
     cs_get_required_parameter ("title", title, MAX_PATH);
     cs_get_required_parameter ("realname", sender_realname, MAX_NAME);
     cs_get_required_parameter ("fname", fname, MAX_FILENAME);
     cs_get_required_parameter ("inf", infname, MAX_FILENAME);
     cs_set_int_value("framaplayer_lite", 1);
  }
 }
 else
     cs_set_int_value("framaplayer_lite", 1);

}


static 
void 
   set_framaplayer_information(const char *course, 
                   const char *key,
		   const char *title, const char *sender, 
		   const char *sender_realname, 
		   const char *fname, const char *infname, int grp, const char *link)
{
 char url[MAX_PATH + 1];
 char server_name[MAX_PATH + 1];
 
  get_server_string(server_name, MAX_PATH + 1);         /* shared_server_string.c */
  
  cs_set_value("server_name", server_name);
  if(strlen(link))
      snprintf(url, MAX_PATH + 1, "%s/%s/podcast_frama_playlist?link=%s.xml", server_name, ALIAS, link);
  else if(!strlen(sender)) 
      snprintf(url, MAX_PATH + 1, "%s/%s/podcast_frama_playlist?crs=%sid%c%s%cgrp%c%d.xml",
                         server_name, 
			 ALIAS,
                         course,
			 EQUAL_SUB,
                         key,
			 AMP_SUB,
			 EQUAL_SUB,
                         grp
                         ); 
   else
      snprintf(url, MAX_PATH + 1, 
		  "%s/%s/podcast_frama_playlist?crs=%ssender%c%s%cgrp%c%d%cfname%c%s%cinf%c%s%crlname%c%s%ctitle%c%s%cid%c%s.xml",
                         server_name, 
			 ALIAS,
                         course,
			 EQUAL_SUB,
			 sender,
			 AMP_SUB,
			 EQUAL_SUB,
			 grp,
			 AMP_SUB,
			 EQUAL_SUB,
			 fname,
			 AMP_SUB,
			 EQUAL_SUB,
                         infname,
			 AMP_SUB,
			 EQUAL_SUB,
                         sender_realname,
			 AMP_SUB,
			 EQUAL_SUB,
			 title,
			 AMP_SUB,
			 EQUAL_SUB,
                         key
			 );
	 
  
  cs_set_value("frama_xml_data_url", url);
  cs_set_value("frama_path", IMAGE_PATH); 
}

  
  
  

 

int
main (void)
{
  char course[MAX_PATH + 1];	/* from crs=? command line */
  char key[MAX_KEY + 1];	/* from id=? command line */
  SESSION user;
  CONFIG_STRUCT conf;
  int grp = 0;
  char fname[MAX_FILENAME + 1];	
  char infname[MAX_FILENAME + 1];	
  char sender[MAX_USERNAME + 1];	
  char sender_realname[MAX_NAME + 1];	
  char title[MAX_PATH + 1];	
  char link[MAX_PATH + 1];	
  
   *link = '\0'; 
   cs_cgi_init();
   read_parameters (course, key, title, sender, sender_realname,fname, infname, &grp, link);
   if(!strlen(link))
   {
      read_configuration_file (course, &conf);	/* shared_util.c */
     validate_key (key, &user, &conf);	/* shared_util.c */
   } 
   set_framaplayer_information(course, key, title, sender, sender_realname, fname, infname, grp, link);
  
  cs_cgi_display("podcast_launch_framaplayer", 1);
  cs_cgi_destroy();
  return (0);
}

