#!/usr/bin/env python

import argparse
import arvados
import daemon
import logging
import os
import signal
import subprocess
import traceback

from arvados_fuse import *

if __name__ == '__main__':
    # Handle command line parameters
    parser = argparse.ArgumentParser(
        description='''Mount Keep data under the local filesystem.  By default, if neither
        --collection or --tags is specified, this mounts as a virtual directory
        under which all Keep collections are available as subdirectories named
        with the Keep locator; however directories will not be visible to 'ls'
        until a program tries to access them.''',
        epilog="""
Note: When using the --exec feature, you must either specify the
mountpoint before --exec, or mark the end of your --exec arguments
with "--".
""")
    parser.add_argument('mountpoint', type=str, help="""Mount point.""")
    parser.add_argument('--allow-other', action='store_true',
                        help="""Let other users read the mount""")
    parser.add_argument('--collection', type=str, help="""Mount only the specified collection at the mount point.""")
    parser.add_argument('--tags', action='store_true', help="""Mount as a virtual directory consisting of subdirectories representing tagged
collections on the server.""")
    parser.add_argument('--groups', action='store_true', help="""Mount as a virtual directory consisting of subdirectories representing groups on the server.""")
    parser.add_argument('--debug', action='store_true', help="""Debug mode""")
    parser.add_argument('--logfile', help="""Write debug logs and errors to the specified file (default stderr).""")
    parser.add_argument('--foreground', action='store_true', help="""Run in foreground (default is to daemonize unless --exec specified)""", default=False)
    parser.add_argument('--exec', type=str, nargs=argparse.REMAINDER,
                        dest="exec_args", metavar=('command', 'args', '...', '--'),
                        help="""Mount, run a command, then unmount and exit""")

    args = parser.parse_args()
    args.mountpoint = os.path.realpath(args.mountpoint)
    if args.logfile:
        args.logfile = os.path.realpath(args.logfile)

    # Daemonize as early as possible, so we don't accidentally close
    # file descriptors we're using.
    if not (args.exec_args or args.foreground):
        os.chdir(args.mountpoint)
        daemon_ctx = daemon.DaemonContext(working_directory='.')
        daemon_ctx.open()
    else:
        daemon_ctx = None

    # Set up logging.
    # If we're daemonized without a logfile, there's nowhere to log, so don't.
    if args.logfile or (daemon_ctx is None):
        log_conf = {}
        if args.debug:
            log_conf['level'] = logging.DEBUG
            arvados.config.settings()['ARVADOS_DEBUG'] = 'true'
        if args.logfile:
            log_conf['filename'] = args.logfile
        logging.basicConfig(**log_conf)
        logging.debug("arv-mount debugging enabled")

    try:
        # Create the request handler
        operations = Operations(os.getuid(), os.getgid())
        api = arvados.api('v1')

        if args.groups:
            e = operations.inodes.add_entry(GroupsDirectory(llfuse.ROOT_INODE, operations.inodes, api))
        elif args.tags:
            e = operations.inodes.add_entry(TagsDirectory(llfuse.ROOT_INODE, operations.inodes, api))
        elif args.collection != None:
            # Set up the request handler with the collection at the root
            e = operations.inodes.add_entry(CollectionDirectory(llfuse.ROOT_INODE, operations.inodes, args.collection))
        else:
            # Set up the request handler with the 'magic directory' at the root
            operations.inodes.add_entry(MagicDirectory(llfuse.ROOT_INODE, operations.inodes))
    except Exception as ex:
        logging.error("arv-mount: exception during API setup")
        logging.error(traceback.format_exc())
        exit(1)

    # FUSE options, see mount.fuse(8)
    opts = [optname for optname in ['allow_other', 'debug']
            if getattr(args, optname)]

    if args.exec_args:
        # Initialize the fuse connection
        llfuse.init(operations, args.mountpoint, opts)

        t = threading.Thread(None, lambda: llfuse.main())
        t.start()

        # wait until the driver is finished initializing
        operations.initlock.wait()

        rc = 255
        try:
            sp = subprocess.Popen(args.exec_args, shell=False)

            # forward signals to the process.
            signal.signal(signal.SIGINT, lambda signum, frame: sp.send_signal(signum))
            signal.signal(signal.SIGTERM, lambda signum, frame: sp.send_signal(signum))
            signal.signal(signal.SIGQUIT, lambda signum, frame: sp.send_signal(signum))

            # wait for process to complete.
            rc = sp.wait()

            # restore default signal handlers.
            signal.signal(signal.SIGINT, signal.SIG_DFL)
            signal.signal(signal.SIGTERM, signal.SIG_DFL)
            signal.signal(signal.SIGQUIT, signal.SIG_DFL)
        except Exception as e:
            logging.error('arv-mount: exception during exec %s' % (args.exec_args,))
            logging.error(traceback.format_exc())
            try:
                rc = e.errno
            except AttributeError:
                pass
        finally:
            subprocess.call(["fusermount", "-u", "-z", args.mountpoint])

        exit(rc)
    else:
        try:
            llfuse.init(operations, args.mountpoint, opts)
            llfuse.main()
        except Exception as e:
            logging.error('arv-mount: exception during mount')
            logging.error(traceback.format_exc())
            exit(getattr(e, 'errno', 1))
