#!/usr/bin/env python

"""
Move data_files directory from default location (sys.prefix) or specified location
to chosen directory.
Results in PmagPy-data directory which contains notebooks and data_files.
"""

import shutil
import sys
import os
from os import path
from pmagpy import pmag
from pmagpy import find_pmag_dir


def copy_directory(src, dest):
    dest = path.join(dest, 'data_files')
    print('-I- Copying {} to {}'.format(src, dest))
    try:
        shutil.copytree(src, dest)
        # Directories are the same
    except shutil.Error as error:
        print('Directory not copied. Error: %s' % error)
        # Any error saying that the directory doesn't exist
    except OSError as error:
        print('Directory not copied. Error: %s' % error)


def main():
    if '-h' in sys.argv:
        print("Choose the folder where you want the PmagPy data files to be.")
        print("Navigate to that folder, and use the command: 'move_data_files.py'")
        print("Alternatively, you may use the full path to the directory of your choice from anywhere in the file system, using the '-d' flag: 'move_data_files.py -d /Users/***/Desktop' where *** is your username")
        sys.exit()
    # create PmagPy-data directory
    dest = pmag.get_named_arg('-d', '.', False)
    dest = path.realpath(dest)
    dest = path.join(dest, 'PmagPy-data')
    if not path.exists(dest):
        try:
            os.mkdir(dest)
        except FileNotFoundError:
            pass
    # get source of data_files
    source = pmag.get_named_arg('-s', sys.prefix, False)
    source = path.realpath(source)
    if source.endswith('data_files') or source.endswith('data_files/'):
        source = path.split(source)[0]
    # copy data_files to PmagPy-data directory
    data_files = path.join(source, 'data_files')
    copy_directory(data_files, dest)
    # now try to get notebooks
    pmagpy_dir = find_pmag_dir.get_pmag_dir()
    for notebook in ['PmagPy.ipynb', 'PmagPy-cli.ipynb']:
        # for pip install
        notebook_location = path.join(dest, 'data_files', notebook)
        if path.exists(notebook_location):
            shutil.copy(path.join(dest, 'data_files', notebook), path.join(dest, notebook))
        # for developer install
        elif pmagpy_dir:
            if path.exists(path.join(pmagpy_dir, notebook)):
                shutil.copy(path.join(pmagpy_dir, notebook), path.join(dest, notebook))


if __name__ == "__main__":
    main()

@@ -561,32 +561,36 @@
 %0A        print('
+-W- 
 Directory not co
@@ -719,16 +719,20 @@
  print('
+-W- 
 Director
@@ -765,16 +765,211 @@
  error)%0A
+        print(%22    If you have a developer install, move_data_files.py won't work.  Simply navigate to your PmagPy directory.  You can find data_files, PmagPy.ipynb and PmagPy-cli.ipynb there.%22)%0A
 %0A%0Adef ma
@@ -1014,16 +1014,20 @@
  print(%22
+    
 Choose t
@@ -1097,16 +1097,20 @@
  print(%22
+    
 Navigate
@@ -1185,16 +1185,20 @@
  print(%22
+    
 Alternat
@@ -1389,16 +1389,212 @@
 rname%22)%0A
+        print(%22    **IMPORTANT** If you have a developer install, this won%5C't work.  Simply navigate to your PmagPy directory.  You can find data_files, PmagPy.ipynb and PmagPy-cli.ipynb there.%22)%0A
         

Add an informative error message if someone with a developer install tries to run move_data_files.py, fixes #452