#!/usr/bin/env python3

import os
from os import path
import argparse
import macresources
from macresources import binhex


def do_file(the_path):
    base_path = path.splitext(the_path)[0] # known to have hqx extension
    hb = binhex.HexBin(the_path)

    if hb.FInfo.Type == hb.FInfo.Creator == b'????':
        try:
            os.remove(base_path + '.idump')
        except FileNotFoundError:
            pass
    else:
        with open(base_path + '.idump', 'wb') as f:
            f.write(hb.FInfo.Type + hb.FInfo.Creator)

    data = hb.read()
    if hb.FInfo.Type in [b'TEXT', b'ttro']:
        data = data.replace(b'\r', b'\n').decode('mac_roman').encode('utf-8')
    with open(base_path, 'wb') as f:
        f.write(data)

    rsrc = hb.read_rsrc()
    if rsrc:
        with open(base_path + '.rdump', 'wb') as f:
            f.write(macresources.make_rez_code(macresources.parse_file(rsrc), ascii_clean=True))
    else:
        try:
            os.remove(base_path + '.rdump')
        except FileNotFoundError:
            pass


def is_hqx_name(the_path):
    name = path.basename(the_path)
    base, ext = path.splitext(name)
    if ext.lower() == '.hqx':
        return True
    else:
        return False


parser = argparse.ArgumentParser(description='''
    UnBinHex (BASE.hqx) into (BASE + BASE.rdump + BASE.idump)
''')

parser.add_argument('hqx', metavar='BASE.hqx', nargs='+', help='file or directory')

args = parser.parse_args()

for hqx in args.hqx:
    if path.isdir(hqx):
        for hqx, dirlist, filelist in os.walk(hqx):
            dirlist[:] = [d for d in dirlist if not d.startswith('.')]; dirlist.sort()
            filelist[:] = [f for f in filelist if not f.startswith('.')]; filelist.sort()

            for f in filelist:
                if is_hqx_name(f):
                    do_file(path.join(hqx, f))
    else:
        if not is_hqx_name(hqx):
            exit('Not a BinHex file')

        do_file(hqx)
