/* gcc vte.c $(pkg-config --cflags --libs vte gtk+-2.0 glib-2.0) */
/* gcc vte.c $(pkg-config --cflags --libs vte-2.91 gtk+-3.0 glib-2.0) */

#include <string.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#include <gtk/gtk.h>
#include <vte/vte.h>

static int debug_fd = 0;

static void
log_handler (const gchar *log_domain,
             GLogLevelFlags log_level,
             const gchar *message,
             gpointer user_data)
{
  gchar *log_message = NULL;
  gsize count;

  log_message = g_strdup_printf ("%s: %s\n", log_domain, message);
  count = strlen (log_message);
  write (debug_fd, log_message, count);
  g_free (log_message);
}

static void
window_title_changed (VteTerminal *terminal, gpointer user_data)
{
  GtkWidget *window = GTK_WIDGET (user_data);
  const gchar *title;

  title = vte_terminal_get_window_title (terminal);
  gtk_window_set_title (GTK_WINDOW (window), title);
}

gint
main (gint argc, gchar *argv[])
{
  GPid pid;
  GtkWidget *terminal;
  GtkWidget *window;
  const gchar *command;
  gchar *cwd = NULL;
  gchar *filename = NULL;
  gchar *shell = NULL;
  gchar **command_argv = NULL;

  cwd = g_get_current_dir ();
  pid = getpid ();
  filename = g_strdup_printf ("%s/vtelog.%d", cwd, pid);
  debug_fd = open (filename, O_WRONLY | O_CREAT, 00644);

  g_log_set_handler ("GLib", G_LOG_LEVEL_MASK, log_handler, NULL);
  g_log_set_handler ("Vte", G_LOG_LEVEL_MASK, log_handler, NULL);

  gtk_init (NULL, NULL);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_widget_set_size_request (window, 600, 400);
  g_signal_connect (window, "delete-event", gtk_main_quit, NULL);

  terminal = vte_terminal_new ();
  gtk_container_add (GTK_CONTAINER (window), terminal);
  g_signal_connect (terminal,
                    "child-exited",
                    G_CALLBACK (gtk_main_quit),
                    window);
  g_signal_connect (terminal,
                    "window-title-changed",
                    G_CALLBACK (window_title_changed),
                    window);

  if (argc > 1)
    {
      command = argv[1];
    }
  else
    {
#if VTE_CHECK_VERSION (0, 28, 0)
      shell = vte_get_user_shell ();
#else
      shell = g_strdup ("/bin/sh");
#endif
      command = shell;
    }

  if (!g_shell_parse_argv (command, NULL, &command_argv, NULL))
    goto out;

#if VTE_CHECK_VERSION (0, 38, 0)
  vte_terminal_spawn_sync (VTE_TERMINAL (terminal),
                           VTE_PTY_DEFAULT,
                           NULL,
                           command_argv,
                           NULL,
                           G_SPAWN_SEARCH_PATH,
                           NULL,
                           NULL,
                           NULL,
                           NULL,
                           NULL);
#else
  vte_terminal_fork_command_full (VTE_TERMINAL (terminal),
                                  VTE_PTY_NO_LASTLOG,
                                  NULL,
                                  command_argv,
                                  NULL,
                                  G_SPAWN_SEARCH_PATH,
                                  NULL,
                                  NULL,
                                  NULL,
                                  NULL);
#endif

  gtk_widget_grab_focus (terminal);
  gtk_widget_show_all (window);

  gtk_main ();

 out:
  g_free (cwd);
  g_free (filename);
  g_free (shell);
  g_strfreev (command_argv);
  return 0;
}
