#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2010-2012 Bastian Kleineidam
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""
patool [extract|list|create|formats] [sub-command-options] <command-args>
"""
import os
import sys
from patoolib import handle_archive, list_formats, baker

# parameter help and short options
params = {
    "verbose": "Verbose operation (if the helper application supports it).",
}
shortopts = {"verbose": "v"}


def handle_multi_archive(archives, cmd, **kwargs):
    """Handle a multi-archive command."""
    res = 0
    for archive in archives:
        if not os.path.isfile(archive):
            res = 1
            msg = "archive %r is not a file" % archive
            print >>sys.stderr, "patool error:", msg
        else:
            newres = handle_archive(archive, cmd, **kwargs)
            # return error if one of the archives could not be extracted
            if newres:
                res = newres
    return res


extract_params = {
  "outdir": "Extract to given directory.",
}
extract_params.update(params)
@baker.command(default=True, shortopts=shortopts, params=extract_params)
def extract (archive, *archives, **kwargs):
    """Extract files from archive(s)."""
    return handle_multi_archive((archive,)+archives, 'extract', **kwargs)


@baker.command(shortopts=shortopts, params=params)
def list (archive, *archives, **kwargs):
    """List files in archive(s)."""
    return handle_multi_archive((archive,)+archives, 'list', **kwargs)


@baker.command(shortopts=shortopts, params=params)
def test (archive, *archives, **kwargs):
    """Test files in archive(s)."""
    return handle_multi_archive((archive,)+archives, 'test', **kwargs)


@baker.command(shortopts=shortopts, params=params)
def create (archive, file1, *files, **kwargs):
    """Create an archive from given files."""
    allfiles = (file1,)+files
    return handle_archive(archive, 'create', *allfiles, **kwargs)


@baker.command(shortopts=shortopts, params=params)
def diff (archive1, archive2, **kwargs):
    """Show differences between two archives."""
    from patoolib import diff
    return diff(archive1, archive2, **kwargs)


@baker.command(shortopts=shortopts, params=params)
def repack (archive1, archive2, **kwargs):
    """Repackage one archive in another format."""
    from patoolib import repack
    return repack(archive1, archive2, **kwargs)


@baker.command
def formats ():
    """List supported and available archive formats."""
    return list_formats()

try:
    sys.exit(baker.run())
except baker.CommandError, msg:
    print >>sys.stderr, "patool error:", msg
    baker.help(sys.argv[0])
    sys.exit(1)
