# Process shot auxiliary files, depending on mode:
# -- if mode == 0 (ie, INDEP, as in "Example 1" below), it 
#    processes the "frmlist" files generated by index_dataset
# -- if mode != 0 (eg, if using LOC, as in "Example 2" below), it 
#    processes the shot files output by the shot detector

import os, os.path, sys, glob, math, time, re, subprocess, shutil, errno

def print_usage():
    print "python %s <frame_lists> <number_centroids> <shot_thresh> <number_fpshot> <mode> <out_shot_frmlist> <out_shot_first_frames> " % os.path.basename(__file__);
    print "Example 1: python %s " % os.path.basename(__file__);
    print " test_keyframe_lists.txt \ ";
    print " 512 0.8 5 0 \ ";
    print " test_shot_mode_0_frame_list.txt \ ";
    print " dummy ";
    print " ";
    print "Example 2: python %s " % os.path.basename(__file__);
    print " test_keyframe_lists.txt \ ";
    print " 512 0.8 -1 1 \ ";
    print " dummy \ ";
    print " test_shot_mode_1_first_frames.txt";

def file_len(fname):
    with open(fname) as f:
        for i, l in enumerate(f):
            pass
    return i + 1

if __name__ == "__main__":
    if len(sys.argv) < 8:
        print_usage();
    else:
        # 0. Get arguments
        frame_lists_file = sys.argv[1];
        with open(frame_lists_file) as f:
            frame_lists = f.readlines();
            frame_lists = [line.strip() for line in frame_lists];
        number_centroids = int(sys.argv[2]);
        shot_thresh = float(sys.argv[3]);
        number_fpshot = int(sys.argv[4]);
        mode = int(sys.argv[5]);
        out_shot_frmlist = sys.argv[6];
        out_shot_first_frames = sys.argv[7];

        # Open output files
        if mode == 0:
            fid_shot_frmlist = open(out_shot_frmlist, 'w');
        else:
            fid_shot_first_frames = open(out_shot_first_frames, 'w');

        count_total_frames = 0;
        for frame_list in frame_lists:
            # Get total frames in this frame_list
            frames_this = file_len(frame_list);

            # Split into root and ext
            (root, ext) = os.path.splitext(frame_list);

            if mode == 0:
                # Read frmlist for this configuration and write to output
                new_extension = "sift_scfv_idx_k%d_shot_t%.1f_n%d_m%d.frmlist" % (number_centroids, shot_thresh, number_fpshot, mode);
                frmlist_file = "%s.%s" % (root, new_extension);
                with open(frmlist_file) as f:
                    frmlist_lines = f.readlines();
                    frmlist_lines = [line.strip() for line in frmlist_lines];
                for item in frmlist_lines:
                    fid_shot_frmlist.write("%d\n" % (count_total_frames + int(item)));
            else:
                # Read shot first frames and write to output
                new_extension = "shot_t%.1f" % shot_thresh;
                shot_file = "%s.%s" % (root, new_extension);
                with open(shot_file) as f:
                    shot_lines = f.readlines();
                    shot_lines = [line.strip() for line in shot_lines];
                for item in shot_lines:
                    fid_shot_first_frames.write("%d\n" % (count_total_frames + int(item)));

            # Increment counter
            count_total_frames += frames_this;

        # Close output files
        if mode == 0:
            fid_shot_frmlist.close();
        else:
            fid_shot_first_frames.close();

