Add option to force use of flat file store to moonshot-webp
[moonshot-ui.git] / src / moonshot-webp-parser.vala
1 using Moonshot;
2
3 namespace WebProvisioning
4
5
6
7   public static int main (string[] args)
8   {
9     int arg_index = -1;
10     int force_flat_file_store = 0;
11     bool bad_switch = false;
12     for (arg_index = 1; arg_index < args.length; arg_index++) {
13       int index = 0;
14       unichar c = 0;
15       string arg = args[arg_index];
16       if (arg.get_next_char(ref index, out c)) {
17         if ((c=='-') && arg.get_next_char(ref index, out c)) {
18           switch (c) {
19             case 'f':
20               force_flat_file_store = 1;
21               break;
22             default:
23               bad_switch = true;
24               break;
25           }
26         } else
27           break; // arg is not a switch; presume it's the file
28       }
29     }
30     if (bad_switch || (arg_index != args.length - 1))
31     {
32       error ("Usage %s [-f] WEB_PROVISIONING_FILE\n -f: add identities to flat file store", args[0]);
33     }
34     string webp_file = args[arg_index];
35     
36     if (!FileUtils.test (webp_file, FileTest.EXISTS | FileTest.IS_REGULAR))
37     {
38       error ("%s does not exist", webp_file);
39     }
40     
41     var webp = new Parser (webp_file);
42     webp.parse();
43     
44     foreach (IdCard card in cards)
45     {
46       Moonshot.Error error;
47       string[] rules_patterns = {};
48       string[] rules_always_confirm = {};
49         
50       if (card.rules.length > 0)
51       {
52         int i = 0;
53         rules_patterns = new string[card.rules.length];
54         rules_always_confirm = new string[card.rules.length];
55         foreach (Rule r in card.rules)
56         {
57           rules_patterns[i] = r.pattern;
58           rules_always_confirm[i] = r.always_confirm;
59           i++;
60         }
61       }
62
63       Moonshot.install_id_card (card.display_name,
64                                 card.username,
65                                 card.password,
66                                 card.issuer,
67                                 rules_patterns,
68                                 rules_always_confirm,
69                                 card.services,
70                                 card.trust_anchor.ca_cert,
71                                 card.trust_anchor.subject,
72                                 card.trust_anchor.subject_alt,
73                                 card.trust_anchor.server_cert,
74                                 force_flat_file_store,
75                                 out error);
76
77       if (error != null)
78       {
79         stderr.printf ("Error: %s", error.message);
80         continue;
81       }
82     }
83     
84     return 0;
85   }
86 }