#!/usr/bin/env python3
"""Try to change from GCC11.2 to GCC11.3

For it, you need

- Python3
"""

import argparse


releases = {
    'ABINIT':                   ['9.6.2',           '9.6.2'],
    'ACTC':                     ['1.1',             '1.1'],
    'ADIOS2':                   ['2.7.1',           '2.7.1'],
    'Advisor':                  ['2021.4.0',        '2021.4.0'],
    'AMBER':                    ['20',              '20'],
    'ant-java':                 ['1.10.12',         '1.10.12'],
    'ANTLR':                    ['2.7.7',           '2.7.7'],
    'AOCC':                     ['3.1.0',           '3.1.0'],
    'AOCC':                     ['3.2.0',           '3.2.0'],
    'Apptainer-Tools':          ['2022',            '2022'],
    'APR':                      ['1.7.0',           '1.7.0'],
    'APR-util':                 ['1.6.1',           '1.6.1'],
    'archspec':                 ['0.1.3',           '0.1.3'],
    'ARMForge':                 ['21.1.2',          '21.1.2'],
    'ARPACK-NG':                ['3.8.0',           '3.8.0'],
    'ASE':                      ['3.22.0',          '3.22.0'],
    'ATK':                      ['2.36.0',          '2.36.0'],
    'AT-SPI2-ATK':              ['2.38.0',          '2.38.0'],
    'AT-SPI2-core':             ['2.40.3',          '2.40.3'],
    'Autoconf':                 ['2.71',            '2.71'],
    'Automake':                 ['1.16.4',          '1.16.5'],
    'Autotools':                ['20210726',        '20220317'],
    'archspec':                 ['0.1.3',           '0.1.4'],
    'Bazel':                    ['3.7.2',           '3.7.2'],
    'BeautifulSoup':            ['4.10.0',          '4.10.0'],
    'binutils':                 ['2.37',            '2.38'],
    'Biopython':                ['1.79',            '1.79'],
    'Bison':                    ['3.6.4',           '3.7.6'],
    'Blender':                  ['3.1.2',           '3.1.2'],
    'BLIS':                     ['0.8.1',           '0.9.0'],
    'Blosc':                    ['1.21.1',          '1.21.1'],
    'bokeh':                    ['2.4.1',           '2.4.1'],
    'Boost':                    ['1.78.0',          '1.78.0'],
    'Boost.Python':             ['1.78.0',          '1.78.0'],
    'Brotli':                   ['1.0.9',           '1.0.9'],
    'bzip2':                    ['1.0.8',           '1.0.8'],
    'cairo':                    ['1.16.0',          '1.16.0'],
    'Cartopy':                  ['0.20.0',          '0.20.0'],
    'ccache':                   ['4.3',             '4.3'],
    'CDO':                      ['2.0.2',           '2.0.2'],
    'ceres-solver':             ['2.0.0',           '2.0.0'],
    'CFITSIO':                  ['4.0.0',           '4.0.0'],
    'CGAL':                     ['5.2',             '5.2'],
    'CGNS':                     ['4.2.0',           '4.2.0'],
    'Cirq':                     ['0.13.1',          '0.14.1'],
    'Cirq':                     ['0.14.1',          '0.14.1'],
    'Clang':                    ['13.0.1',          '13.0.1'],
    'Cling':                    ['0.9',             '0.9'],
    'CMake':                    ['3.23.1',          '3.23.1'],
    'Code_Saturne':             ['7.0.2',           '7.0.2'],
    'Colmap':                   ['3.7',             '3.7'],
    'CoordgenLibs':             ['3.0.1',           '3.0.1'],
    'Coreutils':                ['9.0',             '9.0'],
    'CP2K':                     ['9.1.0',           '9.1.0'],
    'CPMD':                     ['4.3',             '4.3'],
    'cppcheck':                 ['2.6',             '2.6'],
    'cppy':                     ['1.1.0',           '1.2.1'],
    'CUDA':                     ['11.5',            '11.7.0'],
    'cuDNN':                    ['8.3.1.22',        '8.6.0.163'],
    'cURL':                     ['7.78.0',          '7.78.0'],
    'cuTENSOR':                 ['1.4.0.6',         '1.4.0.6'],
    'CVS':                      ['1.11.23',         '1.11.23'],
    'CVXOPT':                   ['1.2.7',           '1.2.7'],
    'darshan-runtime':          ['3.4.0',           '3.4.0'],
    'darshan-util':             ['3.4.0',           '3.4.0'],
    'dask':                     ['2021.9.1',        '2021.9.1'],
    'DB':                       ['18.1.40',         '18.1.40'],
    'DBus':                     ['1.13.18',         '1.14.0'],
    'distributed':              ['2.30.1',          '2.30.1'],
    'double-conversion':        ['3.1.6',           '3.1.6'],
    'Doxygen':                  ['1.9.1',           '1.9.1'],
    'DWave':                    ['4.2.0',           '4.2.0'],
    'ecCodes':                  ['2.22.1',          '2.22.1'],
    'Eigen':                    ['3.3.9',           '3.4.0'],
    'elfutils':                 ['0.185',           '0.185'],
    'ELPA':                     ['2021.11.001',     '2021.11.001'],
    'Emacs':                    ['27.1',            '27.1'],
    'Embree':                   ['3.13.2',          '3.13.2'],
    'ESMF':                     ['8.2.0',           '8.2.0'],
    'eudev':                    ['3.2.9',           '3.2.9'],
    'Exiv2':                    ['0.27.5',          '0.27.5'],
    'expat':                    ['2.4.1',           '2.4.1'],
    'expecttest':               ['0.1.3',           '0.1.3'],
    'Extrae':                   ['3.8.3',           '3.8.3'],
    'FFmpeg':                   ['4.4.1',           '4.4.2'],
    'FFTW':                     ['3.3.10',          '3.3.10'],
    'Fiona':                    ['1.8.20',          '1.8.20'],
    'Flask':                    ['2.0.2',           '2.0.2'],
    'flatbuffers':              ['2.0.0',           '2.0.0'],
    'flatbuffers-python':       ['2.0',             '2.0'],
    'flex':                     ['2.6.4',           '2.6.4'],
    'FLINT':                    ['2.7.1',           '2.7.1'],
    'FLTK':                     ['1.3.7',           '1.3.7'],
    'fmt':                      ['8.1.1',           '8.1.1'],
    'fontconfig':               ['2.13.94',         '2.13.94'],
    'freeglut':                 ['3.2.1',           '3.2.1'],
    'FreeImage':                ['3.18.0',          '3.18.0'],
    'FreeSurfer':               ['7.1.1',           '7.1.1'],
    'freetype':                 ['2.11.0',          '2.11.0'],
    'FriBidi':                  ['1.0.10',          '1.0.12'],
    'g2clib':                   ['1.6.3',           '1.6.3'],
    'g2lib':                    ['3.2.0',           '3.2.0'],
    'gc':                       ['8.2.0',           '8.2.0'],
    'GCC':                      ['11.2.0',          '11.3.0'],
    'GDAL':                     ['3.3.2',           '3.3.2'],
    'GDB':                      ['11.1',            '11.1'],
    'Gdk-Pixbuf':               ['2.42.6',          '2.42.6'],
    'GEOS':                     ['3.9.1',           '3.9.1'],
    'gettext':                  ['0.21',            '0.21'],
    'gflags':                   ['2.2.2',           '2.2.2'],
    'Ghostscript':              ['9.54.0',          '9.54.0'],
    'giflib':                   ['5.2.1',           '5.2.1'],
    'git':                      ['2.33.1',          '2.33.1'],
    'git-lfs':                  ['2.11.0',          '2.11.0'],
    'GL2PS':                    ['1.4.2',           '1.4.2'],
    'GLib':                     ['2.69.1',          '2.69.1'],
    'GLM':                      ['0.9.9.8',         '0.9.9.8'],
    'glog':                     ['0.5.0',           '0.5.0'],
    'GLPK':                     ['5.0',             '5.0'],
    'GMP':                      ['6.2.1',           '6.2.1'],
    'gmpy2':                    ['2.1.0b5',         '2.1.0b5'],
    'gnuplot':                  ['5.4.2',           '5.4.2'],
    'GObject-Introspection':    ['1.68-0',          '1.68-0'],
    'Go':                       ['1.17.3',          '1.17.3'],
    'gomkl':                    ['2021b',           '2021b'],
    'gompi':                    ['2021b',           '2021b'],
    'GPAW':                     ['21.6.0',          '21.6.0'],
    'GPAW-setups':              ['0.9.9672',        '0.9.9672'],
    'gperf':                    ['3.1',             '3.1'],
    'GPicView':                 ['0.2.5',           '0.2.5'],
    'gpsmkl':                   ['2021b',           '2021b'],
    'gpsmpi':                   ['2021b',           '2021b'],
    'Grace':                    ['5.1.25',          '5.1.25'],
    'GraphicsMagick':           ['1.3.36',          '1.3.36'],
    'Graphviz':                 ['2.49.3',          '2.49.3'],
    'groff':                    ['1.22.4',          '1.22.4'],
    'GROMACS':                  ['2021.4',          '2021.4'],
    'gsettings-desktop-schemas': ['3.34.0',          '3.34.0'],
    'GSL':                      ['2.7',             '2.7'],
    'GStreamer':                ['1.18.6',          '1.18.6'],
    'GTK+':                     ['3.24.23',         '3.24.23'],
    'GtkSourceView':            ['4.4.0',           '4.4.0'],
    'GTS':                      ['0.7.6',           '0.7.6'],
    'guile':                    ['2.0.14',          '2.0.14'],
    'guile':                    ['3.0.7',           '3.0.7'],
    'gzip':                     ['1.10',            '1.12'],
    'h5py':                     ['3.5.0',           '3.5.0'],
    'HarfBuzz':                 ['2.8.2',           '2.8.2'],
    'Harminv':                  ['1.4.1',           '1.4.1'],
    'HDF5':                     ['1.12.1',          '1.12.1'],
    'HDF':                      ['4.2.15',          '4.2.15'],
    'HDFView':                  ['3.1.3',           '3.1.3'],
    'help2man':                 ['1.48.3',          '1.48.3'],
    'Horovod':                  ['0.24.3',          '0.24.3'],
    'Horovod':                  ['0.25.0',          '0.25.0'],
    'HTSlib':                   ['1.14',            '1.14'],
    'hwloc':                    ['2.5.0',           '2.5.0'],
    'hypothesis':               ['6.14.6',          '6.46.7'],
    'Hypre':                    ['2.23.0',          '2.23.0'],
    'Hypre':                    ['2.25.0',          '2.25.0'],
    'ICU':                      ['70.1',            '70.1'],
    'iimpi':                    ['2021b',           '2021b'],
    'ImageMagick':              ['7.1.0-13',        '7.1.0-13'],
    'imkl':                     ['2021.4.0',        '2021.4.0'],
    'impi':                     ['2021.4.0',        '2021.4.0'],
    'Inspector':                ['2022.0.0',        '2022.0.0'],
    'intel-compilers':          ['2021.4.0',        '2021.4.0'],
    'intel':                    ['2021b',           '2021b'],
    'intel-para':               ['2021b',           '2021b'],
    'intltool':                 ['0.51.0',          '0.51.0'],
    'iomkl':                    ['2021b',           '2021b'],
    'iompi':                    ['2021b',           '2021b'],
    'ipp':                      ['2021.5.1',        '2021.5.1'],
    'ipsmpi':                   ['2021b',           '2021b'],
    'IRkernel':                 ['1.3',             '1.3'],
    'ISL':                      ['0.24',            '0.24'],
    'ispc':                     ['1.16.1',          '1.16.1'],
    'itac':                     ['2021.5.0',        '2021.5.0'],
    'ITK':                      ['5.2.1',           '5.2.1'],
    'ITSTool':                  ['2.0.7',           '2.0.7'],
    'Java':                     ['15',              '15'],
    'jax':                      ['0.3.14',          '0.3.14'],
    'jbigkit':                  ['2.1',             '2.1'],
    'jemalloc':                 ['5.2.1',           '5.2.1'],
    'JsonCpp':                  ['1.9.4',           '1.9.4'],
    'Julia':                    ['1.7.1',           '1.7.1'],
    'kim-api':                  ['2.2.1',           '2.2.1'],
    'LAME':                     ['3.100',           '3.100'],
    'LAMMPS':                   ['7Jan2022',        '7Jan2022'],
    'LCov':                     ['1.15',            '1.15'],
    'libaec':                   ['1.0.6',           '1.0.6'],
    'libaio':                   ['0.3.112',         '0.3.112'],
    'libarchive':               ['3.5.1',           '3.5.1'],
    'libcerf':                  ['1.17',            '1.17'],
    'libcint':                  ['4.4.0',           '4.4.0'],
    'libcroco':                 ['0.6.13',          '0.6.13'],
    'libctl':                   ['4.5.1',           '4.5.1'],
    'libdap':                   ['3.20.8',          '3.20.8'],
    'libdeflate':               ['1.10',            '1.10'],
    'libdrm':                   ['2.4.108',         '2.4.108'],
    'libdwarf':                 ['20210528',        '20210528'],
    'libelf':                   ['0.8.13',          '0.8.13'],
    'libepoxy':                 ['1.5.9',           '1.5.9'],
    'libevent':                 ['2.1.12',          '2.1.12'],
    'libffi':                   ['3.4.2',           '3.4.2'],
    'libgd':                    ['2.3.1',           '2.3.1'],
    'libGDSII':                 ['0.21',            '0.21'],
    'libgeotiff':               ['1.7.0',           '1.7.0'],
    'libgit2':                  ['1.1.1',           '1.1.1'],
    'libglvnd':                 ['1.3.3',           '1.3.3'],
    'libiconv':                 ['1.16',            '1.16'],
    'Libint':                   ['2.7.0-beta.6',    '2.7.0-beta.6'],
    'libjpeg-turbo':            ['2.1.1',           '2.1.3'],
    'libreadline':              ['8.0',             '8.1'],
    'librsvg':                  ['2.48.8',          '2.51.2'],
    'LibTIFF':                  ['4.3.0',           '4.3.0'],
    'libunwind':                ['1.4,0',           '1.5.0'],
    'libxc':                    ['5.1.5',           '5.1.6'],
    'libxml2':                  ['2.9.10',          '2.9.13'],
    'libyaml':                  ['0.2.5',           '0.2.5'],
    'LAME':                     ['3.100',           '3.100'],
    'LLVM':                     ['10.0.1',          '13.0.0'],
    'lz4':                      ['1.9.3',           '1.9.3'],
    'M4':                       ['1.4.18',          '1.4.19'],
    'magma':                    ['2.6.1',           '2.6.2'],
    'Mako':                     ['1.1.3',           '1.1.4'],
    'matplotlib':               ['3.4.2',           '3.5.2'],
    'Meson':                    ['0.55.0',          '0.58.2'],
    'MPFR':                     ['4.1.0',           '4.1.0'],
    'NASM':                     ['2.15.03',         '2.15.05'],
    'NSS':                      ['3.51',            '3.69'],
    'netCDF':                   ['4.7.4',           '4.8.1'],
    'numactl':                  ['2.0.14',          '2.0.15'],
    'OpenEXR':                  ['2.5.2',           '3.1.1'],
    'OpenGL':                   ['2020',            '2021b'],
    'Pango':                    ['1.44.7',          '1.48.8'],
    'parallel':                 ['20201122',        '20210722'],
    'PCRE2':                    ['10.34',           '10.37'],
    'PCRE':                     ['8.44',            '8.45'],
    'Perl':                     ['5.32.0',          '5.34.0'],
    'pkg-config':               ['0.29.2',          '0.29.2'],
    'pkgconfig':                ['1.5.4',           '1.5.5'],
    'Pillow-SIMD':              ['9.0.1',           '9.2.0'],
    'PROJ':                     ['7.1.0',           '8.1.0'],
    'PLUMED':                   ['2.6.1',           '2.7.2'],
    'protobuf':                 ['3.17.3',          '3.19.4'],
    'protobuf-python':          ['3.17.3',          '3.19.4'],
    'psmpi':                    ['5.4.9-1',         '5.5.0-1'],
    'pybind11':                 ['2.7.1',           '2.9.2'],
    'pyproj':                   ['2.6.1.post1',     '3.3.0'],
    'Python':                   ['3.8.5',           '3.9.6'],
    'PyTorch':                  ['1.8.1',           '1.11'],
    'PyYAML':                   ['5.4.1',           '6.0'],
    'Qhull':                    ['2020.1',          '2020.2'],
    'Qt5':                      ['5.14.2',          '5.15.2'],
    'RDFlib':                   ['5.0.0',           '6.0.2'],
    're2c':                     ['1.3',             '2.2'],
    'Rust':                     ['1.47.0',          '1.54.0'],
    'scikit':                   ['2021',            '2022'],
    'SciPy-Stack':              ['2021',            '2021b'],
    'SciPy-bundle':             ['2021.10',         '22.05'],
    'Shapely':                  ['1.7.1',           '1.8.0'],
    'SuiteSparse':              ['5.7.1',           '5.10.1'],
    'snappy':                   ['1.1.8',           '1.1.9'],
    'SQLite':                   ['3.32.3',          '3.36'],
    'Tcl':                      ['8.6.10',          '8.6.11'],
    'texinfo':                  ['6.7',             '6.8'],
    'Tkinter':                  ['3.9.6',           '3.10.4'],
    'texlive':                  ['20200406',        '20200406'],
    'torchvision':              ['0.9.1',           '0.12.0'],
    'tqdm':                     ['4.62.3',          '4.62.3'],
    'trimesh':                  ['3.8.11',          '3.9.36'],
    'UDUNITS':                  ['2.2.26',          '2.2.28'],
    'uftp':                     ['1.4.1',           '1.4.2'],
    'X11':                      ['20200222',        '20210802'],
    'x264':                     ['20210613',        '20220620'],
    'x265':                     ['3.4',             '3.5'],
    'XServer':                  ['1.20.9',          '1.20.13'],
    'zlib':                     ['1.2.11',          '1.2.12'],
    'zstd':                     ['1.5.0',           '1.5.2'],
}


