#!python

# Copyright 2018-2019 Symlink GmbH
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import sys
import click
import pkg_resources
from   Secondlock    import SecondLock

VERSION = pkg_resources.require("cli42ndLock")[0].version

@click.group()
@click.version_option(version=VERSION, prog_name='cli42ndlock')
def secondlock():
    pass

@click.command()
@click.argument('user_name')
@click.argument('user_password')
@click.argument('passphrase')
@click.argument('private_key_file_name', type=click.File("wb") )
@click.option('--key_length', default=2**12, help='RSA key length >=2^11, default 4096')
def generate_keys(user_name, user_password, passphrase, private_key_file_name, key_length):
    try:
        s                      = SecondLock(user_name, user_password)
        private_key_content, _ = s.generateKeyPair(passphrase, key_length)
        private_key_file_name.write(private_key_content)
    except Exception as e:
        print(e)
        sys.exit(1)

@click.command()
@click.argument('user_name')
@click.argument('user_password')
@click.argument('public_key_file_name', type=click.File("rb"))
def import_public_key(user_name, user_password, public_key_file_name):
    try:
        s = SecondLock(user_name, user_password)
        s.importPublicKey(public_key_file_name.read())
    except Exception as e:
        print(e)
        sys.exit(1)

@click.command()
@click.argument('user_name')
@click.argument('user_password')
@click.argument('in_file_name',  type=click.File("rb"))
@click.argument('out_file_name',type=click.File("wb") )
@click.argument('email')
@click.option('--live_time_in_days', default=365, help='live time in days, default 1 year')
@click.option('--max_open', default=100, help='max decrypt counter, default 100')
def encrypt(user_name, user_password, in_file_name, out_file_name, email, live_time_in_days, max_open):
    try:
        s = SecondLock(user_name, user_password)
        input_content = in_file_name.read()
        output_content = s.encrypt(input_content, email, live_time_in_days, max_open)
        out_file_name.write(output_content)
    except Exception as e:
        print(e)
        sys.exit(1)

@click.command()
@click.argument('user_name')
@click.argument('user_password')
@click.argument('passphrase')
@click.argument('in_file_name',  type=click.File("rb"))
@click.argument('out_file_name', type=click.File("wb") )
@click.argument('private_key_file_name',  type=click.File("rb") )
def decrypt(user_name, user_password, passphrase, in_file_name, out_file_name, private_key_file_name):
    try:
        s = SecondLock(user_name, user_password)
        s.importPrivateKey(passphrase, private_key_file_name.read())
        output_content = s.decrypt(in_file_name.read())
        out_file_name.write(output_content)
    except Exception as e:
        print(e)
        sys.exit(1)

secondlock.add_command(generate_keys)
secondlock.add_command(import_public_key)
secondlock.add_command(encrypt)
secondlock.add_command(decrypt)

if __name__ == '__main__':
    secondlock()
