#!/usr/bin/env python
#
# Copyright (c) 2017-2022 Dell Inc. or its subsidiaries. All Rights Reserved.
#
import sys

import isi.misc as misc

# The layout of these masks and releases is documented here:
# https://wiki.west.isilon.com/index.php/OneFS_Version_Integer
MJ_MN_SB_MASK = 0xFFFFFF0000000000
ACELA_RELEASE = 0x0802010000000000


# UDSPS-385 - DSP 1.41.1 fails on OneFS 8.2.1

BLOCK_DSP_INSTALL = """
    Please NOTE:

    DSP versions 1.41.1 and later:
    - Are incompatible with OneFS versions earlier than 8.2.1
    - Are only compatible with OneFS version 8.2.1 if
      a workaround procedure has been applied first

    The workaround for installing this DSP on OneFS 8.2.1
    is available at:
        https://www.dell.com/support/kbdoc/000203212
"""

ONEFS_EARLIER_THAN_ACELA = """
    Also NOTE:

    The installed version of OneFS is earlier than
    OneFS 8.2.1.

    This version of OneFS will not install this DSP.
"""

ONEFS_ACELA_WITHOUT_WORKAROUND = """
    Also NOTE:

    The installed OneFS version is 8.2.1.

    This version of OneFS will install this DSP, but
    only with the above workaround.
"""


def main():
    """
    To block DSP install, return: a string
    To allow DSP install, return: 0
    """
    exitval = 0
    osrev = misc.osrevision() & MJ_MN_SB_MASK

    if osrev < ACELA_RELEASE:
        exitval = BLOCK_DSP_INSTALL + ONEFS_EARLIER_THAN_ACELA
    elif osrev == ACELA_RELEASE:
        try:
            # On Acela, the workaround makes it so this import succeeds
            # (and so DSP install succeeds):
            from builtins import map  # noqa: F401
        except ImportError:
            exitval = BLOCK_DSP_INSTALL + ONEFS_ACELA_WITHOUT_WORKAROUND
    return exitval


if __name__ == "__main__":
    sys.exit(main())
