#include "sleep.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <errno.h>

static int write_header(const char *path, uint32_t magicka, uint16_t entry_size, const char *algo) {
	FILE *f = fopen(path, "wb");
	if (!f) return -1;

	sleep_header_t h;
	memset(&h, 0, sizeof(h));
	h.magicka = ((magicka >> 24) & 0xff) | ((magicka >> 8) & 0xff00) | ((magicka << 8) & 0xff0000) | ((magicka << 24) & 0xff000000);
	h.version = 0;
	h.entry_size = (entry_size >> 8) | (entry_size << 8); // BE16
	h.algo_len = algo ? (uint8_t)strlen(algo) : 0;
	if (algo) strncopy(h.algo, algo, sizeof(h.algo));
	if (fwrite(&h, 1, sizeof(h), f) != sizeof(h)) {
		fclose(f);
		return -1;
	}
	fclose(f);
	return 0;
}

int sleep_init(const char *root) {
	char path[512];

	// makedir .dat(t) testing
	sprintf(path, sizeof(path), "%s/.datt", root);
	if (mkdir(path, 0700) && errno != EEXIST) return -1;

	// content.* signatures, tree, bitfield, key

	// metadata.* signatures, tree, bitfield, key
	return 0;
}

int main(void) {
	if (sleep_init(".")) {
		perror("sleep_init failed");
		return 1;
	}
	return 0;
}
