# Exit on error:
set -e

# Adapted from: https://www.kevincao.xyz/posts/dwmbar

cat > main.c << 'EOF'
#include <X11/Xlib.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>

bool run = true;

void sigint_handler() {
    run = false;
}

int main(void) {
    Display *dpy = XOpenDisplay(NULL);

    signal(SIGINT, sigint_handler);

    while(run) {
        char bar[128];

        time_t t = time(NULL);
        struct tm *tm = localtime(&t);
        int n = strftime(bar, sizeof(bar), " %d %b %R ", tm);

        XStoreName(dpy, DefaultRootWindow(dpy), bar);
        XSync(dpy, False);

        usleep(10000000);
    }

    XCloseDisplay(dpy);
}
EOF

gcc main.c -I/include -lX11 -O3 -o dwmbar

cp dwmbar /bin -v

exit 0
