"""Setup some baseline archive folders / files to prevent 404s

 - RIDGE folders
 - NOAAport archive files

Since we have web scrapers, we need to have empty folders to keep the Server
from having lots of 404s
"""
import os
import sys
import grp
import subprocess

from pyiem.network import Table as NetworkTable
from pyiem.util import utc

PRODS = {
    "NEXRAD": ["N0B", "N0S", "N0U", "N0Z", "NET"],
    "TWDR": ["NET", "TZL", "TV0"],
}
PILS = (
    "LSR|FWW|CFW|TCV|RFW|FFA|SVR|TOR|SVS|SMW|MWS|NPW|WCN|WSW|EWW|FLS"
    "|FLW|SPS|SEL|SWO|FFW|DSW|SQW"
).split("|")


def chgrp(filepath, gid):
    """https://gist.github.com/jmahmood/2505741"""
    uid = os.stat(filepath).st_uid
    os.chown(filepath, uid, gid)


def supermakedirs(path, mode, group):
    """ "http://stackoverflow.com/questions/5231901"""
    if not path or os.path.exists(path):
        return []
    (head, _) = os.path.split(path)
    res = supermakedirs(head, mode, group)
    os.mkdir(path)
    os.chmod(path, mode)
    chgrp(path, group)
    res += [path]
    return res


def main(argv):
    """Go Main Go"""
    if len(argv) == 1:
        ts = utc()
    else:
        ts = utc(int(argv[1]), int(argv[2]), int(argv[3]))
    nt = NetworkTable(["NEXRAD", "TWDR"])
    basedir = ts.strftime("/mesonet/ARCHIVE/data/%Y/%m/%d/GIS/ridge")
    for sid in nt.sts:
        for prod in PRODS[nt.sts[sid]["network"]]:
            mydir = os.path.join(basedir, sid, prod)
            if os.path.isdir(mydir):
                continue
            supermakedirs(mydir, 0o775, grp.getgrnam("iem-friends")[2])
    # Do noaaport text
    basedir = ts.strftime("/mesonet/ARCHIVE/data/%Y/%m/%d/text/noaaport")
    supermakedirs(basedir, 0o775, grp.getgrnam("iem-friends")[2])
    os.chdir(basedir)
    for pil in PILS:
        fn = f"{pil}_{ts:%Y%m%d}.txt"
        if not os.path.isfile(fn):
            subprocess.call(f"touch {fn}", shell=True)
            os.chmod(fn, 0o664)
            chgrp(fn, grp.getgrnam("iem-friends")[2])


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

"""Setup some baseline archive folders / files to prevent 404s

 - RIDGE folders
 - NOAAport archive files

Since we have web scrapers, we need to have empty folders to keep the Server
from having lots of 404s
"""
import os
import sys
import grp
import subprocess

from pyiem.network import Table as NetworkTable
from pyiem.util import utc

PRODS = {
    "NEXRAD": ["N0B", "N0S"],
    "TWDR": ["TZL", "TV0"],
}
PILS = (
    "LSR|FWW|CFW|TCV|RFW|FFA|SVR|TOR|SVS|SMW|MWS|NPW|WCN|WSW|EWW|FLS"
    "|FLW|SPS|SEL|SWO|FFW|DSW|SQW"
).split("|")


def chgrp(filepath, gid):
    """https://gist.github.com/jmahmood/2505741"""
    uid = os.stat(filepath).st_uid
    os.chown(filepath, uid, gid)


def supermakedirs(path, mode, group):
    """ "http://stackoverflow.com/questions/5231901"""
    if not path or os.path.exists(path):
        return []
    (head, _) = os.path.split(path)
    res = supermakedirs(head, mode, group)
    os.mkdir(path)
    os.chmod(path, mode)
    chgrp(path, group)
    res += [path]
    return res


def main(argv):
    """Go Main Go"""
    if len(argv) == 1:
        ts = utc()
    else:
        ts = utc(int(argv[1]), int(argv[2]), int(argv[3]))
    nt = NetworkTable(["NEXRAD", "TWDR"])
    basedir = ts.strftime("/mesonet/ARCHIVE/data/%Y/%m/%d/GIS/ridge")
    for sid in nt.sts:
        for prod in PRODS[nt.sts[sid]["network"]]:
            mydir = os.path.join(basedir, sid, prod)
            if os.path.isdir(mydir):
                continue
            supermakedirs(mydir, 0o775, grp.getgrnam("iem-friends")[2])
    # Do noaaport text
    basedir = ts.strftime("/mesonet/ARCHIVE/data/%Y/%m/%d/text/noaaport")
    supermakedirs(basedir, 0o775, grp.getgrnam("iem-friends")[2])
    os.chdir(basedir)
    for pil in PILS:
        fn = f"{pil}_{ts:%Y%m%d}.txt"
        if not os.path.isfile(fn):
            subprocess.call(f"touch {fn}", shell=True)
            os.chmod(fn, 0o664)
            chgrp(fn, grp.getgrnam("iem-friends")[2])


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

correct NEXRAD baseline for archive