/*
 * 	libglacier.c - Backend C library for Glacier
 *
 * 	This file is part of Glacier.
 *
 * 	Glacier is free software: you can redistribute it and/or modify it under the terms of the
 * 	GNU General Public License as published by the Free Software Foundation, either
 * 	version 3 of the License, or (at your option) any later version.
 *
 * 	Glacier is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * 	without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * 	PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * 	You should have received a copy of the GNU General Public License along with Glacier. If
 * 	not, see <https://www.gnu.org/licenses/>.
 */

#include <color.h>
#include <errno.h>
#include <lauxlib.h>
#include <libconfig.h>
#include <locale.h>
#include <lua.h>
#include <lualib.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <wchar.h>

/* Global variables */

config_t cfg;
config_setting_t *setting;
const char str;
int GLACIER_ALLOW_SERVICES;
char GLACIER_ALLOWED_LICENSES;
int GLACIER_DO_INT_CHECK;

const char *runtime_files[] = { "/etc/glacier.cfg", "/etc/glacier/call-hooks", "/etc/make.conf" };

/*
 * infolog
 *
 * DESCRIPTION: Output a stylized info message.
 * PARAMETERS: char MSG[]
 *
 */

void
infolog(char MSG[])
{
	if (MSG == NULL) {
		return;
	}

	printf(COL_BLUE "[i]" COL_RESET " %s\n", MSG);
	return;
}

/*
 * warnlog
 *
 * DESCRIPTION: Output a stylized warning message.
 * Parameters: char MSG[]
 *
 */

void
warnlog(char MSG[])
{
	if (MSG == NULL) {
		return;
	}

	printf(COL_YELLOW "[!]" COL_RESET " %s\n", MSG);
	return;
}

/*
 * errlog
 * 
 * DESCRIPTION: Output a stylized error message.
 * PARAMETERS: char MSG[]
 *
 */

void
errlog(char MSG[])
{
	if (MSG == NULL) {
		return;
	}

	fprintf(stderr, COL_RED "[x]" COL_RESET " %s\n", MSG);
	return;
}

/*
 * runtime_exists
 *
 * DESCRIPTION: Check if necesary runtime files exist.
 * PARAMETERS: None.
 *
 */

void
runtime_exists()
{
	int f;

	for (f = 0; f < 3; f++) {
		if (access(runtime_files[f], F_OK) == 0) {
			printf("%s exists\n", runtime_files[f]);
		} else {
			printf("%s does not exist\n", runtime_files[f]);
		}
	}
	return;
}

/*
 * init_config
 *
 * DESCRIPTION: Initialize libconfig.
 * PARAMETERS: None.
 *
 */

int
init_config()
{
	config_init(&cfg);

	if (! config_read_file(&cfg, "glacier.cfg")) {
		fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg),
			config_error_line(&cfg), config_error_text(&cfg));
		config_destroy(&cfg);
		return(EXIT_FAILURE);
	}

	infolog("Initialized libconfig");
}

int
die_config()
{
	config_destroy(&cfg);
	infolog("Destroyed libconfig");
	return(EXIT_SUCCESS);
}

void
load_setting_from_config()
{
	if(config_lookup_bool(&cfg, "GLACIER_ALLOW_SERVICES", &GLACIER_ALLOW_SERVICES))
		printf("Services allowed: %s\n", GLACIER_ALLOW_SERVICES ? "true" : "false");
	else
		warnlog("GLACIER_SERVICES_ALLOWED is not defined in glacier.cfg.");

	if(config_lookup_bool(&cfg, "GLACIER_DO_INT_CHECK", &GLACIER_DO_INT_CHECK))
		printf("Integrity checking enabled: %s\n", GLACIER_DO_INT_CHECK ? "true" : "false");
	else
		warnlog("GLACIER_DO_INT_CHECK is not defined in glacier.cfg.");

}

int
download_package_sources(char PACKAGE[])
{
	printf(COL_BLUE "[i] " COL_RESET "Downloading package archive for %s...\n", PACKAGE);
	if (GLACIER_SYSTEM_PROFILE == "x86-musl") {
		execl("/glacier/tools/wget", "https://git.everestlinux.org/EverestLinux/epkgs-x86-musl/", NULL);
	} else {
		printf(COL_RED "[x] " COL_RESET "System profile '%s' is not supported.\n", GLACIER_SYSTEM_PROFILE);
	}
}

int
run_build_task()
{
	execl("pkgops", "build", NULL);
}

int
run_install_task()
{
	execl("pkgops", "install", NULL);
}

int
run_update_task()
{
	execl("pkgops", "update", NULL);
}

int
run_remove_task()
{
	execl("pkgops", "remove", NULL);
}

int
run_s_install_task()
{
	execl("pkgops", "s_install", NULL);
}

int
run_s_update_task()
{
	execl("pkgops", "s_update", NULL);
}

int
run_s_remove_task()
{
	execl("pkgops", "s_remove", NULL);
}

/*
void
init_lua()
{
	lua_State *L = luaL_newstate();
	luaL_openlibs(L);
}

int
load_lua_file(char luafile[])
{
	if (luaL_loadfile(L, luafile)) {
		fprintf(stderr, "LIBRARY ERROR: Could not open package %s. Does it exist?\n");
		lua_close(L);
		exit(1);
	}
	return 0;
}

int
exec_lua_file(char luafile[])
{
	if (lua_pcall(L, 0, 0, 0)) {
		fprintf(stderr, "LIBRARY ERROR: Failed to execute package %s.\n");
		lua_close(L);
		exit(1);
	}
	return 0;
}

void
die_lua()
{
	lua_close(L);
}
*/

int
parse_pkg_file(char fname[])
{
	if (fname == NULL) {
		fprintf(stderr, "LIBRARY ERROR: fname not specified\n");
		exit(1);
	}

	if (access(fname, F_OK) != 0) {
		fprintf(stderr, COL_RED "[x] " COL_RESET "Package file '%s' does not exist.\n", fname);
		exit(1);
	}


	FILE *pkg;

	char filename[100], contents;

	pkg = fopen(fname, "r");
	infolog("Parsing package file...");
	contents = fgetc(pkg);
	while (contents != EOF) {
		printf("%c", contents);
		contents = fgetc(pkg);
	}

	fclose(pkg);

	return 0;
}


int
main(int argc, char *argv[])
{
	errlog("Unknown option.");
	printf(COL_RED "[x]" COL_RESET " usage: %s [-h] [-v] [-f] [-u] [-x] [-fl] [-ul] [-d] [-s] PKG\n", argv[0]);
	runtime_exists();
	init_config();
	load_setting_from_config();
	die_config();
	parse_pkg_file("busybox");
	/* init_lua();
	load_lua_file("lua/testpkg.lua");
	exec_lua_file("lua/testpkg.lua");
	die_lua(); */
}
