#!/usr/bin/env python
import sys
import os
from collections import OrderedDict
from os.path import expanduser

# Size of the history
histsize = 10000

# History file
HISTFILE = ".directory_history"

# Home directory of user
home = expanduser("~")

def split_after_first_semicolon(text):
    return text.split(";", 1)

def remove_duplicates(commands):
    seen = None
    no_duplicates = []
    for cmd in commands:
        if cmd != seen:
            no_duplicates.append(cmd)
            seen = cmd

    return no_duplicates

def get_commands_in_directory(directory):
    commands_dir = []
    commands_not_dir = []
    try:
        with open(home + "/" + HISTFILE, "r") as f:
            # Get commands from history which were executed in directory "directory"
            for line in f.readlines():
                try:
                    directory_in_history, command = split_after_first_semicolon(line.strip())
                except ValueError:
                    continue

                if directory_in_history == directory and len(commands_dir) < histsize:
                    commands_dir.append(command)

            # Get commands from history which where not executed in directory "directory"
            f.seek(0)
            for line in f.readlines():
                try:
                    directory_in_history, command = split_after_first_semicolon(line.strip())
                except ValueError:
                    continue

                if directory_in_history != directory and (len(commands_not_dir) + len(commands_dir)) < histsize:
                    commands_not_dir.append(command)
    except IOError:
        open(home + "/" + HISTFILE, 'a').close()

    # More important/recent commands go towards the end of the list
    # Thats why commands_dir comes after commands_not_dir
    commands = commands_not_dir + commands_dir

    # Removing duplicates which are next to each other
    commands = remove_duplicates(commands)

    return commands

def get_indices_by_substring_and_directory(directory, substring):
    commands = get_commands_in_directory(directory)

    indices = []
    for cmd in commands:
        if substring.lower() in cmd.lower():
            index = commands.index(cmd)+1
            indices.append(index)

    # Remove duplicates - removing duplicates from the front
    # [4,3,2,1,4] -> [3,2,1,4]
    indices.reverse()
    indices_unique = list(OrderedDict.fromkeys(indices))
    indices_unique.reverse()

    return indices_unique

# Return complete history for a directory if -a/-all -d DIRECTORY are arguments
if len(sys.argv) == 4 and (sys.argv[1] == "-a" or sys.argv[1] == "--all") and (sys.argv[2] == "-d"):
    directory = sys.argv[3]

    print "\n".join(get_commands_in_directory(directory))

# Return list of indizes which match a given substring
# dirhist -s/--substring SUBSTRING -d DIRECTORY
if len(sys.argv) == 5 and (sys.argv[1] == "-s" or sys.argv[1] == "--substring") and sys.argv[3] == "-d":
    directory = sys.argv[4]
    substring = sys.argv[2]

    indices = [str(i) for i in get_indices_by_substring_and_directory(directory, substring)]
    if indices:
        print "\n".join(indices)
    else:
        print "NONE"
