#!/usr/bin/env python3
#
# Copyright (C) 2009 Leandro Lisboa Penz <lpenz@lpenz.org>
# This file is subject to the terms and conditions defined in
# file 'LICENSE', which is part of this source code package.

"""
ftpsmartsync is a program that synchronize all files beneath the
current directory with an FTP host efficiently.

The destination host is identified by a .ftp_upstream in the
current directory that must have the following line:
upstream=ftp://user@host/path

The password is found by looking at ~/.netrc, see netrc(5).

ftpsmartsync sends all files in the current directory to the target host,
and stores the MD5 of the sent files in a hashes.txt files in the
remote host. When syncing again, it checks the MD5 of each file
against the one stored in the remote hashes.txt file, and only sends
the files that are different. This makes ftpsmartsync very efficient
when synchronizing a directory with only a few different files,
as long as they are always sent by ftpsmartsync.
"""

import argparse
import logging

import ftpsmartsync


class LoggingAction(argparse.Action):
    def __init__(self, nargs=None, **kwargs):
        if nargs is not None:
            raise ValueError("nargs not allowed")
        super(LoggingAction, self).__init__(nargs=0, **kwargs)

    def __call__(self, parser, namespace, values, option_string=None):
        raise NotImplementedError


class LoggingVerbose(LoggingAction):
    def __call__(self, parser, namespace, values, option_string=None):
        logging.getLogger().setLevel(logging.INFO)


class LoggingDebug(LoggingAction):
    def __call__(self, parser, namespace, values, option_string=None):
        logging.getLogger().setLevel(logging.DEBUG)


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument(
        "--version", action="version", version="%(prog)s " + ftpsmartsync.__version__
    )
    parser.add_argument(
        "-s",
        "--safe",
        action="store_true",
        default=False,
        help="Safe mode: sends hashes.txt after every " "successful file transfer.",
    )
    parser.add_argument(
        "-v", "--verbose", action=LoggingVerbose, help="Be more verbose"
    )
    parser.add_argument(
        "-d", "--debug", action=LoggingDebug, help="Show debug messages"
    )
    args = parser.parse_args()

    ftpsmartsync.ftpsmartsync(path=".", safe=args.safe)


if __name__ == "__main__":
    main()
