#ifndef TRANSTUI_DAEMON_H
#define TRANSTUI_DAEMON_H

#include <stddef.h>

/* True if host refers to this machine (127.0.0.1/localhost/::1) - the only
 * case where offering to spawn transmission-daemon locally makes sense. */
int daemon_host_is_local(const char *host);

/* Launches `transmission-daemon` (must be on PATH) and lets it daemonize
 * itself in the background as it normally does. Returns 0 if the command
 * was found and its (short-lived, since it double-forks) launcher process
 * exited cleanly, -1 on failure (err filled, e.g. "not found on PATH"). */
int daemon_start(char *err, size_t errlen);

/* Rewrites the system transmission-daemon's RPC credentials (Debian's
 * packaged /etc/transmission-daemon/settings.json) and restarts the service
 * via `sudo systemctl` - for when the daemon is already running but RPC
 * auth is enabled with a password nobody knows (the Debian package
 * generates a random one that's never shown in plaintext).
 *
 * If password is NULL or empty, disables RPC auth entirely instead of
 * setting a username/password. sudo will prompt on the controlling
 * terminal - the caller is responsible for leaving curses mode first
 * (def_prog_mode()+endwin(), then reset_prog_mode() after).
 *
 * Returns 0 on success, -1 on failure (err filled - e.g. sudo was denied,
 * the settings file wasn't found, or the service failed to restart). */
int daemon_fix_auth(const char *username, const char *password, char *err, size_t errlen);

/* For when the daemon's errorString on a torrent points at a permission
 * problem writing to `dir`: adds the system daemon's service account
 * (Debian's package runs it as `debian-transmission`) to `dir`'s owning
 * group, chmods the directory group-writable, and restarts the service via
 * `sudo systemctl` so the new group membership takes effect (group changes
 * don't apply to an already-running process). `dir` must be a directory the
 * *invoking* user already owns/can write - this only bridges the gap to the
 * separate daemon account, it can't grant access nobody has.
 *
 * sudo will prompt on the controlling terminal - same caller responsibility
 * as daemon_fix_auth() re: leaving/re-entering curses mode.
 *
 * Returns 0 on success, -1 on failure (err filled). */
int daemon_fix_download_dir_perms(const char *dir, char *err, size_t errlen);

#endif
