/*
 * CodersSecret harmless control-flow utility.
 *
 * Course behavior only: read the fixed file "course-input.txt" from the
 * current course directory, validate two tiny records, and write the fixed
 * file "course-summary.txt". The program accepts no arguments, opens no
 * network connection, starts no child process, requests no privilege, and
 * does not delete or modify an existing file.
 */

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define INPUT_FILE "course-input.txt"
#define OUTPUT_FILE "course-summary.txt"
#define MAX_LINE_LENGTH 64

static int parse_course_value(const char *line, const char *name, int *value) {
    char expected_prefix[32];
    char *end = NULL;
    long parsed;

    if (snprintf(expected_prefix, sizeof expected_prefix, "%s=", name) < 0) {
        return 0;
    }
    if (strncmp(line, expected_prefix, strlen(expected_prefix)) != 0) {
        return 0;
    }

    errno = 0;
    parsed = strtol(line + strlen(expected_prefix), &end, 10);
    if (errno != 0 || end == line || (*end != '\n' && *end != '\0')) {
        return 0;
    }
    if (parsed < 0 || parsed > 100) {
        return 0;
    }
    *value = (int)parsed;
    return 1;
}

int main(int argc, char **argv) {
    FILE *input = NULL;
    FILE *existing = NULL;
    FILE *output = NULL;
    char alpha_line[MAX_LINE_LENGTH];
    char beta_line[MAX_LINE_LENGTH];
    char extra_line[MAX_LINE_LENGTH];
    int alpha = 0;
    int beta = 0;
    int exit_code = EXIT_FAILURE;

    (void)argv;
    if (argc != 1) {
        fputs("This course utility accepts no arguments.\n", stderr);
        return EXIT_FAILURE;
    }

    existing = fopen(OUTPUT_FILE, "r");
    if (existing != NULL) {
        fclose(existing);
        fputs("course-summary.txt already exists; refusing to overwrite it.\n", stderr);
        return EXIT_FAILURE;
    }

    input = fopen(INPUT_FILE, "r");
    if (input == NULL) {
        fputs("The fixed course input file is unavailable.\n", stderr);
        goto cleanup;
    }
    if (fgets(alpha_line, sizeof alpha_line, input) == NULL ||
        fgets(beta_line, sizeof beta_line, input) == NULL ||
        fgets(extra_line, sizeof extra_line, input) != NULL) {
        fputs("The course input must contain exactly two bounded records.\n", stderr);
        goto cleanup;
    }
    if (!parse_course_value(alpha_line, "alpha", &alpha) ||
        !parse_course_value(beta_line, "beta", &beta)) {
        fputs("The course input did not match the documented format.\n", stderr);
        goto cleanup;
    }

    output = fopen(OUTPUT_FILE, "wx");
    if (output == NULL) {
        fputs("Could not create the fixed summary without overwriting a file.\n", stderr);
        goto cleanup;
    }
    if (fprintf(output, "course_total=%d\nbranch=%s\n", alpha + beta,
                alpha < beta ? "alpha-less-than-beta" : "alpha-not-less-than-beta") < 0) {
        fputs("Could not write the course summary.\n", stderr);
        goto cleanup;
    }
    exit_code = EXIT_SUCCESS;

cleanup:
    if (output != NULL && fclose(output) != 0) exit_code = EXIT_FAILURE;
    if (input != NULL && fclose(input) != 0) exit_code = EXIT_FAILURE;
    return exit_code;
}
