#!/usr/bin/env python3
# This file is part of GUFI, which is part of MarFS, which is released
# under the BSD license.
#
#
# Copyright (c) 2017, Los Alamos National Security (LANS), LLC
# 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.
#
# 3. Neither the name of the copyright holder nor the names of its contributors
# may be used to endorse or promote products derived from this software without
# specific prior written permission.
#
# 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 HOLDER 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.
#
#
# From Los Alamos National Security, LLC:
# LA-CC-15-039
#
# Copyright (c) 2017, Los Alamos National Security, LLC All rights reserved.
# Copyright 2017. Los Alamos National Security, LLC. This software was produced
# under U.S. Government contract DE-AC52-06NA25396 for Los Alamos National
# Laboratory (LANL), which is operated by Los Alamos National Security, LLC for
# the U.S. Department of Energy. The U.S. Government has rights to use,
# reproduce, and distribute this software.  NEITHER THE GOVERNMENT NOR LOS
# ALAMOS NATIONAL SECURITY, LLC MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR
# ASSUMES ANY LIABILITY FOR THE USE OF THIS SOFTWARE.  If software is
# modified to produce derivative works, such modified software should be
# clearly marked, so as not to confuse it with the version available from
# LANL.
#
# THIS SOFTWARE IS PROVIDED BY LOS ALAMOS NATIONAL SECURITY, LLC 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 LOS ALAMOS NATIONAL SECURITY, LLC 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.



import argparse
import math
import os
import subprocess
import sys
import time

sys.path.append('/opt/beegfs/python/index')

import bee_common
import bee_config
import find
import ls
import stats
import bee_icreate
import bee_update
import bee_db_upgrade
import bee_stat
import query_index
import bee_index

# hiding traceback
sys.tracebacklimit=0

# location of this file
PATH = os.path.realpath(__file__)

SECONDS_PER_DAY = str(24 * 60 * 60)

def bee_help():
    print ('Usage: bee [ls] or bee [find] or bee [stats] or bee [create-index] or bee [update] or bee [db-upgrade] or bee [index] or bee [stat] or bee [query-index]')

# argv[0] should be the command name
def run(argv, config_path):

    # storing name of test config file
    test_config_path ="config.test"

    if len(argv) <= 1:
        bee_help()
        exit(1)

    # find and parse the configuration file first
    if(config_path == bee_config.DEFAULT_PATH or config_path == test_config_path):
        config = bee_config.Server(config_path)
    else:
        config = bee_config.envServer(bee_config.DEFAULT_PATH)

    if argv[1] not in ['find', 'ls', 'stats', 'create-index', 'update', 'db-upgrade', 'index', 'stat', 'query-index']:
        bee_help()
        exit(1)

    if argv[1] == 'find':
        return find.exec_find(argv, config_path)
    elif argv[1] == 'ls':
        return ls.exec_ls(argv, config_path)
    elif argv[1] == 'stats':
        return stats.exec_stats(argv, config_path)
    elif argv[1] == 'create-index':
        return bee_icreate.exec_create(argv, config_path)
    elif argv[1] == 'update':
        return bee_update.exec_update(argv, config_path)
    elif argv[1] == 'db-upgrade':
        return bee_db_upgrade.exec_db_upgrade(argv, config_path)
    elif argv[1] == 'index':
        return bee_index.exec_index(argv, config_path)
    elif argv[1] == 'stat':
        return bee_stat.exec_stat(argv, config_path)
    elif argv[1] == 'query-index':
        return query_index.exec_query_index(argv, config_path)

if __name__=='__main__':
    output_buffer = bee_config.os.getenv('OPBuffSize', 4096)
    index_paths = bee_config.os.getenv('IndexPaths', bee_config.DEFAULT_PATH)
    sys.exit(run(sys.argv, index_paths))
