#  Copyright (C) 2023 The authors of Py-ChemShell
#
#  This file is part of Py-ChemShell.
#
#  Py-ChemShell is free software: you can redistribute it and/or modify
#  it under the terms of the GNU Lesser General Public License as
#  published by the Free Software Foundation, either version 3 of the
#  License, or (at your option) any later version.
#
#  Py-ChemShell is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU Lesser General Public License for more details.
#
#  You should have received a copy of the GNU Lesser General Public
#  License along with Py-ChemShell.  If not, see
#  <http://www.gnu.org/licenses/>.

# you.lu@ukri.stfc.org
# Jan 2023


def hack():
    '''To comment out enable_testing() which performs testing at the configuration stage'''

    from os     import path
    from shutil import copy
    
#    CHEMSH_ARCH = '@CHEMSH_ARCH@'
#    
#    CWD = path.abspath(path.join(path.dirname(__file__)))
#    LSDalton_SRC_DIR = path.join(CWD, CHEMSH_ARCH, 'src', 'cmake', 'custom')
    FILENAME = path.join('@LSDALTON_CMAKE_DIR@', 'testing.cmake')
    
    if path.isfile(FILENAME):
        BAK = FILENAME+'.BAK'
        if not path.isfile(BAK):
            with open(FILENAME, 'r') as fp:
                lines = fp.readlines()
            # backup file
            copy(FILENAME, BAK)
        else:
            with open(BAK, 'r') as fp:
                lines = fp.readlines()
    
        # replace string
        for i, line in enumerate(lines):
            if 'enable_testing' in line:
                lines[i] = line.replace('enable_testing', '#enable_testing')
    
        # write out a new file
        with open(FILENAME, 'w') as fp:
            for line in lines:
                fp.write(line)
    
        print(f"ChemShell has hacked LSDalton's {FILENAME}")

    else:

        print(f"\n{FILENAME} not found!\n")


if __name__ == '__main__':

    hack()

