+# Strip and enable debug
+for (my $i = 0; $i <= $#ARGV; $i++) {
+       # Match redhat types
+       if ($ARGV[$i] =~ /^(?:(\-c|\-\-config)(?:=(.+))?)$/) {
+               if (defined($2) && -f $2) {
+                       $configFilename = $2;
+                       splice(@ARGV, $i, 1);
+                       $i--;
+               # Extract next parameter
+               } elsif(defined($ARGV[$i+1]) && $ARGV[$i+1] =~ /^(.+)$/ && -f $1) {
+                       $configFilename = $1;
+                       splice(@ARGV, $i, 2);
+                       $i--;
+               # Set default
+               } else {
+                       print 'Config parameter without valid file name'."\n";
+                       exit EXIT_FAILURE;
+               }
+       }
+}
+
+# Load config
+unless (
+       #XXX: use eval to workaround a fatal in decode_json
+       eval {
+               # Check file
+               (-f $configFilename) &&
+               # Read it
+               ($config = read_file($configFilename)) &&
+               # Decode it
+               ($config = decode_json($config)) &&
+               # Check hash validity
+               defined($config->{certificates}) &&
+               # Check not empty
+               scalar($config->{certificates}) &&
+               # Check hash validity
+               defined($config->{thumbprint}) &&
+               # Check certificates array
+               ! scalar map {unless(defined($_->{cert}) && defined($_->{key}) && defined($_->{mail}) && defined($_->{domain}) && defined($_->{domains})) {1;} else {();}} @{$config->{certificates}}
+       }
+) {
+       print 'Config file '.$configFilename.' is not readable or invalid'."\n";
+       exit EXIT_FAILURE;
+}
+
+# Deal with specified domains
+if (scalar(@ARGV) > 0) {
+       # Check that domains are present in config
+       foreach my $domain (@ARGV) {
+               my $found = undef;
+               foreach my $certificate (@{$config->{certificates}}) {
+                       if ($certificate->{domain} eq $domain) {
+                               push(@domains, $certificate);
+                               $found = 1;
+                       }
+               }
+               unless($found) {
+                       print 'Domain '.$domain.' not found in config file '.$configFilename."\n";
+                       exit EXIT_FAILURE;
+               }
+       }
+# Without it
+} else {
+       # Populate domains array with available ones
+       foreach my $certificate (@{$config->{certificates}}) {
+               push(@domains, $certificate);
+       }
+}