def parse_arguments():
    """Receives command line input"""
    parser = argparse.ArgumentParser(description='Command line options.')
    parser.add_argument('-i', metavar='INPUT', default="", type=str,
                        help='Input File')
    parser.add_argument('-o', metavar='OLDRELEASE', default="", type=str,
                        help='Current version of given software to be replaced')
    parser.add_argument('-n', metavar='LATESTRELEASE', default="", type=str,
                        help='Most recent version of given software')

    return parser.parse_args()


def updateFileName(fileName, current, latest):
    """Tries to bring filename automatically to the latest toolchain"""
    if current and latest:
        fileName = fileName.replace(current, latest, 1)
    if fileName:
        fileName = fileName.replace(releases['GCC'][0], releases['GCC'][1], 1)
        if 'gcccoremkl' in fileName:
            fileName = fileName.replace(
                releases['imkl'][0], releases['imkl'][1], 1)
        elif 'gomkl' in fileName:
            fileName = fileName.replace(
                releases['gomkl'][0], releases['gomkl'][1], 1)
        elif 'gpsmpi' in fileName:
            fileName = fileName.replace(
                releases['gpsmpi'][0], releases['gpsmpi'][1], 1)
        elif 'gpsmkl' in fileName:
            fileName = fileName.replace(
                releases['gpsmkl'][0], releases['gpsmkl'][1], 1)
        elif 'ipsmpi' in fileName:
            fileName = fileName.replace(
                releases['ipsmpi'][0], releases['ipsmpi'][1], 1)
        elif 'intel-para' in fileName:
            fileName = fileName.replace(
                releases['intel-para'][0], releases['intel-para'][1], 1)
        fileName = fileName.replace(
            '-Python-3.8.5', '', 1)
    return fileName


