// SPDX-License-Identifier: GPL-2.0-only
/*
 *  ppm/src/cmd/common.h
 *
 *  Shared helpers for CLI subcommands.
 *
 *  ppm is a *system* package manager, not a per-user tool. The store
 *  and registry mirror live at fixed paths chosen at configure time
 *  (see configure.ac --with-store-dir / --with-registry-dir), and the
 *  registry's git URL is hard-coded at build time
 *  (--with-registry-url). There is exactly one canonical registry per
 *  build. Users may not repoint it via environment variables.
 *
 *  Copyright (C) 2026 Project Tick
 */

#ifndef PPM_CMD_COMMON_H
#define PPM_CMD_COMMON_H

#include <ppm/error.h>
#include <ppm/store.h>
#include <ppm/registry.h>

/* The hard-coded, configure-time constants. Exposed for `ppm --version`
 * and for error messages so users can see where ppm is looking. */
const char *ppm_cmd_store_dir   (void);
const char *ppm_cmd_registry_dir(void);
const char *ppm_cmd_registry_url(void);

/*
 * Open the default store. Creates the on-disk layout the first time
 * around (requires write access to the configured store directory,
 * which usually means running ppm as root for mutating commands).
 */
ppm_error_t ppm_cmd_open_store(ppm_store_t **out);

/*
 * Open the registry mirror, cloning from the configure-time URL on the
 * first call. The clone target is the configure-time registry directory.
 */
ppm_error_t ppm_cmd_open_registry(ppm_registry_t **out);

/*
 * Bail out with a clear message if the calling user cannot write to
 * the store directory. Returns PPM_OK if writable, PPM_ERR_PERMISSION
 * with an error logged otherwise. Used at the top of mutating commands
 * (install/remove/update/gc) so non-root users get a helpful hint
 * instead of mysterious EACCES failures deeper in.
 */
ppm_error_t ppm_cmd_require_root_or_writable_store(void);

#endif
