summaryrefslogtreecommitdiffhomepage
path: root/src/lingot.c
blob: cc8a3035f838670799c4ee6b4bcf9137cc701d72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//-*- C++ -*-
/*
 * lingot, a musical instrument tuner.
 *
 * Copyright (C) 2004-2007  Ibán Cereijo Graña, Jairo Chapela Martínez.
 *
 * This file is part of lingot.
 *
 * lingot is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * lingot is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with lingot; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <getopt.h>

#include "lingot-defs.h"
#include "lingot-config.h"
#include "lingot-mainframe.h"
#include "lingot-i18n.h"

char CONFIG_FILE_NAME[100];

int main(int argc, char *argv[])
  {
    char *homedir;
#ifdef __WIN32__
    homedir = getenv("APPDATA");
#else
    homedir = getenv("HOME");
#endif

#ifdef ENABLE_NLS	
#ifdef __WIN32__
    bindtextdomain (GETTEXT_PACKAGE, "share/locale");
#else
    bindtextdomain (GETTEXT_PACKAGE, LINGOT_LOCALEDIR);
#endif
    bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
    textdomain (GETTEXT_PACKAGE);
#endif

    // default config file.
    sprintf(CONFIG_FILE_NAME, "%s/" CONFIG_DIR_NAME DEFAULT_CONFIG_FILE_NAME,
        homedir);

    // TODO: indicate complete config file path
    if ((argc > 3) || (argc == 2))
      {
        printf("\nusage: lingot [-c config]\n\n");
        return -1;
      }
    else if (argc > 1)
      {
        int c;
        int digit_optind = 0;

        while (1)
          {
            int this_option_optind = optind ? optind : 1;
            int option_index = 0;
            static struct option long_options[] =
              {
                  { "config", 1, 0, 'c'},
                  { 0, 0, 0, 0}
              };

            c = getopt_long (argc, argv, "c:",
                long_options, &option_index);
            if (c == -1)
            break;

            switch (c)
              {
                case 'c':
                sprintf(CONFIG_FILE_NAME, "%s/%s%s.conf", homedir,
                    CONFIG_DIR_NAME, optarg);
                printf("using config file %s\n", CONFIG_FILE_NAME);
                break;

                case '?':
                break;

                default:
                printf ("?? getopt returned character code 0%o ??\n", c);
              }
          }

        /*        if (strcmp(argv[1], "-c") || (argc < 3))
         {
         printf("invalid argument\n");
         return -1;
         }

         sprintf(CONFIG_FILE_NAME, "%s/%s%s.conf", getenv("HOME"),
         CONFIG_DIR_NAME, argv[2]);
         printf("using config file %s\n", CONFIG_FILE_NAME);*/
      }

    // if config file doesn't exists, i will create it.
    FILE* fp;
    if ((fp = fopen(CONFIG_FILE_NAME, "r")) == NULL)
      {

        char config_dir[100];
        sprintf(config_dir, "%s/%s", homedir, CONFIG_DIR_NAME);
        printf("creating directory %s ...\n", config_dir);
#ifdef __WIN32__
	mkdir(config_dir);
#else
        mkdir(config_dir, 0777); // creo el directorio.
#endif
        printf("creating file %s ...\n", CONFIG_FILE_NAME);

        // new configuration with default values.
        LingotConfig* new_conf = lingot_config_new();
        lingot_config_save(new_conf, CONFIG_FILE_NAME);
        lingot_config_destroy(new_conf);

        printf("ok\n");

      }
    else
    fclose(fp);

    LingotMainFrame* gui = lingot_mainframe_new(argc, argv);
    lingot_mainframe_run(gui);

    return 0;
  }