#include <stdio.h>
#include <sys/stat.h>		/** for stat() */
#include <dirent.h>
#include <string.h>


#include "global.h"
#include "ClearSilver.h"


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


static void
read_parameters (char *symlink, int *send_smil_file)
{

  char tmp[10];


  cs_get_required_parameter ("link", symlink, MAX_FILENAME);  /* shared_cs_util.c */
  cs_get_optional_parameter ("smil", tmp, 9);
  *send_smil_file = *tmp? 1: 0;
  

}


static void
set_index_values(const char *symlink)
{
 char server_string[MAX_PATH + 1];
 char url[MAX_PATH + 1];
 
 
 
 get_server_string(server_string, MAX_PATH + 1);   /* shared_server_string.c */

 snprintf(url,MAX_PATH + 1, "%s?link=%s&amp;smil=1", "lectern_handler", symlink);
 cs_set_value("view_presentation_url", url);


 snprintf(url,MAX_PATH + 1, "%s%slinks/%s/voice.mp3",
                                   server_string, IMAGE_PATH, symlink);
 cs_set_value("download_mp3_url", url);
 
  /* 
  ** provide url for mp3 player, so users
  ** can listen to the voice.mp3 file
  */
  cs_set_server_name();
  snprintf(url, MAX_PATH +1, "podcast_launch_framaplayer?link=%s", symlink);
  cs_set_value("wimpy_button_url", url);  
 


 snprintf(url,MAX_PATH + 1, "%s%slinks/%s/printslides.html",
                                   server_string, IMAGE_PATH, symlink);
 cs_set_value("view_slides_url", url);
}





static
void deliver_smil_file(const char *symlink)
{

 char server_string[MAX_PATH + 1];
 
  get_server_string(server_string, MAX_PATH + 1);   /* shared_server_string.c */

   printf("Content-type: application/smil\n\n");
   
   puts("<smil>\n"
        "<body>\n"
        "<par>");
        
   printf("<ref src=\"%s%slinks/%s/present.rp\"/>\n",
               server_string,IMAGE_PATH,symlink);

   
   printf("<audio src=\"%s%slinks/%s/voice.mp3\" />",
               server_string,IMAGE_PATH,symlink);

   puts("</par>\n"
        "</body>\n"
        "</smil>");
        


}

   



int
main ()
{

  char symlink[MAX_FILENAME + 1];
  int send_smil_file;
  

  cs_cgi_init();    /* shared_cs_util.c */
  read_parameters (symlink, &send_smil_file);
  
  if(send_smil_file)
    deliver_smil_file(symlink);
  else
   {
    set_index_values(symlink);
    cs_cgi_display ("lectern_handler", 1);    /* shared_cs_util.c */
   }
   
   
  cs_cgi_destroy();        /* shared_cs_util.c */


  return 0;			/* exit successfully */

}

