#!python

import json
import os
from datetime import datetime
from random import randint

import boto3
import click
from pathlib import Path


@click.group()
def cli():
    pass


@click.command()
@click.option('--access_key', prompt='Your access key', help='s3 access key')
@click.option('--secret_key', prompt='Your secret key', help='s3 secret key')
@click.option('--bucket', prompt='Your bucket name', help='s3 bucket name')
@click.option('--endpoint_url', prompt='Your endpoint address', help='s3 endpoint address')
def set_config(access_key, secret_key, bucket, endpoint_url):
    home = str(Path.home())
    config_path = Path(f"{home}/.ibackupper.json")
    if config_path.exists():
        click.echo('ibackupper configured before')
        return
    config = open(f"{home}/.ibackupper.json", "w")
    config.write(json.dumps({
        "access_key": access_key,
        "secret_key": secret_key,
        "bucket": bucket,
        "endpoint_url": endpoint_url,
    }))
    config.close()
    click.echo('Initialized the config')


@click.command()
def get_config():
    home = str(Path.home())
    config_path = Path(f"{home}/.ibackupper.json")
    if config_path.exists():
        config = open(f"{home}/.ibackupper.json", "r")
        click.echo(json.load(config))
        config.close()
    else:
        click.echo('ibackupper not configured')


@click.command()
def clear_config():
    home = str(Path.home())
    config_path = Path(f"{home}/.ibackupper.json")
    if config_path.exists():
        click.echo('config cleared')
        os.remove(config_path)
    else:
        click.echo('ibackupper not configured')


@click.command()
@click.option('--path', prompt='Your path', help='path for backup')
@click.option('--name', help='path for backup')
def backup(path, name):
    home = str(Path.home())
    config_path = Path(f"{home}/.ibackupper.json")
    if config_path.exists():
        config_file = open(f"{home}/.ibackupper.json", "r")
        config = json.load(config_file)
        a_path = Path(path)
        if a_path.exists():
            a_name = path.split("/")[-1]
            if name:
                a_name = name

            backup_name = a_name + "---" + str(datetime.date(datetime.now())) + "---" + str(
                randint(0, 999999)) + ".tar.gz"
            click.echo(backup_name)
            os.system(f"cd /tmp && tar -Pczf {backup_name} {path}")
            object_name = f"{backup_name}"
            backup_path = "/tmp/" + backup_name
            session = boto3.session.Session()
            try:
                s3_client = session.client(
                    service_name='s3',
                    aws_access_key_id=config['access_key'],
                    aws_secret_access_key=config['secret_key'],
                    endpoint_url=config['endpoint_url'],
                )
                s3_client.upload_file(backup_path, config['bucket'], object_name)
                click.echo('backup completed successfully')
            except:
                click.echo('backup failed, maybe your configuration wrong!')
            finally:
                os.remove(backup_path)
        else:
            click.echo('path not exists')

        config_file.close()
    else:
        click.echo('ibackupper not configured')


cli.add_command(set_config)
cli.add_command(get_config)
cli.add_command(clear_config)
cli.add_command(backup)

if __name__ == '__main__':
    cli()
