#!/usr/bin/python

# tzlib_getem.py
#       --copyright--                   Copyright 2011 (C) Tranzoa, Co. All rights reserved.    Warranty: You're free and on your own here. This code is not necessarily up-to-date or of public quality.
#       --url--                         http://www.tranzoa.net/tzpython/
#       --email--                       pycode is the name to send to. tranzoa.com is the place to send to.
#       --bodstamps--
#       March 5, 2011           bar     make generic module so only the file names need be in projects
#                                       allow dir to be ./tzl, too, not just ./tzlib
#                                       write out __init__.py
#       November 6, 2011        bar     split out subroutines
#       January 15, 2012        bar     try to run on non-flight linux systems
#       May 27, 2012            bar     doxygen namespace
#                                       return target dir from main
#       October 31, 2015        bar     copy_a_file()
#       November 7, 2017        bar     maxint->maxsize
#       --eodstamps--
##      \file
#       \namespace              tzpython.tzlib_getem
#
#
#       Get tzlib as best we can.
#
#

import  glob
import  os
import  shutil
import  sys


if  sys.platform == 'win32' :
    DSK = "I:/"
elif os.path.isdir(os.path.expanduser("~/flight")) :
    DSK = os.path.expanduser("~/flight")
else    :
    DSK = os.path.expanduser("~")

TZC     = os.path.join(DSK, "tzlib/source")
TZJ     = os.path.join(DSK, "tzjavascript")
TZP     = os.path.join(DSK, "tzpython")


__init__py  = """
import  tzlib as tzlib
for n in dir(tzlib) :
    locals()[n] = getattr(tzlib, n)         # this lets scripts import tzlib and get tzlib.py rather than the package
pass
"""


def make_target_dir_from_argv0(argv) :
    pn  = argv[0]
    return(os.path.abspath(os.path.dirname(pn)))


def copy_files(dn, all_files) :
    for fn in all_files :

        if  not os.path.isfile(fn) :
            print >> sys.stderr, "Cannot find %s!" % fn
            sys.exit(102)

        shutil.copyfile(fn, os.path.join(dn, os.path.basename(fn)))
        shutil.copystat(fn, os.path.join(dn, os.path.basename(fn)))


def make_init_py(dn) :
    fo  = open(os.path.join(dn, "__init__.py"), "w")
    fo.write(__init__py)
    fo.close()


def clean_pyc_obj(dn) :
    for fn in glob.glob(os.path.join(dn, "*.pyc")) :
        os.remove(fn)
    for fn in glob.glob(os.path.join(dn, "*.obj")) :
        os.remove(fn)

    pass


def copy_a_file(frm_fn, to_fn) :
    """ Copy a file from tz/bar/dev's location to our area. """
    frm_fn  = os.path.expanduser(os.path.expandvars(frm_fn))
    if  os.path.isdir(to_fn) :
        to_fn   = os.path.join(to_fn, os.path.basename(frm_fn))

    if  os.path.abspath(os.path.normpath(frm_fn)) == os.path.abspath(os.path.normpath(to_fn)) :
        print >>sys.stderr, "I cannot copy %s to %s - they are both the same file!" % ( frm_fn, to_fn, )
        sys.exit(101)

    if  os.path.isfile(frm_fn) :                                            # be quiet about things if the dev doesn't have the file
        dn  = os.path.dirname(to_fn)
        if  not os.path.isdir(dn) :
            print >>sys.stderr, "I cannot copy %s in to a non-existent %s directory!" % ( frm_fn, dn, )
            sys.exit(101)
        shutil.copyfile(frm_fn, to_fn)
    pass



def main(argv, all_files) :
    dn  = make_target_dir_from_argv0(argv)

    if  os.path.basename(dn) not in [ 'tzlib', 'tzl' ] :
        print >> sys.stderr, "Run me when I am in ./tzlib or ./tzl not [%s]!" % os.path.basename(dn)
        sys.exit(101)

    copy_files(dn, all_files)
    make_init_py(dn)
    clean_pyc_obj(dn)

    return(dn)



if  __name__ == '__main__' :
    # e.g.
    #   all_files   = [ os.path.join(tzlib_getem.TZP, "TZCommandLineAtFile.py"), ]
    #   tzlib_getem.main(sys.argv, all_files)
    pass

#
#
#
# eof
