#!/usr/bin/env python
"""
resync-explorer: The ResourceSync explorer

Copyright 2012-2017 Simeon Warner

   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 optparse
import sys

from resync import __version__
from resync.client import ClientFatalError
from resync.client_utils import init_logging
from resync.explorer import Explorer

DEFAULT_LOGFILE = 'resync-explorer.log'


def main():

    if (sys.version_info < (2, 7)):
        sys.exit("This program requires python version 2.7 or later")

    # Options and arguments
    p = optparse.OptionParser(description='ResourceSync explorer',
                              usage='usage: %prog [options] uri_path local_path  (-h for help)',
                              version='%prog ' + __version__)

    # Specification of map between remote URI and local file paths, and remote
    # sitemap
    nam = p.add_option_group('FILE/URI NAMING OPTIONS')
    nam.add_option('--outfile', type=str, action='store',
                   help="write sitemap to specified file rather than STDOUT")
    nam.add_option('--paths', type=str, action='store',
                   help="explicit set of paths for disk scan --resourceslist or --changelist "
                        "generation")
    nam.add_option('--sitemap', type=str, action='store',
                   help="explicitly set sitemap name, overriding default sitemap.xml "
                        "appended to first source URI specified in the mappings")
    nam.add_option('--reference', type=str, action='store',
                   help="reference sitemap name for --changelist calculation")
    nam.add_option('--newreference', type=str, action='store',
                   help="updated reference sitemap name for --changelist calculation")
    nam.add_option('--dump', metavar='DUMPFILE', type=str, action='store',
                   help="write dump to specified file for --resourcelist or --changelist")
    nam.add_option('--changelist-uri', '--change-list-uri', type=str, action='store',
                   help="explicitly set the changelist URI that will be use in --inc mode, "
                        "overrides process of getting this from the sitemap")

    # Options that apply to multiple modes
    opt = p.add_option_group('MISCELANEOUS OPTIONS')
    opt.add_option('--hash', type=str, action='append',
                   help="use specified hash types in addition to last modification time "
                        "and size (repeatable, may include `md5`, `sha-1` and `sha-256`)")
    opt.add_option('--checksum', action='store_true',
                   help="use md5 checksum in addition to last modification time and size "
                        "(same as --hash=md5)")
    opt.add_option('--from', type=str, action='store', dest='from_datetime', metavar="DATETIME",
                   help="explicit datetime value used to filter updates in change list for "
                        "--incremental sync")
    opt.add_option('--exclude', type=str, action='append',
                   help="exclude resources with URI or filename matching the python regex "
                        "supplied (see: <https://docs.python.org/2/howto/regex.html> for regex "
                        "information, repeat option for multiple excludes)")
    opt.add_option('--multifile', '-m', action='store_true',
                   help="disable reading and output of sitemapindex for multifile sitemap")
    opt.add_option('--noauth', action='store_true',
                   help="disable checking of URL paths to ensure that the sitemaps refer "
                        "only to resources on the same server/sub-path etc. Use with care.")
    # These likely only useful for experimentation
    opt.add_option('--max-sitemap-entries', type=int, action='store',
                   help="override default size limits")
    # Want these to show at the end
    opt.add_option('--verbose', '-v', action='store_true',
                   help="verbose")
    opt.add_option('--logger', '-l', action='store_true',
                   help="create detailed log of explorer actions (will write "
                        "to %s unless specified with --logfile" %
                        DEFAULT_LOGFILE)
    opt.add_option('--logfile', type='str', action='store',
                   help="create detailed log of client actions")

    (args, map) = p.parse_args()

    init_logging(to_file=args.logger, logfile=args.logfile, default_logfile=DEFAULT_LOGFILE,
                 verbose=args.verbose)

    print("----- ResourceSync Explorer -----")
    if (args.checksum):
        args.hash.append('md5')
    c = Explorer(hashes=args.hash,
                 verbose=args.verbose)

    try:
        if (map):
            # Mappings apply to (almost) everything
            c.set_mappings(map)
        if (args.sitemap):
            c.sitemap_name = args.sitemap
        if (args.exclude):
            c.exclude_patterns = args.exclude
        if (args.multifile):
            c.allow_multifile = not args.multifile
        if (args.noauth):
            c.noauth = args.noauth
        if (args.max_sitemap_entries):
            c.max_sitemap_entries = args.max_sitemap_entries

        c.explore()

    # Any problem we expect will come as a ClientFatalError, anything else
    # is... an exception ;-)
    except ClientFatalError as e:
        sys.stderr.write("\nFatalError: " + str(e) + "\n")

if __name__ == '__main__':
    main()
