# Path: ififuncs.py
# def hashlib_manifest(manifest_dir, manifest_textfile, path_to_remove):
#     '''
#     Creates an MD5 manifest with relative filepaths.
#     '''
#     file_count = 0
#     for root, directories, filenames in os.walk(manifest_dir):
#         filenames = [f for f in filenames if not f[0] == '.']
#         directories[:] = [d for d in directories if not d[0] == '.']
#         for files in filenames:
#             #print(" - Calculating number of files to process in current directory -  %s files        \r "% file_count)
#             print("- Calculating number of files to process in current directory - {0} files ".format(file_count), end="\r")
#             file_count += 1
#     manifest_generator = ''
#     md5_counter = 1
#     for root, directories, filenames in os.walk(manifest_dir):
#         filenames = [f for f in filenames if f[0] != '.']
#         directories[:] = [d for d in directories if d[0] != '.']
#         for files in filenames:
#             print(' - Generating MD5 for %s - file %d of %d' % (os.path.join(root, files), md5_counter, file_count))
#             md5 = hashlib_md5(os.path.join(root, files))
#             md5_counter += 1
#             root2 = os.path.abspath(root).replace(path_to_remove, '')
#             try:
#                 if root2[0] == '/':
#                     root2 = root2[1:]
#                 if root2[0] == '\\':
#                     root2 = root2[1:]
#             except: IndexError
#             manifest_generator += md5[:32] + '  ' + os.path.join(root2, files).replace("\\", "/") + '\n'
#     manifest_list = manifest_generator.splitlines()
#     files_in_manifest = len(manifest_list)
#     # http://stackoverflow.com/a/31306961/2188572
#     manifest_list = sorted(manifest_list, key=lambda x: (x[34:]))
#     with open(manifest_textfile, "w", encoding='utf-8') as fo:
#         for i in manifest_list:
#             fo.write((unicodedata.normalize('NFC', i) + '\n'))
# 
# Path: ififuncs.py
# def generate_log(log, what2log):
#     if not os.path.isfile(log):
#         with open(log, "w", encoding='utf-8') as fo:
#             fo.write(time.strftime("%Y-%m-%dT%H:%M:%S ")
#                      + getpass.getuser()
#                      + ' ' + what2log + ' \n')
#     else:
#         with open(log, "a", encoding='utf-8') as fo:
#             fo.write(time.strftime("%Y-%m-%dT%H:%M:%S ")
#                      + getpass.getuser()
#                      + ' ' + what2log + ' \n')


import argparse
import os
import shutil
from ififuncs import hashlib_manifest
from ififuncs import generate_log
        ' Designed for a specific IFI Irish Film Archive workflow. '
        'Written by Kieran O\'Leary.'
    )
    parser.add_argument(
        'input', help='file path of parent directory'
    )
    parser.add_argument(
        '-v', action='store_true',
        help='verbose mode - some extra information such as overall file count.'
    )
    return parser


def create_manifest(source):
    '''
    Generates a master log and creates checksum manifests for all subdirectories.
    '''
    master_log = os.path.expanduser('~/Desktop/batchfixity_errors.log')
    os.chdir(source)
    for dirname in os.walk('.').next()[1]:
        full_path = os.path.join(source, dirname)
        manifest_textfile = '%s/%s_manifest.md5' % (full_path, dirname)
        if not os.path.isfile(manifest_textfile):
            log_name = '%s/%s_fixity.log' % (
                os.path.dirname(full_path), dirname
            )
            generate_log(log_name, 'batchfixity started')
            generate_log(log_name, '%s created' % manifest_textfile)
            try:
                hashlib_manifest(full_path, manifest_textfile, full_path)

# Path: ififuncs.py
# def hashlib_manifest(manifest_dir, manifest_textfile, path_to_remove):
#     '''
#     Creates an MD5 manifest with relative filepaths.
#     '''
#     file_count = 0
#     for root, directories, filenames in os.walk(manifest_dir):
#         filenames = [f for f in filenames if not f[0] == '.']
#         directories[:] = [d for d in directories if not d[0] == '.']
#         for files in filenames:
#             #print(" - Calculating number of files to process in current directory -  %s files        \r "% file_count)
#             print("- Calculating number of files to process in current directory - {0} files ".format(file_count), end="\r")
#             file_count += 1
#     manifest_generator = ''
#     md5_counter = 1
#     for root, directories, filenames in os.walk(manifest_dir):
#         filenames = [f for f in filenames if f[0] != '.']
#         directories[:] = [d for d in directories if d[0] != '.']
#         for files in filenames:
#             print(' - Generating MD5 for %s - file %d of %d' % (os.path.join(root, files), md5_counter, file_count))
#             md5 = hashlib_md5(os.path.join(root, files))
#             md5_counter += 1
#             root2 = os.path.abspath(root).replace(path_to_remove, '')
#             try:
#                 if root2[0] == '/':
#                     root2 = root2[1:]
#                 if root2[0] == '\\':
#                     root2 = root2[1:]
#             except: IndexError
#             manifest_generator += md5[:32] + '  ' + os.path.join(root2, files).replace("\\", "/") + '\n'
#     manifest_list = manifest_generator.splitlines()
#     files_in_manifest = len(manifest_list)
#     # http://stackoverflow.com/a/31306961/2188572
#     manifest_list = sorted(manifest_list, key=lambda x: (x[34:]))
#     with open(manifest_textfile, "w", encoding='utf-8') as fo:
#         for i in manifest_list:
#             fo.write((unicodedata.normalize('NFC', i) + '\n'))
# 
# Path: ififuncs.py
# def generate_log(log, what2log):
#     if not os.path.isfile(log):
#         with open(log, "w", encoding='utf-8') as fo:
#             fo.write(time.strftime("%Y-%m-%dT%H:%M:%S ")
#                      + getpass.getuser()
#                      + ' ' + what2log + ' \n')
#     else:
#         with open(log, "a", encoding='utf-8') as fo:
#             fo.write(time.strftime("%Y-%m-%dT%H:%M:%S ")
#                      + getpass.getuser()
#                      + ' ' + what2log + ' \n')


# Path: legacy_scripts/batchfixity.py
import argparse
import os
import shutil
from ififuncs import hashlib_manifest
from ififuncs import generate_log

        ' Designed for a specific IFI Irish Film Archive workflow. '
        'Written by Kieran O\'Leary.'
    )
    parser.add_argument(
        'input', help='file path of parent directory'
    )
    parser.add_argument(
        '-v', action='store_true',
        help='verbose mode - some extra information such as overall file count.'
    )
    return parser


def create_manifest(source):
    '''
    Generates a master log and creates checksum manifests for all subdirectories.
    '''
    master_log = os.path.expanduser('~/Desktop/batchfixity_errors.log')
    os.chdir(source)
    for dirname in os.walk('.').next()[1]:
        full_path = os.path.join(source, dirname)
        manifest_textfile = '%s/%s_manifest.md5' % (full_path, dirname)
        if not os.path.isfile(manifest_textfile):
            log_name = '%s/%s_fixity.log' % (
                os.path.dirname(full_path), dirname
            )
            generate_log(log_name, 'batchfixity started')
            generate_log(log_name, '%s created' % manifest_textfile)
            try:
                hashlib_manifest(full_path, manifest_textfile, full_path)

                generate_log(log_name, 'manifest creation complete')