#!/usr/bin/python

# tz_browser.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--
#       September 24, 2011      bar
#       November 29, 2011       bar     pyflake cleanup
#       May 27, 2012            bar     doxygen namespace
#       March 7, 2015           bar     quick kludge to take http or ftp urls
#       October 26, 2015        bar     find ff on 64-bit machines
#       --eodstamps--
##      \file
#       \namespace              tzpython.tz_browser
#
#
#       Web browser stuff.
#
#

import  os
import  webbrowser
import  subprocess

try :
    WindowsError
except NameError :
    WindowsError    = AttributeError


def is_url(fn) :
    """ Is this a URL, not a file? """
    return(fn.startswith('http') or fn.startswith('ftp'))


def start_file(fn) :
    if  fn  :
        if  not is_url(fn) :
            fn  = os.path.abspath(fn)
        try     :
            if  is_url(fn) :
                raise AttributeError
            os.startfile(fn)
        except ( WindowsError, AttributeError ) :
            try :
                webbrowser.open_new_tab(fn)
            except ( webbrowser.Error, AttributeError ) :
                try :
                    webbrowser.open(fn)
                except ( webbrowser.Error, AttributeError ) :
                    try :
                        fffn        = "C:/Program Files/Mozilla Firefox/firefox.exe"
                        if  not os.path.isfile(fffn) :
                            fffn    = "C:/Program Files (x86)/Mozilla Firefox/firefox.exe"
                        if  not os.path.isfile(fffn) :
                            fffn    = "firefox"
                        url         = fn
                        if  not is_url(url) :
                            url         = "file://" + fn.replace("\\", "/")
                        # print "@@@@", url
                        subprocess.Popen( [ fffn, url ] )
                    except WindowsError :
                        return(False)
                    pass
                pass
            pass
        pass

    return(True)




if  __name__ == '__main__' :
    import  sys

    import  TZCommandLineAtFile
    import  tzlib


    program_name    = sys.argv.pop(0)
    TZCommandLineAtFile.expand_at_sign_command_line_files(sys.argv)


    help_str    = """
%s (options) file

    Run the given file, presumably by showing it in the browser.

Options:

    --sub_dirs              Look in sub-directories for the files to show/run.

""" % ( os.path.basename(program_name) )


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


    sub_dirs    = False

    while True  :
        oi  = tzlib.array_find(sys.argv, [ "--sub_dirs", "--subdirs", "--sub-dirs", "--sub_dir", "--subdir", "--sub-dir", "--sd", "-s", ])
        if  oi < 0 :    break
        del sys.argv[oi]
        sub_dirs    = True

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


    fns     = {}
    while len(sys.argv) :
        fn  = sys.argv.pop(0)
        fd  = tzlib.make_dictionary(tzlib.ambiguous_file_list(fn, do_sub_dirs = sub_dirs))
        if  not len(fd) :
            print "No files found for", fn
        fns.update(fd)

    if  not len(fns) :
        exit(101)

    ok      = True
    for fn in fns :
        ok &= start_file(fn)

    if  not ok :
        sys.exit(199)

    pass

#
#
# eof
