# -*- coding: utf-8 -*-
#
# The internetarchive module is a Python/CLI interface to Archive.org.
#
# Copyright (C) 2012-2016 Internet Archive
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""Move and rename files in archive.org items.

usage:
    ia move <src-identifier>/<src-file> <dest-identifier>/<dest-file> [options]...
    ia move --help

options:
    -h, --help
    -m, --metadata=<key:value>...  Metadata to add to your new item, if you are moving
                                   the file to a new item.
"""
from __future__ import print_function, absolute_import
import sys

from docopt import docopt

from internetarchive.cli import ia_copy


def main(argv, session):
    args = docopt(__doc__, argv=argv)
    src_path = args['<src-identifier>/<src-file>']
    dest_path = args['<dest-identifier>/<dest-file>']

    # First we use ia_copy, prep argv for ia_copy.
    argv.pop(0)
    argv = ['copy'] + argv

    # Call ia_copy.
    r, src_file = ia_copy.main(argv, session, cmd='move')
    dr = src_file.delete(cascade_delete=True)
    if dr.status_code == 204:
        print('success: moved {} to {}'.format(src_path, dest_path))
        sys.exit(0)
    print('error: {}'.format(dr.content))
