"""Attempt to manage the disaster that is IEM symlinking"""

from pathlib import Path

from pyiem.util import logger

LOG = logger()

# LINK , TARGET
M2 = "/mnt/mesonet2"
PAIRS = [
    ["/mesonet/data/merra2", f"{M2}/data/merra2"],
    ["/mesonet/nawips", f"{M2}/gempak"],
    ["/mesonet/wepp", f"{M2}/idep"],
    ["/mesonet/ARCHIVE/gempak", f"{M2}/longterm/gempak"],
    ["/mesonet/ARCHIVE/raw", f"{M2}/longterm/raw"],
    ["/mesonet/ARCHIVE/rer", f"{M2}/ARCHIVE/rer"],
    ["/mesonet/data/conus404", f"{M2}/data/conus404"],
    ["/mesonet/data/dotcams", f"{M2}/data/dotcams"],
    ["/mesonet/data/era5", f"{M2}/data/era5"],
    ["/mesonet/data/era5_china", f"{M2}/data/era5_china"],
    ["/mesonet/data/era5_europe", f"{M2}/data/era5_europe"],
    ["/mesonet/data/era5_sa", f"{M2}/data/era5_sa"],
    ["/mesonet/data/gempak", f"{M2}/data/gempak"],
    ["/mesonet/data/iemre", f"{M2}/data/iemre"],
    ["/mesonet/data/iemre_china", f"{M2}/data/iemre_china"],
    ["/mesonet/data/iemre_europe", f"{M2}/data/iemre_europe"],
    ["/mesonet/data/iemre_sa", f"{M2}/data/iemre_sa"],
    ["/mesonet/data/prism", f"{M2}/data/prism"],
    ["/mesonet/data/incoming", f"{M2}/data/incoming"],
    ["/mesonet/data/madis", f"{M2}/data/madis"],
    ["/mesonet/data/model", f"{M2}/data/model"],
    ["/mesonet/data/mrms", f"{M2}/data/mrms"],
    ["/mesonet/data/ndfd", f"{M2}/data/ndfd"],
    ["/mesonet/data/nldas", f"{M2}/data/nldas"],
    ["/mesonet/data/smos", f"{M2}/data/smos"],
    ["/mesonet/data/stage4", f"{M2}/data/stage4"],
    ["/mesonet/data/text", f"{M2}/data/text"],
    ["/mesonet/ldmdata", f"{M2}/ldmdata"],  # May fail if node writes data
    ["/mesonet/share/cases", f"{M2}/share/cases"],
    ["/mesonet/share/climodat", f"{M2}/share/climodat"],
    ["/mesonet/share/features", f"{M2}/share/features"],
    ["/mesonet/share/iemmaps", f"{M2}/share/iemmaps"],
    ["/mesonet/share/lapses", f"{M2}/share/lapses"],
    ["/mesonet/share/pickup", f"{M2}/share/pickup"],
    ["/mesonet/share/pics", f"{M2}/share/pics"],
    ["/mesonet/share/present", f"{M2}/share/present"],
    ["/mesonet/share/usage", f"{M2}/share/usage"],
    ["/mesonet/share/windrose", f"{M2}/share/windrose"],
    ["/mesonet/home", f"{M2}/home"],
]


def workflow(link: Path, target: Path):
    """Do things"""
    if not target.exists():
        LOG.info("ERROR: link target: %s is not found", target)
        return
    if not link.is_symlink() and link.is_dir():
        LOG.info("ERROR: symlink: %s is already a directory!", link)
        return
    if link.is_symlink():
        oldtarget = link.resolve()
        if oldtarget == target:
            return
        link.unlink()
    LOG.info("%s -> %s", link, target)
    link.symlink_to(target)


def main():
    """Go Main"""
    # Ensure some base folders exist
    for mysubdir in ["share", "ARCHIVE", "data"]:
        path = Path("/mesonet") / Path(mysubdir)
        if not path.is_dir():
            path.mkdir(parents=True)
    # Quasi dynamic generation of /mesonet/ARCHIVE/data/YYYY links
    if not Path("/mesonet/ARCHIVE/data").is_dir():
        Path("/mesonet/ARCHIVE/data").mkdir(parents=True)
    for year in range(1893, 2015):
        link = Path("/mesonet/ARCHIVE/data") / str(year)
        target = Path(f"/mnt/archive32/ARCHIVE/data/{year}")
        workflow(link, target)
    for year in range(2015, 2019):
        link = Path("/mesonet/ARCHIVE/data") / str(year)
        target = Path(f"/mnt/archive5/ARCHIVE/data/{year}")
        workflow(link, target)
    for year in range(2019, 2024):
        link = Path("/mesonet/ARCHIVE/data") / str(year)
        target = Path(f"/mnt/archive32/ARCHIVE/data/{year}")
        workflow(link, target)
    for year in range(2024, 2025):
        link = Path("/mesonet/ARCHIVE/data") / str(year)
        target = Path(f"/mnt/archive5/ARCHIVE/data/{year}")
        workflow(link, target)
    for year in range(2025, 2026):
        link = Path("/mesonet/ARCHIVE/data") / str(year)
        target = Path(f"/mnt/archive32/ARCHIVE/data/{year}")
        workflow(link, target)
    for year in range(2026, 2028):
        link = Path("/mesonet/ARCHIVE/data") / str(year)
        target = Path(f"/mnt/archive33/ARCHIVE/data/{year}")
        workflow(link, target)

    for link, target in PAIRS:
        workflow(Path(link), Path(target))


if __name__ == "__main__":
    main()
