#!/usr/bin/python

# onpath.py
#       --copyright--                   Copyright 2007 (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--
#       September 22, 2005      bar
#       February 13, 2006       bar     default path
#       July 28, 2007           bar     make work with dirs that have double quotes around them
#       July 29, 2007           bar     comment
#       October 27, 2007        bar     optional 'all' parameter
#       November 18, 2007       bar     turn on doxygen
#       November 20, 2007       bar     comments
#       November 27, 2007       bar     insert boilerplate copyright
#       May 17, 2008            bar     email adr
#       --eodstamps--
##      \file
#
#       Tell whether a file is on the PATH or not.
#
#

import  os
import  sys


def on_env(path, file_name, all = False) :
    """
        Is the given file name found in any directory in the given "path"?

        If so, return an array of the files found, including their path.
        Otherwise, return None.
    """

    all_fnames  = []

    if  (path != None) and file_name :
        dirs    = [ p.strip('"') for p in path.split(os.pathsep) ]

        for d in dirs :
            fn  = os.path.join(os.path.normpath(d), os.path.normpath(file_name))
            if  os.path.isfile(fn) :
                if  not all :
                    return(fn)
                all_fnames.append(fn)
            pass
        pass

    if  all :
        return(all_fnames)

    return(None)


def on_path(file_name, all = False) :
    """
        Is the given file name found in any directory in the environment PATH?

        If so, return an array of the files found, including their path.
        Otherwise, return None.
    """

    fn  = on_env(os.getenv("PATH"), file_name, all)
    if  not fn :
        fn = on_env(os.defpath, file_name, all)

    return(fn)




#
#
#
if __name__ == '__main__' :

    import  tzlib
    import  TZCommandLineAtFile


    help_str = """
python onpath (options) file_name

Return ERRORLEVEL 1 if file is not on PATH env.

--var   environment variable    Use given env variable rather than PATH.

"""

    del(sys.argv[0])
    TZCommandLineAtFile.expand_at_sign_command_line_files(sys.argv)

    if  tzlib.array_find(sys.argv, [ "--help", "-h", "/?" ] ) >= 0 :
        print help_str
        sys.exit(254)


    env_var = None
    quiet   = False

    while True :
        oi  = tzlib.array_find(sys.argv, [ "--var", "-v", "/v" ] )
        if  oi < 0 :    break
        del sys.argv[oi]
        env_var = sys.argv.pop(oi)

    while True :
        oi  = tzlib.array_find(sys.argv, [ "--quiet", "-q", "/q" ] )
        if  oi < 0 :    break
        del sys.argv[oi]
        quiet = True


    if  len(sys.argv) == 0 :
        print   help_str
        sys.exit(254)


    while len(sys.argv) > 0 :

        file_name = sys.argv.pop(0)

        if  env_var :

            v   = os.getenv(env_var)
            if  v == None :
                if  not quiet :                 print "No such env variable:", env_var
                sys.exit(101)

            r = on_env(v, file_name)
            if  not r :
                if  not quiet :                 print "File", file_name, "not on", env_var
                sys.exit(102)

            pass

        else :

            r = on_path(file_name)
            if  not r :
                if  not quiet :                 print "File", file_name, "not on PATH"
                sys.exit(103)

            pass

        if  not quiet :                         print "Found", r

        pass

    pass

#
#
#
# eof

