/*****************************************************************
 * gmerlin - a general purpose multimedia framework and applications
 *
 * Copyright (c) 2001 - 2012 Members of the Gmerlin project
 * gmerlin-general@lists.sourceforge.net
 * http://gmerlin.sourceforge.net
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * *****************************************************************/


#include <config.h>
#include <gmerlin/cfg_registry.h>
#include <gmerlin/cfgctx.h>
#include <gmerlin/cmdline.h>
#include <gmerlin/utils.h>
#include <gmerlin/translation.h>

#include <gmerlin/bgsocket.h>
#include <gmerlin/websocket.h>

#include <gmerlin/log.h>
#define LOG_DOMAIN "gmerlin_transcoder_remote"

#include "transcoder_remote.h"

bg_controllable_t * ctrl = NULL;

static void cmd_addalbum(void * data, int * argc, char *** _argv, int arg)
  {
  gavl_buffer_t buf;
  FILE * out = stderr;
  gavl_msg_t * msg;
  char ** argv = *_argv;

  
  if(arg >= *argc)
    {
    fprintf(out, "Option -addalbum requires an argument\n");
    exit(-1);
    }
  msg = bg_msg_sink_get(ctrl->cmd_sink);

  /* Load the entire xml file into a string */

  gavl_buffer_init(&buf);

  if(!bg_read_file(argv[arg], &buf))
    return;
  
  gavl_msg_set_id(msg, TRANSCODER_REMOTE_ADD_ALBUM);
  gavl_msg_set_arg_string(msg, 0, (char*)buf.buf);
  bg_cmdline_remove_arg(argc, _argv, arg);
  
  gavl_buffer_free(&buf);
  
  bg_msg_sink_put(ctrl->cmd_sink, msg);
  }

static void cmd_add(void * data, int * argc, char *** _argv, int arg)
  {
  FILE * out = stderr;
  gavl_msg_t * msg;
  
  if(arg >= *argc)
    {
    fprintf(out, "Option -add requires an argument\n");
    exit(-1);
    }
  msg = bg_msg_sink_get(ctrl->cmd_sink);
  
  gavl_msg_set_id(msg, TRANSCODER_REMOTE_ADD_FILE);

  gavl_msg_set_arg_string(msg, 0, (*_argv)[arg]);
  bg_cmdline_remove_arg(argc, _argv, arg);
  
  bg_msg_sink_put(ctrl->cmd_sink, msg);
  }

bg_cmdline_arg_t commands[] =
  {
    {
      .arg =         "-addalbum",
      .help_arg =    "<album_file>",
      .help_string = "Add album to track list",
      .callback =    cmd_addalbum,
    },
    {
      .arg =         "-add",
      .help_arg =    "<location>",
      .help_string = "Add <location> to the track list",
      .callback =    cmd_add,
    },
    { /* End of options */ }
  };

char * host = NULL;
int port = TRANSCODER_REMOTE_PORT;
int launch = 0;


static void opt_host(void * data, int * argc, char *** argv, int arg)
  {
  FILE * out = stderr;
  if(arg >= *argc)
    {
    fprintf(out, "Option -host requires an argument\n");
    exit(-1);
    }
  host = gavl_strrep(host, (*argv)[arg]);
  bg_cmdline_remove_arg(argc, argv, arg);
  }

static void opt_port(void * data, int * argc, char *** argv, int arg)
  {
  FILE * out = stderr;
  if(arg >= *argc)
    {
    fprintf(out, "Option -port requires an argument\n");
    exit(-1);
    }
  port = atoi((*argv)[arg]);
  bg_cmdline_remove_arg(argc, argv, arg);
  }

static void opt_launch(void * data, int * argc, char *** argv, int arg)
  {
  launch = 1;
  }


static bg_cmdline_arg_t global_options[] =
  {
    {
      .arg =         "-host",
      .help_arg =    "<hostname>",
      .help_string = "Host to connect to, default is localhost",
      .callback =    opt_host,
    },
    {
      .arg =         "-port",
      .help_arg =    "<port>",
      .help_string = "Port to connect to",
      .callback =    opt_port,
    },
    {
      .arg =         "-launch",
      .help_string = "Launch new transcoder if necessary",
      .callback =    opt_launch,
    },
    { /* End of options */ }
  };

const bg_cmdline_app_data_t app_data =
  {
    .package =  PACKAGE,
    .version =  VERSION,
    .name =     "gmerlin_transcoder_remote",
    .long_name = TRS("Remote control command for the Gmerlin GUI transcoder\n"),
    .synopsis = TRS("[options] command\n"),
    .help_before = TRS("Remote control command for the Gmerlin GUI transcoder\n"),
    .args = (bg_cmdline_arg_array_t[]) { { TRS("Global options"), global_options },
                                       { TRS("Commands"),       commands       },
                                       {  } },
    .env = (bg_cmdline_ext_doc_t[])
    { { TRANSCODER_REMOTE_ENV,
        TRS("Default port for the remote control") },
      { /* End */ }
    },
  };

int main(int argc, char ** argv)
  {
  int i;
  gavl_time_t delay_time = GAVL_TIME_SCALE / 50;
  bg_websocket_connection_t * remote;
  char * env;
  char * url;
  
  bg_cmdline_init(&app_data);
  
  if(argc < 2)
    bg_cmdline_print_help(argv[0], 0);
  
  port = TRANSCODER_REMOTE_PORT;
  env = getenv(TRANSCODER_REMOTE_ENV);
  if(env)
    port = atoi(env);
  
  bg_cmdline_parse(global_options, &argc, &argv, NULL);

  if(!host)
    host = gavl_strrep(host, "localhost");
  
  url = bg_sprintf("ws://%s:%d%s", host, port, TRANSCODER_REMOTE_PATH);
  
  if(!(remote = bg_websocket_connection_create(url, 1000,
                                                      NULL)))
    {
    if(launch)
      {
      if(system("gmerlin_transcoder &"))
        {
        gavl_log(GAVL_LOG_ERROR, LOG_DOMAIN, "Cannot launch transcoder process");
        return -1;
        }

      for(i = 0; i < 500; i++)
        {
        if(!(remote = bg_websocket_connection_create(url, 1000, NULL)))
          break;
        gavl_time_delay(&delay_time);
        }
      }
    else
      return -1;
    }

  ctrl = bg_websocket_connection_get_controllable(remote);
  
  bg_cmdline_parse(commands, &argc, &argv, remote);

  bg_websocket_connection_destroy(remote);
  free(url);
  return 0;
  }
