/*
 * Copyright © 2017-2019 Collabora Ltd.
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, see <http://www.gnu.org/licenses/>.
 */

#include "wrap-interactive.h"

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

#include <glib/gstdio.h>

void
pv_bwrap_wrap_in_xterm (FlatpakBwrap *wrapped_command,
                        const char *xcursor_path)
{
  g_return_if_fail (wrapped_command != NULL);

  flatpak_bwrap_add_args (wrapped_command,
                          "/usr/bin/env", "xterm", "-e",
                          /* Original command will go here and become
                           * the argv of xterm -e */
                          NULL);

  if (xcursor_path != NULL)
    {
      /* xterm unsets XCURSOR_PATH. Reinstate it for the contained shell. */
      flatpak_bwrap_add_arg (wrapped_command, "/usr/bin/env");
      flatpak_bwrap_add_arg_printf (wrapped_command, "XCURSOR_PATH=%s", xcursor_path);
    }
}

void
pv_bwrap_wrap_interactive (FlatpakBwrap *wrapped_command,
                           PvShell shell)
{
  static const char preamble[] =
    {
      "prgname=\"$1\"\n"
      "shift\n"
    };
  const char *script = "";
  static const char start_shell[] =
    {
      "echo\n"
      "echo\n"
      "echo\n"
      "echo \"$prgname: Starting interactive shell (original command is in "
      "\\\"\\$@\\\")\"\n"
      "echo\n"
      "echo\n"
      "echo\n"
      "exec bash -i -s \"$@\"\n"
    };
  g_autofree char *command = NULL;

  g_return_if_fail (wrapped_command != NULL);

  switch (shell)
    {
      case PV_SHELL_NONE:
        script =
          "e=0\n"
          "\"$@\" || e=$?\n"
          "echo\n"
          "echo \"$prgname: command exit status $e\"\n"
          "echo \"Press Enter or ^D to continue...\"\n"
          "read reply || true\n"
          "exit \"$e\"\n";
        break;

      case PV_SHELL_AFTER:
        script =
          "e=0\n"
          "\"$@\" || e=$?\n"
          "echo \"$prgname: command exit status $e\"\n";
        break;

      case PV_SHELL_FAIL:
        script =
          "if \"$@\"; then exit 0; else e=\"$?\"; fi\n"
          "echo \"$prgname: command exit status $e\"\n";
        break;

      case PV_SHELL_INSTEAD:
        break;

      default:
        g_return_if_reached ();
    }

  command = g_strdup_printf ("%s%s%s",
                             preamble,
                             script,
                             shell == PV_SHELL_NONE ? "" : start_shell);

  flatpak_bwrap_add_args (wrapped_command,
                          "/bin/sh", "-euc",
                          command,
                          "sh",   /* $0 for sh */
                          g_get_prgname (),   /* $1 = $prgname for sh */
                          /* Original command will go here and become
                           * the rest of the argv of command, and eventually
                           * the argv of bash -i -s */
                          NULL);
}
