#!/usr/bin/python
#Copyright Joel Schaerer 2008, 2009
#This file is part of autojump

#autojump is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#autojump is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with autojump.  If not, see <http://www.gnu.org/licenses/>.

from __future__ import division
import cPickle
import getopt
from sys import argv,exit
import os
import signal
max_keyweight=1000
dead_dirs=False #global variable (evil ;-) to know if we should save the dict at the end

def signal_handler(arg1,arg2):
    print "Received SIGINT, trying to continue"
signal.signal(signal.SIGINT,signal_handler) #Don't break on sigint

def uniqadd(list,key):
    if key not in list:
        list.append(key)

def dicadd(dic,key,increment=1):
    dic[key]=dic.get(key,0.)+increment

def match(path,pattern,path_dict,re_flags=0):
    import re
    if os.path.realpath(os.curdir)==path : return False
    if re.search(pattern,"/".join(path.split('/')[-1-pattern.count('/'):]),re_flags) is None:
        return False
    else: 
        if os.path.exists(path) : return True
        else: #clean up dead directories
            del path_dict[path]
            dead_dirs=True
            return False

def save(path_dict,dic_file):
    f=open(dic_file+".tmp",'w')
    cPickle.dump(path_dict,f,-1)
    f.flush()
    os.fsync(f)
    f.close()
    try:
        os.rename(dic_file+".tmp",dic_file) #cf. http://thunk.org/tytso/blog/2009/03/15/dont-fear-the-fsync/
    except OSError:
        pass #Fail quietly, this usually means a concurrent autojump process already did the job
    #import shutil
    #shutil.copy(dic_file+".tmp",dic_file) #cPickle.dump doesn't seem to be atomic, so this is more secure

def forget(path_dict,dic_file):
    """Gradually forget about directories. Only call from the actual jump since it can take time"""
    keyweight=sum(path_dict.values()) #Gradually forget about old directories
    if keyweight>max_keyweight: 
        for k in path_dict.keys():
            path_dict[k]*=0.9*max_keyweight/keyweight
    save(path_dict,dic_file)

def find_matches(dirs,pattern,path_dict,result_list,re_flags,max_matches):
    """Find max_matches paths that match the pattern, and add them to the result_list"""
    for path,count in dirs:
        if len(result_list) >= max_matches : break
        if match(path,pattern,path_dict,re_flags):
            uniqadd(result_list,path)

#Main code
try:
    optlist, args = getopt.getopt(argv[1:], 'a',['stat','import','completion']) 
except getopt.GetoptError, e:
    print "Unknown command line argument: %s" % e
    exit(1)

dic_file=os.path.expanduser("~/.autojump_py")
try:
    aj_file=open(dic_file)
    path_dict=cPickle.load(aj_file)
    aj_file.close()
except IOError:
    path_dict={}

if ('-a','') in optlist:
    if(args[-1] != os.path.expanduser("~")): # home dir can be reached quickly by "cd" and may interfere with other directory
        dicadd(path_dict,args[-1])
        save(path_dict,dic_file)
elif ('--stat','') in optlist:
    a=path_dict.items()
    a.sort(key=lambda e:e[1])
    for path,count in a[-100:]:
        print "%.1f:\t%s" % (count,path)
    print "Total key weight: %d" % sum(path_dict.values())
elif ('--import','') in optlist:
    for i in open(args[-1]).readlines():
        dicadd(path_dict,i[:-1])
    cPickle.dump(path_dict,open(dic_file,'w'),-1)
else:
    import re
    completion=False
    userchoice=-1 #3 if the pattern is of the form __pattern__3, otherwise -1
    results=[]
    if ('--completion','') in optlist:
        completion=True
    else:
        forget(path_dict,dic_file) #gradually forget about old directories
    if not args: pattern=""
    else: pattern=args[-1]

    if len(pattern)>0 and pattern[0]=="/" and os.path.exists(pattern): #if pattern is a full path, jump there
        if not completion : print pattern
    else:
        endmatch=re.search("__([0-9]+)",pattern)
        if endmatch:
            userchoice=int(endmatch.group(1))
            pattern=re.sub("__[0-9]+.*","",pattern)
        else:
            endmatch=re.match("(.*)__",pattern)
            if endmatch: pattern=endmatch.group(1)

        dirs=path_dict.items()
        dirs.sort(key=lambda e:e[1],reverse=True)
        find_matches(dirs,pattern,path_dict,results,re_flags=0,max_matches=9)
        dirs=path_dict.items() #we need to recreate the list since the first iteration potentially deletes paths
        dirs.sort(key=lambda e:e[1],reverse=True)
        if completion or not results: #if not found, try ignoring case. On completion always show all results
            find_matches(dirs,pattern,path_dict,results,re_flags=re.IGNORECASE,max_matches=9) 
        if dead_dirs: #save the dict if there were some non-existent directories in the database
            save(path_dict,dic_file)

        if userchoice!=-1:
            if len(results) > userchoice-1 : print results[userchoice-1]
        elif len(results) > 1 and completion:
            print "\n".join(("%s__%d__%s" % (pattern,n+1,r) for n,r in enumerate(results[:8])))
        else: 
            if results : print results[0]