/*
 * The login screen shows the lock screen picture too: SDDM runs as its
 * own user and can't read ours, so a copy goes to /var/lib through
 * c8-login-picture (root, by pkexec; LOCK-AUTH.md). Run when the picture
 * changes (Photos' Set as, PC settings' Remove, settings sync) and once
 * a while after the shell starts, for a change made while it wasn't
 * running. What was last sent is remembered (its hash, in
 * $XDG_STATE_HOME/cont8nuum/login-picture), so pkexec runs only for a
 * real change. Not nested (the login screen is the host's), and not
 * without the polkit action: only a /usr install has it, and without it
 * pkexec would ask for the admin password.
 */
#include <gio/gio.h>
#include <glib.h>
#include "background.h"
#include "cont8nuum-shell-v1-client-protocol.h"
#include "shell.h"

#define SETTLE_MS 2000
#define FIRST_LOOK_S 15

static guint timer;
static bool running, again;

static char *stamp_path(void) {
	return g_build_filename(g_get_user_state_dir(), "cont8nuum",
		"login-picture", NULL);
}

static char *picture_hash(const char *path) {
	char *data;
	gsize len;
	if (!g_file_get_contents(path, &data, &len, NULL)) {
		return g_strdup("");
	}
	char *hash = g_compute_checksum_for_data(G_CHECKSUM_SHA256,
		(const guchar *)data, len);
	g_free(data);
	return hash;
}

static void look(void);

static void sent(GObject *src, GAsyncResult *res, gpointer data) {
	char *hash = data;
	GError *err = NULL;
	if (g_subprocess_wait_check_finish(G_SUBPROCESS(src), res, &err)) {
		char *path = stamp_path();
		char *dir = g_path_get_dirname(path);
		g_mkdir_with_parents(dir, 0700);
		g_file_set_contents(path, hash, -1, NULL);
		g_free(dir);
		g_free(path);
	} else {
		/* Refused or failed: tried again at the next change or start */
		g_message("The login screen's picture wasn't changed: %s",
			err->message);
		g_error_free(err);
	}
	g_free(hash);
	running = false;
	if (again) {
		again = false;
		look();
	}
}

static void look(void) {
	if (running) {
		again = true;
		return;
	}
	if (shell.power & C8_SHELL_V1_POWER_EXIT) {
		return; // nested
	}
	if (!g_file_test(LOGIN_PICTURE_POLICY, G_FILE_TEST_EXISTS) ||
			!g_file_test(LOGIN_PICTURE, G_FILE_TEST_IS_EXECUTABLE)) {
		return;
	}
	char *path = c8_background_path(C8_BACKGROUND_LOCK);
	char *hash = picture_hash(path);
	char *stamp_file = stamp_path();
	char *stamp = NULL;
	g_file_get_contents(stamp_file, &stamp, NULL, NULL);
	g_free(stamp_file);
	if (g_strcmp0(stamp, hash) == 0 || (stamp == NULL && hash[0] == '\0')) {
		g_free(stamp);
		g_free(hash);
		g_free(path);
		return;
	}
	g_free(stamp);
	GSubprocessLauncher *l = g_subprocess_launcher_new(
		G_SUBPROCESS_FLAGS_STDOUT_SILENCE);
	if (hash[0]) {
		g_subprocess_launcher_set_stdin_file_path(l, path);
	}
	GError *err = NULL;
	GSubprocess *p = g_subprocess_launcher_spawn(l, &err, "pkexec",
		LOGIN_PICTURE, hash[0] ? "set" : "remove", NULL);
	g_object_unref(l);
	g_free(path);
	if (p == NULL) {
		g_message("pkexec: %s", err->message);
		g_error_free(err);
		g_free(hash);
		return;
	}
	running = true;
	g_subprocess_wait_check_async(p, NULL, sent, hash);
	g_object_unref(p);
}

static gboolean settled(gpointer data) {
	timer = 0;
	look();
	return G_SOURCE_REMOVE;
}

void login_picture_changed(void) {
	if (timer) {
		g_source_remove(timer);
	}
	timer = g_timeout_add(SETTLE_MS, settled, NULL);
}

void login_picture_init(void) {
	if (timer == 0) {
		timer = g_timeout_add_seconds(FIRST_LOOK_S, settled, NULL);
	}
}
