#!/usr/bin/env python3
#
# Copyright (c) 2016 Thomas Perl <m@thp.io>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. 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.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''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 AUTHOR 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 sys
import os.path

# Insert "lib" to path if we are running from a source checkout
prefix, bindir = os.path.split(os.path.dirname(os.path.abspath(sys.argv[0])))
if bindir != 'bin':
    sys.path.insert(0, os.path.join(prefix, bindir, 'lib'))

import argparse
import rpt


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description=rpt.__doc__,
                                     formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument('filenames', metavar='FILENAME', type=str, nargs='+',
                        help='File(s) to rename')
    parser.add_argument('--command', dest='command', type=str, default='mv',
                        help='Command to execute (default: mv)')
    parser.add_argument('--input', metavar='inputarg', dest='inputarg', type=str, default=None,
                        help='Shell argument to put before the input file (default: nothing)')
    parser.add_argument('--output', metavar='outputarg', dest='outputarg', type=str, default=None,
                        help='Shell argument to put before the output file (default: nothing)')
    parser.add_argument('--swap', dest='swap', action='store_true',
                        help='Put output argument before input argument')
    parser.add_argument('--print', dest='printonly', action='store_true',
                        help='Only print commands that would be execute, do not execute')
    parser.add_argument('--version', action='version', version='%(prog)s {}'.format(rpt.__version__))
    args = parser.parse_args()

    commandline = rpt.CommandLine(args.command, args.inputarg, args.outputarg, args.swap)
    rpt.rpt(commandline, args.filenames, args.printonly)
