/*
 * vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
 */
#include "e.h"

static void _e_help(void);

/* externally accessible functions */
int
main(int argc, char **argv)
{
   int i = 0;
   int valid_args = 0;
   Eet_File *ef = NULL;
   char buf[4096];
   int write_ops = 0;
   int size;
   char *str, *v;
   
   char *lang = NULL;
   int del_name = 0;
   int del_generic = 0;
   int del_comment = 0;
   int del_exe = 0;
   int del_win_name = 0;
   int del_win_class = 0;
   int del_win_title = 0;
   int del_win_role = 0;
   int del_icon_class = 0;
   int del_startup_notify = 0;
   int del_wait_exit = 0;
   char *file = NULL;
   char *set_name = NULL;
   char *set_generic = NULL;
   char *set_comment = NULL;
   char *set_exe = NULL;
   char *set_win_name = NULL;
   char *set_win_class = NULL;
   char *set_win_title = NULL;
   char *set_win_role = NULL;
   char *set_icon_class = NULL;
   int   set_startup_notify = -1;
   int   set_wait_exit = -1;
   int   get_name = 0;
   int   get_generic = 0;
   int   get_comment = 0;
   int   get_exe = 0;
   int   get_win_name = 0;
   int   get_win_class = 0;
   int   get_win_title = 0;
   int   get_win_role = 0;
   int   get_icon_class = 0;
   int   get_startup_notify = 0;
   int   get_wait_exit = 0;
   
   /* handle some command-line parameters */
   for (i = 1; i < argc; i++)
     {
	if ((!strcmp(argv[i], "-lang")) && (i < (argc - 1)))
	  {
	     i++;
	     lang = argv[i];
             valid_args++;
	  }
	else if ((!strcmp(argv[i], "-set-name")) && (i < (argc - 1)))
	  {
	     i++;
	     set_name = argv[i];
             valid_args++;
             write_ops++;
	  }
	else if ((!strcmp(argv[i], "-set-generic")) && (i < (argc - 1)))
	  {
	     i++;
	     set_generic = argv[i];
             valid_args++;
             write_ops++;
	  }
	else if ((!strcmp(argv[i], "-set-comment")) && (i < (argc - 1)))
	  {
	     i++;
	     set_comment = argv[i];
             valid_args++;
             write_ops++;
	  }
	else if ((!strcmp(argv[i], "-set-exe")) && (i < (argc - 1)))
	  {
	     i++;
	     set_exe = argv[i];
             valid_args++;
             write_ops++;
	  }
	else if ((!strcmp(argv[i], "-set-win-name")) && (i < (argc - 1)))
	  {
	     i++;
	     set_win_name = argv[i];
             valid_args++;
             write_ops++;
	  }
	else if ((!strcmp(argv[i], "-set-win-class")) && (i < (argc - 1)))
	  {
	     i++;
	     set_win_class = argv[i];
             valid_args++;
             write_ops++;
	  }
	else if ((!strcmp(argv[i], "-set-win-title")) && (i < (argc - 1)))
	  {
	     i++;
	     set_win_title = argv[i];
             valid_args++;
             write_ops++;
	  }
	else if ((!strcmp(argv[i], "-set-win-role")) && (i < (argc - 1)))
	  {
	     i++;
	     set_win_role = argv[i];
             valid_args++;
             write_ops++;
	  }
	else if ((!strcmp(argv[i], "-set-icon-class")) && (i < (argc - 1)))
	  {
	     i++;
	     set_icon_class = argv[i];
             valid_args++;
             write_ops++;
	  }
	else if ((!strcmp(argv[i], "-set-startup-notify")) && (i < (argc - 1)))
	  {
	     i++;
	     set_startup_notify = atoi(argv[i]);
             valid_args++;
             write_ops++;
	  }
	else if ((!strcmp(argv[i], "-set-wait-exit")) && (i < (argc - 1)))
	  {
	     i++;
	     set_wait_exit = atoi(argv[i]);
             valid_args++;
             write_ops++;
	  }
	else if ((!strcmp(argv[i], "-del-all")))
	  {
	     del_name = 1;
	     del_generic = 1;
	     del_comment = 1;
	     del_exe = 1;
	     del_win_name = 1;
	     del_win_class = 1;
	     del_win_title = 1;
	     del_win_role = 1;
	     del_icon_class = 1;
	     del_startup_notify = 1;
	     del_wait_exit = 1;
             valid_args++;
             write_ops++;
	  }
	else if ((!strcmp(argv[i], "-del-name")))
	  {
	     del_name = 1;
             valid_args++;
             write_ops++;
	  }
	else if ((!strcmp(argv[i], "-del-generic")))
	  {
	     del_generic = 1;
             valid_args++;
             write_ops++;
	  }
	else if ((!strcmp(argv[i], "-del-comment")))
	  {
	     del_comment = 1;
             valid_args++;
             write_ops++;
	  }
	else if ((!strcmp(argv[i], "-del-exe")))
	  {
	     del_exe = 1;
             valid_args++;
             write_ops++;
	  }
	else if ((!strcmp(argv[i], "-del-win-name")))
	  {
	     del_win_name = 1;
             valid_args++;
             write_ops++;
	  }
	else if ((!strcmp(argv[i], "-del-win-class")))
	  {
	     del_win_class = 1;
             valid_args++;
             write_ops++;
	  }
	else if ((!strcmp(argv[i], "-del-win-title")))
	  {
	     del_win_title = 1;
             valid_args++;
             write_ops++;
	  }
	else if ((!strcmp(argv[i], "-del-win-role")))
	  {
	     del_win_role = 1;
             valid_args++;
             write_ops++;
	  }
	else if ((!strcmp(argv[i], "-del-icon-class")))
	  {
	     del_icon_class = 1;
             valid_args++;
             write_ops++;
	  }
	else if ((!strcmp(argv[i], "-del-startup-notify")))
	  {
	     del_startup_notify = 1;
             valid_args++;
             write_ops++;
	  }
	else if ((!strcmp(argv[i], "-del-wait-exit")))
	  {
	     del_wait_exit = 1;
             valid_args++;
             write_ops++;
	  }
	else if ((!strcmp(argv[i], "-h")) ||
		 (!strcmp(argv[i], "-help")) ||
		 (!strcmp(argv[i], "--h")) ||
		 (!strcmp(argv[i], "--help")))
	  {
	     _e_help();
	     exit(0);
	  }
	else if ((!strcmp(argv[i], "-get-name")))
	  {
	     get_name = 1;
	     valid_args++;
	  }
	else if ((!strcmp(argv[i], "-get-generic")))
	  {
	     get_generic = 1;
	     valid_args++;
	  }
	else if ((!strcmp(argv[i], "-get-comment")))
	  {
	     get_comment = 1;
	     valid_args++;
	  }
	else if ((!strcmp(argv[i], "-get-exe")))
	  {
	     get_exe = 1;
	     valid_args++;
	  }
	else if ((!strcmp(argv[i], "-get-win-name")))
	  {
	     get_win_name = 1;
	     valid_args++;
	  }
	else if ((!strcmp(argv[i], "-get-win-class")))
	  {
	     get_win_class = 1;
	     valid_args++;
	  }
	else if ((!strcmp(argv[i], "-get-win-title")))
	  {
	     get_win_title = 1;
	     valid_args++;
	  }
	else if ((!strcmp(argv[i], "-get-win-role")))
	  {
	     get_win_role = 1;
	     valid_args++;
	  }
	else if ((!strcmp(argv[i], "-get-icon-class")))
	  {
	     get_icon_class = 1;
	     valid_args++;
	  }
	else if ((!strcmp(argv[i], "-get-startup-notify")))
	  {
	     get_startup_notify = 1;
	     valid_args++;
	  }
	else if ((!strcmp(argv[i], "-get-wait-exit")))
	  {
	     get_wait_exit = 1;
	     valid_args++;
	  }
	else
	  file = argv[i];
     }
   if (!file)
     {
	printf("ERROR: no file specified!\n");
	_e_help();
	exit(0);
     }

    if (valid_args == 0) {
        printf("ERROR: no valid arguments!\n");
        _e_help();
        exit(0);
    }

   eet_init();
   if (write_ops != 0)
     {
        ef = eet_open(file, EET_FILE_MODE_READ_WRITE);
     }
   else
     {
        ef = eet_open(file, EET_FILE_MODE_READ);
     }
   if (!ef)
     {
	printf("ERROR: cannot open file %s for READ/WRITE\n", file);
	return -1;
     }
   if (set_name)
     {
	if (lang) snprintf(buf, sizeof(buf), "app/info/name[%s]", lang);
	else snprintf(buf, sizeof(buf), "app/info/name");
	eet_write(ef, buf, set_name, strlen(set_name), 0);
     }
   if (set_generic)
     {
	if (lang) snprintf(buf, sizeof(buf), "app/info/generic[%s]", lang);
	else snprintf(buf, sizeof(buf), "app/info/generic");
	eet_write(ef, buf, set_generic, strlen(set_generic), 0);
     }
   if (set_comment)
     {
	if (lang) snprintf(buf, sizeof(buf), "app/info/comment[%s]", lang);
	else snprintf(buf, sizeof(buf), "app/info/comment");
	eet_write(ef, buf, set_comment, strlen(set_comment), 0);
     }
   if (set_exe)
     eet_write(ef, "app/info/exe", set_exe, strlen(set_exe), 0);
   if (set_win_name)
     eet_write(ef, "app/window/name", set_win_name, strlen(set_win_name), 0);
   if (set_win_class)
     eet_write(ef, "app/window/class", set_win_class, strlen(set_win_class), 0);
   if (set_win_title)
     eet_write(ef, "app/window/title", set_win_title, strlen(set_win_title), 0);
   if (set_win_role)
     eet_write(ef, "app/window/role", set_win_role, strlen(set_win_role), 0);
   if (set_icon_class)
     eet_write(ef, "app/icon/class", set_icon_class, strlen(set_icon_class), 0);
   if (set_startup_notify >= 0)
     {
	unsigned char tmp[1];
	
	tmp[0] = set_startup_notify;
	if (set_startup_notify)
	  eet_write(ef, "app/info/startup_notify", tmp, 1, 0);
	else
	  eet_write(ef, "app/info/startup_notify", tmp, 1, 0);
     }
   if (set_wait_exit >= 0)
     {
	unsigned char tmp[1];
	
	tmp[0] = set_wait_exit;
	if (set_wait_exit)
	  eet_write(ef, "app/info/wait_exit", tmp, 1, 0);
	else
	  eet_write(ef, "app/info/wait_exit", tmp, 1, 0);
     }
   if (del_name)
     {
	char **matches = NULL;
	int match_num = 0;
	
	matches = eet_list(ef, "app/info/name*", &match_num);
	if (matches)
	  {
	     for (i = 0; i < match_num; i++) eet_delete(ef, matches[i]);
	     free(matches);
	  }
     }
   if (del_generic)
     {
	char **matches = NULL;
	int match_num = 0;
	
	matches = eet_list(ef, "app/info/generic*", &match_num);
	if (matches)
	  {
	     for (i = 0; i < match_num; i++) eet_delete(ef, matches[i]);
	     free(matches);
	  }
     }
   if (del_comment)
     {
	char **matches = NULL;
	int match_num = 0;
	
	matches = eet_list(ef, "app/info/comment*", &match_num);
	if (matches)
	  {
	     for (i = 0; i < match_num; i++) eet_delete(ef, matches[i]);
	     free(matches);
	  }
     }
   if (del_exe)
     eet_delete(ef, "app/info/exe");
   if (del_win_name)
     eet_delete(ef, "app/window/name");
   if (del_win_class)
     eet_delete(ef, "app/window/class");
   if (del_win_title)
     eet_delete(ef, "app/window/title");
   if (del_win_role)
     eet_delete(ef, "app/window/role");
   if (del_icon_class)
     eet_delete(ef, "app/icon/class");
   if (del_startup_notify)
     eet_delete(ef, "app/info/startup_notify");
   if (del_wait_exit)
     eet_delete(ef, "app/info/wait_exit");
   if (get_name)
     {
        if (lang)
          {
             snprintf(buf, sizeof(buf), "app/info/name[%s]", lang);
             v = eet_read(ef, buf, &size);
             if (!v) v = eet_read(ef, "app/info/name", &size);
          }
        else
          v = eet_read(ef, "app/info/name", &size);

        if (v)
          {
              str = malloc(size + 1);
              memcpy(str, v, size);
              str[size] = 0;
              free(v);
              printf("%s\n", str);
              free(str);
          }
     }
   if (get_generic)
     {
        if (lang)
          {
             snprintf(buf, sizeof(buf), "app/info/generic[%s]", lang);
             v = eet_read(ef, buf, &size);
             if (!v) v = eet_read(ef, "app/info/generic", &size);
          }
        else
          v = eet_read(ef, "app/info/generic", &size);

        if (v)
          {
              str = malloc(size + 1);
              memcpy(str, v, size);
              str[size] = 0;
              free(v);
              printf("%s\n", str);
              free(str);
          }
     }
   if (get_comment)
     {
        if (lang)
          {
             snprintf(buf, sizeof(buf), "app/info/comment[%s]", lang);
             v = eet_read(ef, buf, &size);
             if (!v) v = eet_read(ef, "app/info/comment", &size);
          }
        else
          v = eet_read(ef, "app/info/comment", &size);

        if (v)
          {
              str = malloc(size + 1);
              memcpy(str, v, size);
              str[size] = 0;
              free(v);
              printf("%s\n", str);
              free(str);
          }
     }
   if (get_exe)
     {
        v = eet_read(ef, "app/info/exe", &size);
        if (v)
          {
             str = malloc(size + 1);
             memcpy(str, v, size);
             str[size] = 0;
             free(v);
             printf("%s\n", str);
             free(str);
          }
     }
   if (get_win_name)
     {
        v = eet_read(ef, "app/window/name", &size);
        if (v)
          {
             str = malloc(size + 1);
             memcpy(str, v, size);
             str[size] = 0;
             free(v);
             printf("%s\n", str);
             free(str);
          }
     }
   if (get_win_class)
     {
        v = eet_read(ef, "app/window/class", &size);
        if (v)
          {
             str = malloc(size + 1);
             memcpy(str, v, size);
             str[size] = 0;
             free(v);
             printf("%s\n", str);
             free(str);
          }
     }
   if (get_win_title)
     {
        v = eet_read(ef, "app/window/title", &size);
        if (v)
          {
             str = malloc(size + 1);
             memcpy(str, v, size);
             str[size] = 0;
             free(v);
             printf("%s\n", str);
             free(str);
          }
     }
   if (get_win_role)
     {
        v = eet_read(ef, "app/window/role", &size);
        if (v)
          {
             str = malloc(size + 1);
             memcpy(str, v, size);
             str[size] = 0;
             free(v);
             printf("%s\n", str);
             free(str);
          }
     }
   if (get_icon_class)
     {
        v = eet_read(ef, "app/icon/class", &size);
        if (v)
          {
             str = malloc(size + 1);
             memcpy(str, v, size);
             str[size] = 0;
             free(v);
             printf("%s\n", str);
             free(str);
          }
     }
   if (get_startup_notify)
     {
        v = eet_read(ef, "app/info/startup_notify", &size);
        if (v)
          {
             if ((int) *v == 1)
               {
                  printf("1\n");
               }
             else
               {
                  printf("0\n");
               }
             free(v);
          }
        else
          {
             printf("Not set\n");
          }
     }
   if (get_wait_exit)
     {
        v = eet_read(ef, "app/info/wait_exit", &size);
        if (v)
          {
             if ((int) *v == 1)
               {
                  printf("1\n");
               }
             else
               {
                  printf("0\n");
               }
             free(v);
          }
        else
          {
             printf("Not set\n");
          }
     }
   
   eet_close(ef);
   eet_shutdown();
   /* just return 0 to keep the compiler quiet */
   return 0;
}

