#!/usr/bin/env python

# Copyright (c) 2014, Tomohiro Kusumi
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
#    list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

if __name__ == '__main__':
    import cProfile
    import os
    import pstats
    import re
    import sys

    try:
        import fileobj.core
        import fileobj.nodep
        import fileobj.screen
        import fileobj.setting
        import fileobj.util
    except ImportError as e:
        sys.stderr.write("{0}\n".format(e))
        sys.exit(1)

    fileobj.nodep.test()

    d = fileobj.setting.get_user_dir()
    if not os.path.isdir(d):
        os.makedirs(d)
        if not os.path.isdir(d):
            sys.stderr.write("No {0}\n".format(d))
            sys.exit(1)

    s = fileobj.util.get_stamp("profile")
    f = os.path.join(d, s) + ".txt"
    if os.path.isfile(f):
        sys.stderr.write("{0} exists\n".format(f))
        sys.exit(1)

    keys = "time",
    for i, s in enumerate(sys.argv):
        m = re.match(r"--sort=(\S+)", s)
        if m:
            l = m.group(1).split(",")
            keys = [x for x in l if x]
            del sys.argv[i]
            break

    profile = cProfile.Profile()
    profile.enable()
    assert fileobj.util.is_running_profile()
    ret = fileobj.core.dispatch()
    assert fileobj.screen.cleanup() == -1
    profile.disable()

    fd = open(f, "w")
    stats = pstats.Stats(profile, stream=fd)
    try:
        stats.sort_stats(*keys)
    except KeyError as e:
        print(repr(e))
        keys = (1,) # time
        stats.sort_stats(*keys)
    stats.print_stats()
    fd.close()

    print("done...")
    print(keys)
    print(f)
    if ret:
        sys.exit(-ret)
