#!/usr/bin/env python2

# This file is part of Archivematica.
#
# Copyright 2010-2013 Artefactual Systems Inc. <http://artefactual.com>
#
# Archivematica 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.
#
# Archivematica 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 Archivematica.  If not, see <http://www.gnu.org/licenses/>.

# @package Archivematica
# @subpackage archivematicaClientScript
# @author Joseph Perry <joseph@artefactual.com>

import os
import sys
import shutil

# archivematicaCommon
from custom_handlers import get_script_logger
import archivematicaFunctions

if __name__ == '__main__':
    logger = get_script_logger("archivematica.mcp.client.restructureBagAIPToSIP")

    path = sys.argv[1]
    os.makedirs(os.path.join(path, "metadata/submissionDocumentation"))

    # Move everything to submission documentation
    for item in os.listdir(path):
        if item in archivematicaFunctions.OPTIONAL_FILES + ["metadata"]:
            continue
        src = os.path.join(path, item)
        dst = os.path.join(path, "metadata/submissionDocumentation", item)
        print 'Moving', src, 'to', dst
        shutil.move(src, dst)
    src = os.path.join(path, "metadata/submissionDocumentation", "data/objects")
    dst = path
    if os.path.exists(src):
        print 'Moving', src, 'to', dst
        shutil.move(src, dst)

    archivematicaFunctions.create_structured_directory(path, manual_normalization=True, printing=True)

@@ -1025,16 +1025,237 @@
 ctions%0A%0A
+def _move_file(src, dst, exit_on_error=True):%0A    print 'Moving', src, 'to', dst%0A    try:%0A        shutil.move(src, dst)%0A    except IOError:%0A        print 'Could not move', src%0A        if exit_on_error:%0A            raise%0A%0A
 if __nam
@@ -1360,16 +1360,20 @@
 %22)%0A%0A    
+sip_
 path = s
@@ -1387,23 +1387,79 @@
 %5B1%5D%0A
+%0A
     
-os.makedirs
+# Move everything out of data directory%0A    for item in os.listdir
 (os.
@@ -1472,234 +1472,256 @@
 oin(
+sip_
 path, 
-%22metadata/submissionDocumentation%22))%0A
+'data')):
 %0A    
-# Move everything to submission documentation%0A    for item in os.listdir(path):%0A        if item in archivematicaFunctions.OPTIONAL_FILES + %5B%22metadata%22%5D:%0A            continue%0A    
+    src = os.path.join(sip_path, 'data', item)%0A        dst = os.path.join(sip_path, item)%0A        _move_file(src, dst)%0A%0A    os.rmdir(os.path.join(sip_path, 'data'))%0A%0A    # Move metadata and logs out of objects if they exist%0A
     
@@ -1739,32 +1739,49 @@
 th.join(
+sip_
 path, 
-item)%0A    
+'objects', 'metadata')%0A
     dst 
@@ -1791,39 +1791,43 @@
 s.path.join(
+sip_
 path, 
-%22
+'
 metadata
 /submissionD
@@ -1818,120 +1818,363 @@
 data
-/submissionDocumentation%22, item)%0A        print 'Moving', src, 'to', dst%0A        shutil.move(src, dst)%0A    src = 
+')%0A    _move_file(src, dst, exit_on_error=False)%0A%0A    src = os.path.join(sip_path, 'objects', 'logs')%0A    dst = os.path.join(sip_path, 'logs')%0A    _move_file(src, dst, exit_on_error=False)%0A%0A    # Move anything unexpected to submission documentation%0A    # Leave objects, metadata, etc%0A    # Original METS ends up in submissionDocumentation%0A    os.makedirs(
 os.p
@@ -2186,23 +2186,27 @@
 oin(
+sip_
 path, 
-%22
+'
 metadata
 /sub
@@ -2201,17 +2201,20 @@
 metadata
-/
+', '
 submissi
@@ -2232,127 +2232,347 @@
 tion
-%22, %22data/objects%22)%0A    dst = path%0A    if os.path.exists(src):%0A        print 'Moving', src, 'to', dst%0A        shutil.mov
+'))%0A    for item in os.listdir(sip_path):%0A        # Leave SIP structure%0A        if item in archivematicaFunctions.OPTIONAL_FILES + archivematicaFunctions.REQUIRED_DIRECTORIES:%0A            continue%0A        src = os.path.join(sip_path, item)%0A        dst = os.path.join(sip_path, 'metadata', 'submissionDocumentation', item)%0A        _move_fil
 e(sr
@@ -2635,16 +2635,20 @@
 rectory(
+sip_
 path, ma

Restructure reingest AIP to resemble SIP