#!/usr/bin/python
import os
import shutil
import sys
import tempfile


CLIENT_CERT_FILENAME = 'client_certificate.pem'
CLIENT_PRIKEY_FILENAME = 'client_prikey'

if __name__ == '__main__':
    if len(sys.argv) != 4:
        print 'usage: %s <out file>  <client cert> client prikey>'%sys.argv[0]
        sys.exit(1)

    out_file = os.path.abspath(sys.argv[1])
    if os.path.exists(out_file):
        os.unlink(out_file)
    cli_cert = os.path.abspath(sys.argv[2])
    cli_prikey = os.path.abspath(sys.argv[3])

    tmpdir = tempfile.mkdtemp()
    shutil.copy(cli_cert, os.path.join(tmpdir, CLIENT_CERT_FILENAME))
    shutil.copy(cli_prikey, os.path.join(tmpdir, CLIENT_PRIKEY_FILENAME))

    os.chdir(tmpdir)
    ret = os.system('zip -e %s %s %s'%(out_file, CLIENT_CERT_FILENAME, CLIENT_PRIKEY_FILENAME))

    shutil.rmtree(tmpdir)
    if ret:
        print 'ERROR!'
        sys.exit(1)

    print 'Done!'