static void
_e_help(void)
{
   printf("OPTIONS:\n"
	  "  -lang LANG                 Set the language properties to modify\n"
	  "  -set-name NAME             Set the application name\n"
	  "  -set-generic GENERIC       Set the application generic name\n"
	  "  -set-comment COMMENT       Set the application comment\n"
	  "  -set-exe EXE               Set the application execute line\n"
	  "  -set-win-name WIN_NAME     Set the application window name glob\n"
	  "  -set-win-class WIN_CLASS   Set the application window class glob\n"
	  "  -set-win-title WIN_TITLE   Set the application window title glob\n"
	  "  -set-win-role WIN_ROLE     Set the application window role glob\n"
	  "  -set-icon-class CLASS_NAME Set the application icon class for themes\n"
	  "  -set-startup-notify [1/0]  Set the application startup notify flag\n"
	  "  -set-wait-exit [1/0]       Set the application wait exit flag\n"
	  "  -get-name                  Get the application name\n"
	  "  -get-generic               Get the application generic name\n"
	  "  -get-comment               Get the application comment\n"
	  "  -get-exe                   Get the application execute line\n"
	  "  -get-win-name              Get the application window name glob\n"
	  "  -get-win-class             Get the application window class glob\n"
	  "  -get-win-title             Get the application window title glob\n"
	  "  -get-win-role              Get the application window role glob\n"
	  "  -get-icon-class            Get the application icon class for themes\n"
	  "  -get-startup-notify        Get the application startup notify flag\n"
	  "  -get-wait-exit             Get the application wait exit flag\n"
	  "  -del-name                  Delete the application name\n"
	  "  -del-generic               Delete the application generic name\n"
	  "  -del-comment               Delete the application comment\n"
	  "  -del-exe                   Delete the application execute line\n"
	  "  -del-win-name              Delete the application window name\n"
	  "  -del-win-class             Delete the application window class\n"
	  "  -del-win-title             Delete the application window title glob\n"
	  "  -del-win-role              Delete the application window role glob\n"
	  "  -del-icon-class            Delete the application icon class for themes\n"
	  "  -del-startup-notify        Delete the application startup notify flag\n"
	  "  -del-wait-exit             Delete the application wait exit flag\n"
	  );
}