def updateFile(oldName, newName, current, latest):
    versionSuffixIsPython = False
    """Replaces toolchain, cleans up site_contacts and goes through the list of releases with the new one"""
    if newName is not oldName:
        newFile = open(newName, 'w')
        with open(oldName) as file:
            for line in file:
                if current and latest:
                    if line.startswith('version'):
                        print("Changing package version...")
                        line = line.replace(current, latest, 1)
                if line.startswith('toolchain'):
                    print("Fixing toolchain...")
                    line = line.replace(
                        releases['GCC'][0], releases['GCC'][1], 1)
                    line = line.replace(
                        releases['imkl'][0], releases['imkl'][1], 1)
                    line = line.replace(
                        releases['gomkl'][0], releases['gomkl'][1], 1)
                    line = line.replace(
                        releases['gpsmpi'][0], releases['gpsmpi'][1], 1)
                    line = line.replace(
                        releases['ipsmpi'][0], releases['ipsmpi'][1], 1)
                    line = line.replace(
                        releases['intel-para'][0], releases['intel-para'][1], 1)
                    line = line.replace(
                        releases['gcccoremkl'][0], releases['gcccoremkl'][1], 1)
                if line.startswith('site_contacts'):
                    print("Removing site_contacts")
                    line = ""
                for key in releases:
                    if key in line:
                        print("Changing", key, "from",
                              releases[key][0], "to", releases[key][1])
                        line = line.replace(
                            releases[key][0], releases[key][1], 1)
                if line.startswith("versionsuffix = '-Python"):
                    print("Removing python version suffix")
                    versionSuffixIsPython = True
                    line = ''
                if versionSuffixIsPython:
                    line = line.replace(', versionsuffix', '', 1)
                newFile.write(line)
        newFile.close()
        return True
    else:
        print("Files are the same")
        return False


if __name__ == "__main__":
    args = parse_arguments()
    newFile = updateFileName(args.i, args.o, args.n)
    if updateFile(args.i, newFile, args.o, args.n):
        print("Updated", args.i, ' to GCC',
              releases['GCC'][1], 'and to version', args.n)
        print("Run: \n\neb -l %s" % newFile)