/**
 * This file is part of the CernVM File System.
 */

#ifndef CVMFS_RECEIVER_COMMIT_PROCESSOR_H_
#define CVMFS_RECEIVER_COMMIT_PROCESSOR_H_

#include <ctime>
#include <string>

#include "repository_tag.h"
#include "server_tool.h"
#include "util/pointer.h"

namespace receiver {

/**
 * Resolve the gateway-local CVMFS_AUTO_TAG_TIMESPAN configuration value into a
 * Unix timestamp threshold, relative to `now`, in-process and without spawning
 * `date` or any other subprocess. Only the documented relative form
 * "<N> <unit> ago" is supported (e.g. "30 days ago", "1 month ago"); the unit
 * may be sec(ond), min(ute), hour, day, week, month or year, singular or
 * plural. Month and year arithmetic is calendar-aware (via mktime), matching
 * the GNU `date` semantics the publisher uses. Anything else (absolute dates,
 * "N days" without "ago", garbage, ...) returns 0 so the caller skips cleanup.
 *
 * Exposed in the header so it can be unit tested with a fixed `now`.
 */
time_t ParseRelativeTimespan(const std::string &timespan, time_t now);

/**
 * This class is used in the `cvmfs_receiver` tool, on repository gateway
 * machines. The receiver::Reactor class, implementing the event loop of the
 * `cvmfs_receiver` tool, dispatches the handling of the kCommit events
 * to this class.
 *
 * Its responsibility is updating the repository (sub-)catalogs with the changes
 * introduced during the lease. After all the catalogs have been updated, the
 * repository manifest is also updated and resigned.
 */
class CommitProcessor {
 public:
  enum Result {
    kSuccess,
    kError,
    kMergeFailure,
    kMissingReflog
  };

  CommitProcessor();
  virtual ~CommitProcessor();

  Result Process(const std::string &lease_path, const shash::Any &old_root_hash,
                 const shash::Any &new_root_hash, const RepositoryTag &tag,
                 uint64_t *final_revision);

  int GetNumErrors() const { return num_errors_; }

  void SetStatistics(perf::Statistics *st, const std::string &start_time);

 private:
  int num_errors_;
  perf::Statistics *statistics_;
  std::string start_time_;
};

}  // namespace receiver

#endif  // CVMFS_RECEIVER_COMMIT_PROCESSOR_H_
