#!/usr/bin/python3

from nbt.region import RegionFile

import os
import os.path
import sys


def fatal(*messages):
    print("{}".format(' '.join([str(m) for m in messages])), file=sys.stderr)
    sys.exit(1)


scriptpath = sys.argv.pop(0)

# Default argument values
region_file = None
cutoff_ticks = 1200 # 60 seconds (20 ticks per second * 60)
dry_run = False
path = os.getcwd()

# Get arguments
for a in sys.argv:
    i = a.index('=')
    k = a[0 : i]
    v = a[i + 1:]
    if k == 'file':
        region_file = v
    elif k == 'cutoff':
        cutoff_ticks = int(v)
    elif k == 'dryrun':
        if v in ['true', 'false']:
            dry_run = (v == 'true')
        else:
            fatal("No such dryrun value: '{}'".format(v))
    elif k == 'path':
        if os.path.exists(v):
            path = v
        else:
            fatal("No such path: {}".format(v))
    else:
        fatal("No such argument: '{}'".format(k))

# Get list of files to trim
files = []
if region_file is not None:
    files = [region_file]
else:
    files = os.listdir(path=path)

# Delete all chunks in files less inhabited than the cutoff number of ticks
for file in files:
    filepath = os.path.join(path, file)
    if not os.path.exists(filepath):
        continue
    region = RegionFile(filepath)
    chunks = region.get_chunks()
    for chunk in chunks:
        x = chunk['x']
        z = chunk['z']
        try:
            chunk_nbt = region.get_chunk(x, z)
        except Exception as e:
            print("Error with file {} chunk {},{}".format(file, x, z), file=sys.stderr)
            raise e
        inhabited_time = chunk_nbt['Level']['InhabitedTime'].value
        if inhabited_time < cutoff_ticks:
            print("{},{},{},{}".format(file, x, z, inhabited_time))
            if not dry_run:
                region.unlink_chunk(x, z)

    # If there are no chunks left in file, delete it
    if len(region.get_chunks()) == 0:
        print("Deleting file {}".format(file))
        if not dry_run:
            os.remove(filepath)
