#!/usr/bin/env python
# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------------------------
# Copyright (c) 2011-2015, Ryan Galloway (ryan@rsgalloway.com)
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#  - Redistributions of source code must retain the above copyright notice,
#    this list of conditions and the following disclaimer.
#
#  - Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#
#  - Neither the name of the software nor the names of its contributors
#    may be used to endorse or promote products derived from this software
#    without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
# -----------------------------------------------------------------------------

import os
import sys
import optparse
import logging
import pyseq


def main():
    """
    Command-line interface.
    """

    usage = """
lss [path] [-f format] [-d]

Formatting options:

    You can format the output of lss using the --format option and passing
    in a format string.

    Default format string is "%s"

    Supported directives:
        %%s   sequence start
        %%e   sequence end
        %%l   sequence length
        %%f   list of found files
        %%m   list of missing files
        %%p   padding, e.g. %%06d
        %%r   absolute range, start-end
        %%R   expanded range, start-end [missing]
        %%h   string preceding sequence number
        %%t   string after the sequence number

    Format directives support padding, for example: "%%04l".
    """ % pyseq.global_format

    parser = optparse.OptionParser(usage=usage,
                                   version="%prog " + pyseq.__version__)
    parser.add_option("-f", "--format", dest="format",
                      default=pyseq.global_format, help="format the output "
                      "string")
    parser.add_option("-d", "--debug", dest="debug", action="store_true",
                      default=False,
                      help="set logging level to debug (or $PYSEQ_LOG_LEVEL)")
    (options, args) = parser.parse_args()

    if options.debug:
        pyseq.log.setLevel(logging.DEBUG)

    if len(args) == 0:
        args = os.listdir(os.getcwd())
    elif len(args) == 1 and os.path.isdir(args[0]):
        args = os.listdir(args[0])
    for seq in pyseq.get_sequences(args):
        print(seq.format(options.format))

    return 0


if __name__ == '__main__':
    sys.exit(main())
