public static void main(String[] args) throws Exception {

        if (args.length != 4 && args.length != 5) {
            System.err.println(USAGE);
            System.exit(1);
        }

        File metadataFile = new File(args[0]);
        File outputFile = new File(args[1]);
        File pkcs12File = new File(args[2]);
        String passwd = args[3];
        String email = null;
        if (args.length == 5) {
            email = args[4];
        }

        X509Info x509Info = null;
        Document doc = null;

        try {

            KeyStore keyStore = X509Utils.pkcs12ToKeyStore(pkcs12File, passwd);
            x509Info = X509Utils.x509FromKeyStore(keyStore, passwd);

        } catch (FileNotFoundException e) {
            System.err.println("certificate file not found: " + pkcs12File);
            System.exit(1);
        } catch (Exception e) {
            System.err.println("error loading certificate (wrong password?)");
            System.exit(1);
        }

        try {

            DocumentBuilder db = XMLUtils.newDocumentBuilder(false);
            doc = db.parse(metadataFile);

            MetadataUtils.signMetadataEntry(doc, x509Info, email);

        } catch (Exception e) {
            System.err.println(e.getMessage());
            System.exit(1);
        }

        try {

            String signedContents = XMLUtils.documentToString(doc);
            writeStringToFile(signedContents, outputFile);

        } catch (Exception e) {
            removeOutputFile(outputFile);
            System.err.println(e.getMessage());
            System.exit(1);
        }
    }