#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/inotify.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <errno.h>
#include <sys/select.h>
#include <string.h>

#define DELAY 0
#define DELAYS 1
#define NAMEOFF (sizeof(int)+sizeof(unsigned int)*3)
#define IEVTSZ (sizeof(struct inotify_event)+NAME_MAX+1)
#define SZMIN (sizeof(int)+sizeof(uint32_t)*3)

int inode, iwatch;
char *pathname=NULL;

int fileInfo(char *path, char *file) {
  char *pathfile=NULL;
  struct stat sb;
  int isstat=0, sz;

  sz = strlen(path) + strlen(file)*2 + sizeof(char)*2 + sizeof("mv /root/monitoring/cores/ "); 
  pathfile = malloc(sz);
  snprintf(pathfile, sz, "%s/%s", path, file);
  fprintf(stdout, "received %d, file is %s\n", sz, pathfile);
  isstat = stat(pathfile, &sb);
  snprintf(pathfile, sz, "mv %s/%s /root/monitoring/cores/%s", path, file, file);
  system(pathfile);
  free(pathfile);
  if (!isstat) {
    switch (sb.st_mode & S_IFMT) {
    case S_IFBLK:  printf("block device\n");            break;
    case S_IFCHR:  printf("character device\n");        break;
    case S_IFDIR:  printf("directory\n");               break;
    case S_IFIFO:  printf("FIFO/pipe\n");               break;
    case S_IFLNK:  printf("symlink\n");                 break;
    case S_IFREG:  printf("regular file\n");            break;
    case S_IFSOCK: printf("socket\n");                  break;
    default:       printf("unknown?\n");                break;
    }
    printf("I-node number: %ld\n", (long) sb.st_ino);
    printf("Mode:          %lX\n", (unsigned long) sb.st_mode);
    printf("Link count:    %ld\n", (long) sb.st_nlink);
    printf("Ownership:    UID=%ld   GID=%ld\n", (long) sb.st_uid, (long) sb.st_gid);
    printf("Preferred I/O block size: %ld bytes\n", (long) sb.st_blksize);
    printf("File size:                %lld bytes\n", (long long) sb.st_size);
    printf("Blocks allocated:         %lld\n", (long long) sb.st_blocks);
    printf("Last status change:       %s", ctime(&sb.st_ctime));
    printf("Last file access:         %s", ctime(&sb.st_atime));
    printf("Last file modification:   %s", ctime(&sb.st_mtime));
  }
  return 0;
}

// if there is more inotify event to read
int inQueue(int fd) {
  int fds;
  fd_set read;
  struct timeval tv={DELAYS, DELAY};

  FD_ZERO(&read);
  FD_SET(fd, &read);
  fds=fd+1;
  select(fds, &read, NULL, NULL, &tv);
  if (FD_ISSET(fd, &read)) {
    return 1;
  }
  return 0;
}

// read inotify event, extract filename, and call action
int catchFile() {
  int sz=0;
  struct inotify_event *evt=NULL;
  char *buffer, *filename;

  buffer = malloc(IEVTSZ);
  do {
		memset(buffer, 0, IEVTSZ);
	  sz = read(inode, buffer, IEVTSZ);
    // devrait plutôt être sur > szmin(= evt->len), que sz > 0
    if (sz < 0) {
      continue;
    } else if (sz > SZMIN) {
	    evt = (struct inotify_event*) buffer;
	    if (evt->mask & (IN_CLOSE_WRITE)) {
	      filename = buffer + NAMEOFF;
	      *(filename + evt->len) = '\0';
	      fprintf(stdout, "nouveau fichier(ou partie)! %do (min:%ld) %s / %s\n", sz, SZMIN, pathname, filename);
        fileInfo(pathname, filename);
        /*evt = next + sizeof(struct inotify_event);
        next = evt;*/
        /* buffer += sizeof(struct inotify_event);
          */
      }
	  }
  } while (inQueue(inode));
  free(buffer);
  return 0;
}

// trap sigquit
int sigQuit(int signum) {
  free(pathname);
  inotify_rm_watch(inode, iwatch);
  close(inode);
  exit(signum);
  return 1;
}

int main(int ac, char **av) {

  pathname = realpath(av[1], NULL);
  if (pathname == NULL) {
    return -1;
  }
  fprintf(stdout, "path: %s\n", pathname);

  // trap, read() error EINTR on ctrl+c
  signal(SIGINT, (__sighandler_t)sigQuit);

  inode = inotify_init1(IN_NONBLOCK);
  iwatch = inotify_add_watch(inode, pathname, IN_CLOSE_WRITE);
  if (iwatch < 0) {
    perror("inotify add:");
    return -3;
  }

  fprintf(stdout, "listening...\n");
  while (1) {
    if (inQueue(inode)) {
      catchFile();
    }
  }

  return 1;
}
