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

parser = argparse.ArgumentParser(description='''
    Supply base name to convert datafork+rdump+idump to HQX.
    Supply base.hqx name to convert HQX to datafork+rdump+idump.
''')

parser.add_argument('srcfile', nargs='+', help='base or base.hqx')

args = parser.parse_args()


for srcfile in args.srcfile:
    # Case 1: from BinHex
    if path.splitext(path.basename(srcfile))[1].lower() == '.hqx':
        hb = binhex.HexBin(srcfile)

        base = path.splitext(srcfile)[0]

        if hb.FInfo.Type == hb.FInfo.Creator == b'????':
            try:
                os.remove(base + '.idump')
            except FileNotFoundError:
                pass
        else:
            with open(base + '.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, 'wb') as f:
            f.write(data)

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

    # Case 2: to BinHex
    else:
        finfo = binhex.FInfo()
        finfo.Flags = 0

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

        try:
            data = open(srcfile, '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(srcfile + '.rdump', 'rb').read()
            rsrc = macresources.make_file(macresources.parse_rez_code(rsrc))
        except:
            rsrc = b''

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

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

        bh.close()
