#include <stdio.h>
#include <stdlib.h>
/* #include <sys/wait.h> */
#include <winsock.h>
#include <process.h>
#include <string.h>
#include <sys/types.h>
/* #include <unistd.h> */

#include <sys/types.h>
#include <winsock.h>
/* #include <sys/socket.h> */
/* #include <netinet/in.h>
#include <arpa/inet.h>
 */


#include "sessions.h"
#include "parser_s.h"

void
Panic(char *Filename, int Line)
{
   fprintf(stderr, "\n!! Panic in line %d of file %s\n", Line, Filename);
   perror("Unexpected library error");
   fprintf(stderr, "\n");
   abort();
}

#define PANIC (Panic(__FILE__, __LINE__))


static void
usage(void)
{
   fprintf(stderr, "Usage: wrtpLaunch -f file\n");
}

void
get_video_dest(struct session *sess, char *dest) {
   struct media *tmp;
   int found = 0;

   /* Insert media ports */
   tmp = sess->media;
 
   while((tmp != NULL) && !found)
   {
      if(strcmp(tmp->media, "video") == 0)
      {
         char port[10];
         strcat(dest, inet_ntoa(tmp->addr));
         strcat(dest, "/");
         sprintf(port, "%d", tmp->port);
         strcat(dest, port);
         strcat(dest, "/1");
         found = 1;
      }
      tmp = tmp->next;
   }
}

void
get_audio_dest(struct session *sess, char *dest) {
   struct media *tmp;
   int found = 0;

   /* Insert media ports */
   tmp = sess->media;
 
   while((tmp != NULL) && !found)
   {
      if(strcmp(tmp->media, "audio") == 0)
      {
         char port[10];
         strcat(dest, inet_ntoa(tmp->addr));
         strcat(dest, "/");
         sprintf(port, "%d", tmp->port);
         strcat(dest, port);
         strcat(dest, "/1");
         found = 1;
      }
      tmp = tmp->next;
   }
}


void
CreateChildVic(struct session *sess)
{
    char dest[1024];
//    char cmd[2048];

    memset(dest, 0, sizeof(dest));
//    memset(cmd, 0, sizeof(dest));
    get_video_dest(sess, dest);

#ifdef DEBUG
   printf("Creating Vic: vic -A rtp -C %s %s\n", sess->sname, dest);
#endif /* DEBUG */
     _spawnlp(_P_NOWAIT, "vic", "vic", "-A", "rtp" , "-C", sess->sname, "-s", "-P", dest, NULL);
//     _spawnlp(_P_NOWAIT, "vic", "vic", "-A", "rtp" , "-C", sess->sname, "-s", "-P",  "-f", "h261", "-X", "geometry=250x250+302+0", dest, NULL);
//   sprintf(cmd, "vic -A rtp -C %s -s -P -f h261 -X geometry=250x250+302+0 %s\n", sess->sname, dest);
//	 system(cmd);
}

void
CreateChildVat(struct session *sess)
{
    char dest[1024];
//    char cmd[2048];

    memset(dest, 0, sizeof(dest));
//    memset(cmd, 0, sizeof(dest));
    get_audio_dest(sess, dest);

#ifdef DEBUG
    printf("Creating Vat: vat -r -C %s %s\n", sess->sname, dest);
#endif /* DEBUG */

     _spawnlp(_P_NOWAIT,"vat", "vat", "-r", "-C", sess->sname, dest, NULL);
//     _spawnlp(_P_NOWAIT,"vat", "vat", "-r", "-C", sess->sname, "-X", "geometry=+0+0", dest, NULL);
//   sprintf(cmd, "vat -r -C %s -X geometry=+0+0 %s\n", sess->sname, dest);
//	 system(cmd);
}

void
Create_Vic_Vat(struct session *sess)
{
   CreateChildVic(sess);
   CreateChildVat(sess);
}

void
main(int argc, char* argv[])
{
    struct session *sess = NULL;

    char filename[1024];

    FILE *file;

	if(argc != 3)
    {
       usage();
       exit(1);
    }

	if(strcmp(argv[1], "-f"))
    {
       usage();
       exit(1);
    }

    strcpy(filename, argv[2]);

    if(filename == NULL)
    {
       usage();
       exit(1);
    }

   if ((file = fopen(filename, "r" )) == NULL )
   {
      fprintf(stderr, "\nFile %s specified could not be found\n", filename);
      exit(1);
   }
   
   if((sess = parse_SDP_file(file)) == NULL)
   {
       fclose(file);
       fprintf(stderr, "\nFile %s specified is not correctly parsed\n", filename);
       exit(1);
   }


  Create_Vic_Vat(sess);

}
