def process_shell_scripts():
    """Rewrite any shell scripts created by MagicLantern.

    Currently, we only process HDR_????.SH scripts, which call enfuse. They MAY
    (well ... should) call align_image_stack first, but that depends on whether I
    remembered to choose 'align + enfuse" in Magic Lantern. Currently, up to two
    changes are made: old file names are replaced with their new file name
    equivalents, and (optionally) output is made TIFF instead of JPEG. This part of
    the script is currently heavily dependent on the structure of these Magic
    Lantern scripts (currently, they're produced by Magic Lantern firmware version
    1.0.2-ml-v2.3). In any case, this procedure creates identical output scripts
    whether or not the input script includes the align step.

    This routine REQUIRES that a set of filename mappings have already been read
    into memory; you can accomplish this by calling read_filename_mappings() to read
    an existing file_names.csv file into memory.
    """

    print('\nRewriting enfuse HDR scripts ... ')
    try:
        for which_script in glob.glob('HDR*SH'):
            print('    Rewriting %s' % which_script)
            old_perms = os.stat(which_script).st_mode
            with open(which_script, 'r') as the_script:
                script_lines = the_script.readlines()
                if script_lines[4].startswith('align_image_stack'):         # It's an align-first script, with 8 lines, 5 non-blank.
                    # Getting the script filenames takes some processing time here. It assumes a familiarity with the format of this line in ML firmware
                    # version 1.0.2-ml-v2.3, which currently looks like this:
                    #
                    #    align_image_stack -m -a OUTPUT_PREFIX INFILE1.JPG INFILE2.JPG [...]

                    # The number of infiles depends, of course, on settings that were in effect when the sequence was taken.
                    #
                    # So, the align_line, when tokenized, is, by array index:
                    #   [0] executable name
                    #   [1] -m, a switch meaning "optimize field of view for all images except for the first."
                    #   [2 and 3] -a OUTPUT_PREFIX specifies the prefix for all of the output files.
                    #   [4 to end] the names of the input files.
                    HDR_input_files = [file_name_mappings[which_file] if which_file in file_name_mappings
                                       else which_file
                                       for which_file in script_lines[4].split()[4:]]
                else:                                                       # It's a just-call-enfuse script, with 6 lines, 3 non-blank.
                    new_script = script_lines[:-1]                          # preserve the opening of the script as-is; we're only altering the last line.
                    last_line_tokens = script_lines[-1].split()             # FIXME: incorporate logic from branch above here to produce better final output.
                    HDR_input_files = [file_name_mappings[which_file] if which_file in file_name_mappings
                                       else which_file
                                       for which_file in last_line_tokens[3:]]
            hdr.create_script_from_file_list(HDR_input_files, file_to_move=which_script)
    except:
        print() # If an error occurs, end the line that's waiting to be ended before letting the error propagate.
        raise
    print('\n ... done rewriting enfuse scripts.\n')
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""This quick hack makes an HDR script from all JPG photos in the current 
directory. Then it runs it. It assumes that all of the photos are JPGs in the
current directory, that all of the JPGs in the current directory are photos for
the project, and that there are no other .SH files in the current directory.

This program comes with ABSOLUTELY NO WARRANTY. Use at your own risk.

This script is copyright 2015-16 by Patrick Mooney. It is free software, and
you are welcome to redistribute it under certain conditions, according to the
GNU general public license, either version 3 or (at your own option) any later
version. See the file LICENSE.md for details.
"""

import os, glob, subprocess

import postprocess_photos as pp # https://github.com/patrick-brian-mooney/personal-library/blob/master/postprocess_photos.py
import create_HDR_script as cHs # https://github.com/patrick-brian-mooney/personal-library/blob/master/create_HDR_script.py

the_files = sorted(glob.glob('*JPG') + glob.glob('*jpg'))
if len(the_files) > 0:
    cHs.create_script_from_file_list(the_files)
    pp.run_shell_scripts()
else:
    raise IndexError('You must call HDR_from_all.py in a folder with at least one *jpg or *JPG file;\n   current working directory is %s' % os.getcwd())