work in progress
[src/app-framework-main.git] / src / wgtpkg-installer.c
1 /*
2  Copyright 2015 IoT.bzh
3
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7
8      http://www.apache.org/licenses/LICENSE-2.0
9
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16
17 #define _GNU_SOURCE
18
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <dirent.h>
23 #include <unistd.h>
24 #include <limits.h>
25 #include <errno.h>
26 #include <getopt.h>
27
28 #include "verbose.h"
29 #include "wgtpkg.h"
30
31 static const char appname[] = "wgtpkg-installer";
32 static const char *root;
33 static int force;
34
35 static void usage()
36 {
37         printf(
38                 "usage: %s [-f] [-q] [-v] [-p list] rootdir wgtfile...\n"
39                 "\n"
40                 "   rootdir       the root directory for installing\n"
41                 "   -p list       a list of comma separated permissions to allow\n"
42                 "   -f            force overwriting\n"
43                 "   -q            quiet\n"
44                 "   -v            verbose\n"
45                 "\n",
46                 appname
47         );
48 }
49
50 static struct option options[] = {
51         { "permissions", required_argument, NULL, 'p' },
52         { "force",       no_argument,       NULL, 'f' },
53         { "help",        no_argument,       NULL, 'h' },
54         { "quiet",       no_argument,       NULL, 'q' },
55         { "verbose",     no_argument,       NULL, 'v' },
56         { NULL, 0, NULL, 0 }
57 };
58
59 /* install the widgets of the list */
60 int main(int ac, char **av)
61 {
62         int i;
63
64         LOGAUTH(appname);
65
66         xmlsec_init();
67
68         force = 0;
69         for (;;) {
70                 i = getopt_long(ac, av, "hfqvp:", options, NULL);
71                 if (i < 0)
72                         break;
73                 switch (i) {
74                 case 'f':
75                         force = 1;
76                         break;
77                 case 'h':
78                         usage();
79                         return 0;
80                 case 'q':
81                         if (verbosity)
82                                 verbosity--;
83                         break;
84                 case 'v':
85                         verbosity++;
86                         break;
87                 case 'p':
88                         grant_permission_list(optarg);
89                         break;
90                 case ':':
91                         ERROR("missing argument value");
92                         return 1;
93                 default:
94                         ERROR("unrecognized option");
95                         return 1;
96                 }
97         }
98
99         ac -= optind;
100         if (ac < 2) {
101                 ERROR("arguments are missing");
102                 return 1;
103         }
104
105         /* install widgets */
106         av += optind;
107         root = *av++;
108         for ( ; *av ; av++)
109                 install_widget(*av, root, force);
110
111         return 0;
112 }
113