 /*
  * todo.c -- a simple todo list
  *
  * Copyright (c) 2005 Pouya Dehghani Tafti <p.d.tafti@ieee.org>
  *
  * Permission to use, copy, modify, and distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above
  * copyright notice and this permission notice appear in all copies.
  *
  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  * 
  * author: Pouya D. Tafti
  *         p dot d dot tafti at ieee dot org
  *         http://www.dete.org
  *
  * $ version 0.1 $ 2005-11-14 $
  *
  */

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

#define N 5
#define NPATH 256

int main(int argc, char *argv[])
{
    char c,i,*dummy,fpath[NPATH];
    FILE *fptr;

    while((c=getopt(argc, argv, "he")) != -1)
        switch(c) {
            case '?':
            case 'h':
                fprintf(stdout,"Usage:  todo [-he]\n\n");
                fprintf(stdout,"  -h    print this help page\n");
                fprintf(stdout,"  -e    open ~/.todo for editing\n");
                fprintf(stdout,"\nReport bugs to <p.d.tafti@ieee.org>\n");
                exit(0);
            case 'e':
                if((dummy=getenv("HOME"))==NULL) {
                    fprintf(stderr,"$HOME not set.\n");
                    exit(1);
                }
                if((dummy=getenv("VISUAL"))==NULL) {
                    fprintf(stderr,"$VISUAL not set.\n");
                    exit(1);
                }
                exit(system("$VISUAL $HOME/.todo")); 
        }

    if((dummy=getenv("HOME"))==NULL) {
        fprintf(stderr,"$HOME not set.\n");
        exit(1);
    }
    if(strlen(dummy)>NPATH-7) {
        fprintf(stderr,"The path to your home directory is too long.\n");
        fprintf(stderr,"Change NPATH in the source and recompile.\n");
        exit(1);
    }
    strcpy(fpath,dummy);
    if((fptr=fopen(strcat(fpath,"/.todo"),"r")) == NULL)
        if((fptr=fopen("/etc/todo","r")) == NULL) {
            fprintf(stderr,"Could not open ~/.todo or /etc/todo\n");
            exit(1);
        }

    fprintf(stdout,"-------\n");
    for(i=1; i<=N; i++, fseek(fptr,0,SEEK_SET)) {
        while((c=fgetc(fptr)) != EOF) {
            while(c == ' ' || c == '\t' || c == '\n')
                if((c=fgetc(fptr)) == EOF) break;

            if(c != i%5+'0') {
                do c=fgetc(fptr); while(c != '\n' && c != EOF);
                continue;
            }

            fprintf(stdout,"-- %1d --",i%5);
            do fputc(c=fgetc(fptr),stdout); while(c != '\n' && c != EOF);
        }
        fprintf(stdout,"-------\n");
    }

    fclose(fptr);
    
    return 0;
}

