#!/usr/bin/env python
# -*- coding: utf-8 -*-

#Copyright (C) Fiz Vazquez vud1@sindominio.net

#This program 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 2
#of the License, or (at your option) any later version.

#This program 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 this program; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

from __future__ import print_function

import sys
import os
import glob
import platform

def _max(one, two):
    '''Function to determine the max of two versions of format /xx/xxx/????-1.2.3 '''
    if one is None:
        return two
    elif two is None:
        return one
    try:
        one_v = os.path.basename(one).split('-')[1]
        two_v = os.path.basename(two).split('-')[1]
    except IndexError:
        #Got format that doesnt fit what we expect
        #Do simple comparison
        if os.path.basename(one) > os.path.basename(two):
            return one
        else:
            return two
    

    one_v_nos = one_v.split('.')
    two_v_nos = two_v.split('.')

    for index, no in enumerate(one_v_nos):
        if index >= len(two_v_nos):
            return one
        elif no > two_v_nos[index]:
            return one
        elif no < two_v_nos[index]:
            return two
        else:
            pass
    if len(one_v_nos) >= len(two_v_nos):
        return one
    return two


bin_path = os.path.realpath(os.path.dirname(__file__)) # directory that the pytrainer script executes from e.g. /usr/bin or /usr/local/bin
base_path = os.path.dirname(bin_path)
#Get the version of the running python interpreter
ver = platform.python_version_tuple()

if (os.path.exists(base_path + "/INSTALL") 
    and os.path.exists(base_path + "/setup.py") 
    and os.path.exists(base_path + "/pytrainer/main.py")
    and os.path.exists(base_path + "/locale")):
    print("running pytrainer from source path")
    data_path = base_path + "/"
    site_path = base_path
    gettext_path = base_path + "/locale"
else:
    print("running pytrainer from egg installation")
    parts = os.path.split(base_path)
    if parts[1] == 'EGG-INFO':
        base_path = parts[0]
    data_path = base_path + "/share/pytrainer/"
    site_path =  "%s/lib/python%s.%s/site-packages" % (base_path, ver[0], ver[1])
    gettext_path = base_path + "/share/locale"

print("data_path: " + data_path)
print("gettext_path: " + gettext_path)
print("site_path: " + site_path)

#ensure pytrainer directory is included in import path
sys.path.insert(0, site_path)

def main():
    import pytrainer.lib.localization
    pytrainer.lib.localization.initialize_gettext(gettext_path)
    from pytrainer.main import pyTrainer
    pytrainer = pyTrainer(None, data_path)

if __name__ == "__main__":
        main()

