#!/usr/bin/env python

#
#  Program: 3D Slicer
#
#  Copyright (c) Kitware Inc.
#
#  See COPYRIGHT.txt
#  or http://www.slicer.org/copyright/copyright.txt for details.
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
#
#  This file was originally developed by Jean-Christophe Fillion-Robin, Kitware Inc.
#  and was partially funded by NIH grant 1U24CA194354-01
#

import os
import sys
import tempfile

from SlicerAppTesting import *

"""
Usage:
    SlicerOptionIgnoreSlicerRCTest.py /path/to/Slicer
"""

if __name__ == "__main__":
    debug = False
    # Set to True to:
    #  * display the path of the created slicerrc file
    #  * avoid deleting the slicerrc file

    if len(sys.argv) != 2:
        print(os.path.basename(sys.argv[0]) + " /path/to/Slicer")
        exit(EXIT_FAILURE)

    slicer_executable = os.path.expanduser(sys.argv[1])
    common_args = ["--disable-modules", "--no-main-window"]

    # Derive the RC environment variable name from the application executable name.
    # For example: "Slicer" -> "SLICERRC", "MyApp" -> "MYAPPRC"
    app_name = os.path.splitext(os.path.basename(slicer_executable))[0]
    rc_env_var = f"{app_name.upper()}RC"

    # Skip when Slicer_USE_SLICERRC is OFF. The --ignore-slicerrc flag is only
    # registered when the feature is compiled in, so its presence in --help
    # output serves as a reliable probe.
    (_, helpStdout, helpStderr) = runSlicerAndExit(slicer_executable, ["--help"])
    if "--ignore-slicerrc" not in (helpStdout + helpStderr):
        print("Slicer_USE_SLICERRC is OFF: slicerrc tests skipped.")
        exit(EXIT_SUCCESS)

    fd, slicerrc = tempfile.mkstemp()
    assert os.path.isfile(slicerrc)
    try:
        # Create a slicerrc file that creates SLICERRCTESTOUTPUT file when executed
        with os.fdopen(fd, "w") as file:
            file.write("# Generated by SlicerOptionIgnoreSlicerRCTest.py\n" +\
                       "import os\n" +\
                       "fd = os.open(os.environ['SLICERRCTESTOUTPUT'], os.O_RDWR|os.O_CREAT)\n" +\
                       "os.write(fd, 'Generated by SlicerOptionIgnoreSlicerRCTest.py test when slicerrc executed'.encode())\n" +\
                       "os.close(fd)\n")

        slicerrctestoutput = slicerrc + ".out"
        os.environ[rc_env_var] = slicerrc
        os.environ["SLICERRCTESTOUTPUT"] = slicerrctestoutput
        if debug:
            print(f"{rc_env_var}={slicerrc}")
            print("SLICERRCTESTOUTPUT=%s" % slicerrctestoutput)

        # Check that slicerrc file is loaded
        args = list(common_args)
        (returnCode, stdout, stderr) = runSlicerAndExit(slicer_executable, args)
        assert os.path.isfile(slicerrctestoutput)
        os.remove(slicerrctestoutput)
        assert returnCode == EXIT_SUCCESS
        print("=> ok\n")

        # Check that --ignore-slicerrc works
        args = list(common_args)
        args.extend(["--ignore-slicerrc"])
        (returnCode, stdout, stderr) = runSlicerAndExit(slicer_executable, args)
        assert not os.path.isfile(slicerrctestoutput)
        assert returnCode == EXIT_SUCCESS
        print("=> ok\n")

        # Check that --testing implies --ignore-slicerrc
        args = list(common_args)
        args.extend(["--testing"])
        (returnCode, stdout, stderr) = runSlicerAndExit(slicer_executable, args)
        assert not os.path.isfile(slicerrctestoutput)
        assert returnCode == EXIT_SUCCESS
        print("=> ok\n")
    finally:
        if not debug:
            os.remove(slicerrc)
            if os.path.isfile(slicerrctestoutput):
                os.remove(slicerrctestoutput)
