#include "scheduled.h"
#include "bailout.h"
#include "error.h"
#include "open.h"
#include "ndelay.h"
#include "fmt.h"
#include "env.h"
#include <unistd.h>
#include <pwd.h>

void
notice(void)
{
	int fd=open_write("fifo");
	if (-1==fd) {
		static stralloc fifo_name;
		char * x;

		x = env_get("XDG_RUNTIME_DIR");
		if (x && *x) {
			if (!stralloc_copys(&fifo_name,x)) oom();
		} else {
			struct passwd *pw; 
			char nb[FMT_ULONG];

			pw = getpwuid(geteuid());
			if (!pw) xbailout(100,0,"failed to get current user data",0,0,0);
			if (!stralloc_copys(&fifo_name,"/var/run/user/:")) oom();
			nb[fmt_ulong(nb,pw->pw_uid)]=0;
			if (!stralloc_cats(&fifo_name,nb)) oom();
		}
		if (!stralloc_cats(&fifo_name,"/uscheduled/trigger")) oom();
		if (!stralloc_0(&fifo_name)) oom();
		fd=open_write(fifo_name.s);
	}
	if (-1==fd) {
		warning(errno,"fatal: failed to open_write fifo",0,0,0);
		xbailout(111,0,"is the uscheduled daemon running?",0,0,0);
	}
	ndelay_on(fd);
	if (-1==write(fd,"",1)) 
		warning(errno,"failed to write into daemon fifo",0,0,0);
	close(fd);
}
