#!/usr/bin/env python3

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


def do_file(the_path):
    finfo = binhex.FInfo()
    finfo.Flags = 0

    try:
        info = open(the_path + '.idump', 'rb').read(8)
        assert len(info) == 8
        finfo.Type = info[:4]
        finfo.Creator = info[4:]
    except:
        pass

    try:
        data = open(the_path, 'rb').read()
        if finfo.Type in [b'TEXT', b'ttro']:
            data = data.replace(b'\n', b'\r').decode('utf-8').encode('mac_roman')
    except:
        data = b''

    try:
        rsrc = open(the_path + '.rdump', 'rb').read()
        rsrc = macresources.make_file(macresources.parse_rez_code(rsrc))
    except:
        rsrc = b''

    bh = binhex.BinHex((path.basename(the_path), finfo, len(data), len(rsrc)), the_path + '.hqx')

    bh.write(data)
    bh.write_rsrc(rsrc)

    bh.close()


def is_valid_base(the_path):
    name = path.basename(the_path)
    base, ext = path.splitext(name)
    if ext.lower() in ('.hqx', '.idump', '.rdump'): return False
    return True


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

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

args = parser.parse_args()

for base in args.base:
    if path.isdir(base):
        for base, dirlist, filelist in os.walk(base):
            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_valid_base(f):
                    do_file(path.join(base, f))

    else:
        if not is_valid_base(hqx):
            exit('Base names cannot have a .hqx/.idump/.rdump extension')

        do_file(base)
