#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <errno.h>
#include <string.h>
#include <conio.h>

static const char *dow_name(uint8_t d)
{
    /* Sprinter SYSTIME returns dow 1-based: 1=Sun..7=Sat. */
    static const char *names[] = { "?", "Sun", "Mon", "Tue",
                                   "Wed", "Thu", "Fri", "Sat" };
    return (d >= 1 && d <= 7) ? names[d] : names[0];
}

int main(void)
{
    /* --- time ----------------------------------------------------------- */
    datetime_t dt;
    getdatetime(&dt);
    printf("getdatetime: %04u-%02u-%02u %02u:%02u:%02u (dow=%u %s)\n",
           dt.year, dt.month, dt.day,
           dt.hour, dt.minute, dt.second,
           dt.dow, dow_name(dt.dow));

    /* dow convention probe — set to known dates, read back.
     * Discriminating test: today (Thu) gave dow=5, which fits either
     *   (a) Sun=1, Mon=2, ..., Sat=7   (DOS 1-based starting Sun)
     *   (b) Sat=0, Sun=1, ..., Fri=6   (week starts Saturday)
     * Saturday's number tells us which one. */
    datetime_t saved = dt;
    datetime_t probe;

    probe = dt; probe.year = 2026; probe.month = 5; probe.day = 30;  /* Sat */
    if (setdatetime(&probe) == 0) {
        getdatetime(&probe);
        printf("probe 2026-05-30 (Sat expected): dow=%u %s\n",
               probe.dow, dow_name(probe.dow));
    }

    probe = dt; probe.year = 2026; probe.month = 5; probe.day = 31;  /* Sun */
    if (setdatetime(&probe) == 0) {
        getdatetime(&probe);
        printf("probe 2026-05-31 (Sun expected): dow=%u %s\n",
               probe.dow, dow_name(probe.dow));
    }

    probe = dt; probe.year = 2026; probe.month = 6; probe.day = 1;   /* Mon */
    if (setdatetime(&probe) == 0) {
        getdatetime(&probe);
        printf("probe 2026-06-01 (Mon expected): dow=%u %s\n",
               probe.dow, dow_name(probe.dow));
    }

    /* Restore original. */
    if (setdatetime(&saved) == 0)
        printf("restored original date\n");
    else
        printf("setdatetime(restore) failed: errno=%d\n", errno);

    /* --- getcwd --------------------------------------------------------- */
    char cwd[256];
    if (getcwd(cwd, sizeof cwd) == NULL) {
        printf("getcwd failed: errno=%d (%s)\n", errno, strerror(errno));
    } else {
        printf("cwd before:  \"%s\"\n", cwd);
    }

    /* --- mkdir / chdir / getcwd / rmdir --------------------------------- */
    const char *test_dir = "TMPTEST";

    /* If a previous run left it behind, ignore the mkdir failure. */
    if (mkdir(test_dir) < 0) {
        printf("mkdir(%s) -> errno=%d (%s) [continuing]\n",
               test_dir, errno, strerror(errno));
    } else {
        printf("mkdir(%s) OK\n", test_dir);
    }

    if (chdir(test_dir) < 0) {
        printf("chdir(%s) failed: errno=%d (%s)\n",
               test_dir, errno, strerror(errno));
        return 1;
    }
    printf("chdir(%s) OK\n", test_dir);

    if (getcwd(cwd, sizeof cwd) != NULL)
        printf("cwd after:   \"%s\"\n", cwd);

    /* Back out, then remove. */
    if (chdir("\\") < 0)
        printf("chdir(\\) failed: errno=%d\n", errno);
    else
        printf("chdir(\\) OK\n");

    if (rmdir(test_dir) < 0)
        printf("rmdir(%s) failed: errno=%d (%s)\n",
               test_dir, errno, strerror(errno));
    else
        printf("rmdir(%s) OK\n", test_dir);

    if (getcwd(cwd, sizeof cwd) != NULL)
        printf("cwd final:   \"%s\"\n", cwd);

    puts("\npress any key to exit");
    (void)getch();
    return 0;
}
