#!/usr/bin/env python

# Copyright C 2009 by Isilon Systems, Inc. All rights not expressly
# granted herein are reserved.
# Created in the United States of America.
#
# This digital media is protected by U.S. and international copyright and
# intellectual property laws. Unless otherwise specified, all information
# and screens appearing as part of this digital medium, including
# software, services, documents, text, images, icons, and logos design;
# the selection, assembly, arrangement, and design thereof; and the code
# that enables its presentation, are the sole property of Isilon Systems,
# Inc.
#
# Except as otherwise required by applicable law, no part of any
# copyrighted material may be reproduced, modified, translated,
# distributed, published, or transmitted by any means, electronic or
# mechanical, for any purpose, without the prior written consent of Isilon
# Systems.
#
# Isilon and Isilon Systems are registered trademarks of Isilon Systems,
# Inc.  Legion is a trademark of Isilon Systems, Inc.  All other
# trademarks and registered trademarks are the property of their
# respective holders.

"""
 $Id: isi_smb_check 246479 2012-05-30 21:24:02Z jryan $
"""

import getopt
import os
import sys
import isi.upgrade.preupgradechecks.smbcheck as smbcheck

#  __  __    _    ___ _   _ 
# |  \/  |  / \  |_ _| \ | |
# | |\/| | / _ \  | ||  \| |
# | |  | |/ ___ \ | || |\  |
# |_|  |_/_/   \_\___|_| \_|

def _usage():
    me = sys.argv[0].split('/')[-1]

    s = \
"""
Usage: %s [--path=<override path>]

  --path: Set override path (default /etc/mcp/override)

%s verifies SMB overrides, displaying any issues encountered. Issues
are classified as follows:

  error: Parameter is unsupported and may cause active harm. In
  future versions, the parameter will have no affect.

  deprecated: Parameter works in the version found, but support
  for it will be removed in a future version.

  warning: Parameter is useless.

Version: %s.
""" % (me, me, smbcheck.get_version())

    print s[1:],

def _exists(f):
    try:
        os.stat(f)
        return True
    except Exception, e:
        return False

def main():
    try:
        opts, args = getopt.getopt(sys.argv[1:], "h",
            ["help", "path="])
    except getopt.GetoptError, err:
        print str(err)
        _usage()
        sys.exit(1)

    path = smbcheck.OVERRIDE_PATH

    for o, a in opts:
        if o in ["-h", "--help"]:
            _usage()
            sys.exit(0)
        elif o in ["--path"]:
            path = a
        else:
            assert False, "Unhandled parameter"

    if not _exists(path):
        print "error: path '%s' not found" % path
        sys.exit(1)

    smbd_xml = path + "/" + smbcheck.SMBD_XML
    if not _exists(smbd_xml):
        print "error: '%s' not found - " % smbd_xml,
        print "is '%s' the right place to find overrides?" % path,
        print "(pre-4.7 clusters may not have a %s)" % smbcheck.SMBD_XML
        sys.exit(1)

    db = {}
    try:
        smbcheck.parse_global_xml(db, smbd_xml)
    except Exception, e:
        print "error: failed to parse '%s': %s" % (smbd_xml, e)
        sys.exit(1)

    smbd_shares_xml = path + "/" + smbcheck.SMBD_SHARES_XML
    if _exists(smbd_shares_xml):
        smbcheck.parse_share_xml(db, smbd_shares_xml)

    issues = smbcheck.supported_config(db)

    smbcheck.print_issues(issues)
    if len(issues):
        print ""
    print "Found %s issues (override v%s, verification version %s)" % \
          (len(issues), smbcheck.version_to_string(db['_version']),
           smbcheck.get_version())

if __name__ == '__main__':
    main()
