/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <iostream>
#include <optional>
#include <string>
#include <vector>

#include "absl/cleanup/cleanup.h"
#include <fmt/core.h>
#include <google/protobuf/text_format.h>
#include "absl/log/log.h"

#include "cuttlefish/common/libs/fs/shared_fd.h"
#include "cuttlefish/common/libs/utils/files.h"
#include "cuttlefish/common/libs/utils/tee_logging.h"
#include "cuttlefish/host/commands/snapshot_util_cvd/parse.h"
#include "cuttlefish/host/commands/snapshot_util_cvd/snapshot_taker.h"
#include "cuttlefish/host/libs/command_util/runner/run_cvd.pb.h"
#include "cuttlefish/host/libs/command_util/util.h"
#include "cuttlefish/host/libs/config/cuttlefish_config.h"
#include "cuttlefish/result/result.h"

namespace cuttlefish {
namespace {

// Send a `LauncherAction` RPC to every instance specified in `parsed`.
Result<void> BroadcastLauncherAction(
    const CuttlefishConfig& config, const Parsed& parsed,
    run_cvd::ExtendedLauncherAction extended_action) {
  for (const auto instance_num : parsed.instance_nums) {
    LOG(INFO) << "Instance #" << instance_num
              << ": Sending request: " << extended_action.ShortDebugString();
    auto socket = CF_EXPECT(
        GetLauncherMonitor(config, instance_num, parsed.wait_for_launcher));
    CF_EXPECT(RunLauncherAction(socket, extended_action, std::nullopt));
  }
  return {};
}

Result<void> SnapshotCvdMain(std::vector<std::string> args) {
  CF_EXPECT(!args.empty(), "No arguments was given");
  const auto prog_path = args.front();
  args.erase(args.begin());
  auto parsed = CF_EXPECT(Parse(args));

  const CuttlefishConfig* config =
      CF_EXPECT(CuttlefishConfig::Get(), "Failed to obtain config object");

  switch (parsed.cmd) {
    case SnapshotCmd::kSuspend: {
      run_cvd::ExtendedLauncherAction extended_action;
      extended_action.mutable_suspend();
      CF_EXPECT(BroadcastLauncherAction(*config, parsed, extended_action));
      return {};
    }
    case SnapshotCmd::kResume: {
      run_cvd::ExtendedLauncherAction extended_action;
      extended_action.mutable_resume();
      CF_EXPECT(BroadcastLauncherAction(*config, parsed, extended_action));
      return {};
    }
    case SnapshotCmd::kSnapshotTake: {
      CF_EXPECT(!parsed.snapshot_path.empty(), "--snapshot_path is required");
      parsed.snapshot_path = AbsolutePath(parsed.snapshot_path);
      if (parsed.force &&
          FileExists(parsed.snapshot_path, /* follow symlink */ false)) {
        CF_EXPECT(RecursivelyRemoveDirectory(parsed.snapshot_path),
                  "Failed to delete preexisting snapshot dir");
      }
      CF_EXPECTF(!FileExists(parsed.snapshot_path, /* follow symlink */ false),
                 "Delete the destination directory \"{}\" first",
                 parsed.snapshot_path);

      // Automatically suspend and resume if requested.
      if (parsed.auto_suspend) {
        run_cvd::ExtendedLauncherAction extended_action;
        extended_action.mutable_suspend();
        CF_EXPECT(BroadcastLauncherAction(*config, parsed, extended_action));
      }
      absl::Cleanup maybe_resume_on_exit = [&parsed, &config]() {
            if (!parsed.auto_suspend) {
              return;
            }
            run_cvd::ExtendedLauncherAction extended_action;
            extended_action.mutable_resume();
            Result<void> result =
                BroadcastLauncherAction(*config, parsed, extended_action);
            if (!result.ok()) {
              LOG(FATAL) << "RunLauncherAction failed: " << result.error();
            }
          };

      // Delete incomplete snapshot if we fail partway.
      absl::Cleanup delete_snapshot_on_fail = [&parsed]() {
        if (!parsed.cleanup_snapshot_path) {
          return;
        }
        LOG(ERROR) << "Snapshot take failed, so running clean-up.";
        Result<void> result = RecursivelyRemoveDirectory(parsed.snapshot_path);
        if (!result.ok()) {
          LOG(ERROR) << "Failed to delete incomplete snapshot: "
                     << result.error();
        }
      };

      // Snapshot group-level host runtime files and generate snapshot metadata
      // file.
      const std::string meta_json_path =
          CF_EXPECT(HandleHostGroupSnapshot(parsed.snapshot_path),
                    "Failed to back up the group-level host runtime files.");
      // Snapshot each instance.
      run_cvd::ExtendedLauncherAction extended_action;
      extended_action.mutable_snapshot_take()->set_snapshot_path(
          meta_json_path);
      CF_EXPECT(BroadcastLauncherAction(*config, parsed, extended_action));
      std::move(delete_snapshot_on_fail).Cancel();
      return {};
    }
    default: {
      return CF_ERRF("unknown cmd: {}", (int)parsed.cmd);
    }
  }
}

}  // namespace
}  // namespace cuttlefish

int main(int argc, char** argv) {
  cuttlefish::LogToStderr();
  std::vector<std::string> all_args(argv, argv + argc);
  auto result = cuttlefish::SnapshotCvdMain(std::move(all_args));
  if (!result.ok()) {
    LOG(ERROR) << result.error();
    return EXIT_FAILURE;
  }
  return EXIT_SUCCESS;
}
