#include "jDockApp/jDockApp.h"
#include "jDockApp/colors.h"
#include <getopt.h>
#include "jenglish.h"

int dontread = 0;

void show_help() {
    printf("-s <sec>:  Seconds between executions of mailchk\n");
    printf("-r      :  Don't show the read mail stat.\n");

    exit(0);
}

void setup(int argc, char** argv) {
    char c;
    int  i;
    while( -1 != (c = getopt(argc, argv, "hrs:"))) {
        switch(c) {
            case '?': exit(1);
            case 'h': show_help();

            case 'r': dontread = 1;   break;
            case 's': i=atoi(optarg); break;
        }
    }
    set_update_delay((i) ? i : 15); /* seconds */
    set_loop_delay(1000); /* mu seconds */
}

void do_update() {
    int i, new, tot, read;

    FILE *f = popen("mailchk", "r");
    fscanf(f, "%i - %i - %i", &new, &tot, &read);
    pclose(f);

    clear_window();

    jpprintf(0, 0, YELLOW, "    J-mail");

    jpprintf(1, 2, BLUE, "New", new);
    jprintf(GREEN, ":");
    if(new) {
        jpprintf(7, 2, RED,  "%i", new);
    } else {
        jpprintf(7, 2, CYAN, "%i", new);
    }

    if(!dontread) {
        jpprintf(1, 3, BLUE, "Read", read);
        jprintf(GREEN, ":");
        jpprintf(7, 3, CYAN, "%i", read);
    }

    jpprintf(1, 4, BLUE, "Total", tot);
    jprintf(GREEN, ":");
    jpprintf(7, 4, CYAN, "%i", tot);
}

void do_expose() {
    do_update();
}

void do_button_release() {
    do_update();
}
