Scallywag-NG/scallywag-ng.c

131 lines
3.7 KiB
C

/*
* Scallywag-NG: Half the scally, and twice the wag.
* Copyright (C) 2025 SILO GROUP LLC
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "src/app.h"
#include "src/http.h"
#include "src/util/log.h"
#include <gtk/gtk.h>
#include <libxml/parser.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <errno.h>
#define CONFIG_DIR_NAME "scallywag"
#define CONFIG_FILE_NAME "config.ini"
static const char *DEFAULT_CONFIG =
"[list_source]\n"
"piratebay_proxy_list = https://piratebayproxy.info/\n";
static char *get_config_path(void) {
const char *home = getenv("HOME");
if (!home) {
LOG_ERROR("HOME environment variable not set");
return NULL;
}
// Build paths - use PATH_MAX or reasonable limit
char config_dir[512];
char config_path[512];
char parent_dir[512];
int n = snprintf(config_dir, sizeof(config_dir), "%s/.config/%s", home, CONFIG_DIR_NAME);
if (n < 0 || (size_t)n >= sizeof(config_dir)) {
LOG_ERROR("Config path too long");
return NULL;
}
n = snprintf(config_path, sizeof(config_path), "%s/%s", config_dir, CONFIG_FILE_NAME);
if (n < 0 || (size_t)n >= sizeof(config_path)) {
LOG_ERROR("Config path too long");
return NULL;
}
// Check if config exists
if (access(config_path, F_OK) == 0) {
return strdup(config_path);
}
// Create config directory if needed
snprintf(parent_dir, sizeof(parent_dir), "%s/.config", home);
mkdir(parent_dir, 0755); // May already exist
if (mkdir(config_dir, 0755) != 0 && errno != EEXIST) {
LOG_ERROR("Failed to create config directory: %s", config_dir);
return NULL;
}
// Write default config
FILE *f = fopen(config_path, "w");
if (!f) {
LOG_ERROR("Failed to create config file: %s", config_path);
return NULL;
}
fprintf(f, "%s", DEFAULT_CONFIG);
fclose(f);
LOG_INFO("Created default config at: %s", config_path);
return strdup(config_path);
}
int main(int argc, char *argv[]) {
// Print GPL license notice
printf("Scallywag Copyright (C) 2025 SILO GROUP LLC\n");
printf("This program comes with ABSOLUTELY NO WARRANTY.\n");
printf("This is free software, and you are welcome to redistribute it\n");
printf("under certain conditions.\n\n");
// Initialize GTK
gtk_init(&argc, &argv);
// Initialize libxml2
xmlInitParser();
// Initialize HTTP (curl)
if (http_global_init() != 0) {
LOG_ERROR("Failed to initialize HTTP");
return 1;
}
// Get config file path
char *config_path = get_config_path();
LOG_INFO("Using config: %s", config_path);
// Create application
App *app = app_new(config_path);
free(config_path);
if (!app) {
LOG_ERROR("Failed to create application");
http_global_cleanup();
xmlCleanupParser();
return 1;
}
// Run application
app_run(app);
// Cleanup
app_free(app);
http_global_cleanup();
xmlCleanupParser();
LOG_INFO("Scallywag exited cleanly");
return 0;
}