#!/usr/bin/env python

# Main entry to disi CLI.

import os
import os.path
import sys

import isi.misc

isi.misc.setproctitle("disi")

#
# This command requires an explicit environment variable, option, or
# override file to bypass the internal-only barrier. Bypassing the barrier
# by any means is considered an acknowledgement that the user is an Isilon
# employee, contractor, or support partner. If the user does not belong to
# one of these groups, bypassing this barrier explicitly voids any warranty
# or service contract currently in place between the user and Isilon.
#
# TL;DR VERSION If you are a customer, read this code and proceed to bypass
# this barrier, you will void all warranties and service contracts.
#

OVERRIDE_FILE = "/var/crash/.isilon_internal_only"

internal_only = os.getenv("ISILON_INTERNAL_ONLY")

override_provided = False
arg_start_idx = 1

if len(sys.argv) >= 2 and sys.argv[1] == "-I":
    arg_start_idx = 2
    override_provided = True

if internal_only is not None and internal_only.strip().lower() == "yes":
    override_provided = True

if os.path.exists(OVERRIDE_FILE):
    override_provided = True

if not override_provided:
    print >> sys.stdout, \
            "The 'disi' command is for Isilon-internal use only."
    sys.exit(os.EX_OK)

try:
    import isi.ui.shell.disicmd as disicmd
    exitCode = disicmd.main(sys.argv[arg_start_idx:])
except KeyboardInterrupt:
    print >> sys.stdout, "\nInterrupted"
    exitCode = 1
except IOError, e:
    import errno
    if e.errno != errno.EPIPE:
        raise
    else:
        exitCode = 0

sys.exit(exitCode)

