#!/usr/bin/env python2.7

import sys
import os.path
dev_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..'))
sys.path.insert(0, dev_path)

import argparse
import subprocess

parser = argparse.ArgumentParser(description='Check a certificate against the CA')

parser.add_argument('-n', '--name',
                    default='normal', 
                    help='Name used for the filenames')

parser.add_argument('-o', '--output_path',
                    default='', 
                    help='Output path')

args = parser.parse_args()

if args.output_path != '':
    output_path = args.output_path
else:
    sys.stdout.write("Please confirm input directory []: ")
    output_path = raw_input().strip()

cmd = ['openssl', 
       'verify', 
       '-verbose', 
       '-CAfile', os.path.join(output_path, 'ca.crt.pem'), 
       os.path.join(output_path, args.name + '.crt.pem')]

p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if p.wait() != 0:
    raise EnvironmentError("Verification failed:\n%s" % 
                           (p.stderr.read(),))

print("Verified.")
