#!/usr/bin/env python3

import sys
import os
import zlib
import base64
import argparse
import subprocess

def create_iso(input_dir, output_file, lba, volume_name):
    subprocess.run(['mkisofs', '-C', f'0,{lba}', '-V', volume_name, '-exclude', 'IP.BIN', '-G', 'data/IP.BIN', '-l', '-J', '-r', '-o', output_file, input_dir], check=True)

def create_cdi_image(input_file, output_file, lba):
    try:
        with open(input_file, 'rb') as f:
            f.seek(0, 2)
            file_size = f.tell()
            sector_count = int(file_size / 2048)
            f.seek(0)

            print(f"Processing file: {input_file}")

            with open(output_file, 'wb') as g:
                g.write(bytes(1063104))
                temp = zlib.decompress(base64.b64decode(tdi))
                g.write(temp)

                for i in range(0, sector_count, 1):
                    temp = f.read(0x800)
                    g.write(b'\x00' * 8)
                    g.write(temp)
                    g.write(b'\x00' * 280)

                temp = zlib.decompress(base64.b64decode(end))
                g.write(temp)

                g.seek(-158, 2)
                g.write(lba.to_bytes(4, 'little'))
                g.seek(-277, 2)
                g.write((sector_count + 152).to_bytes(4, 'little'))
                g.seek(-310, 2)
                g.write(lba.to_bytes(4, 'little'))
                g.write((sector_count + 152).to_bytes(4, 'little'))
                g.seek(-336, 2)
                g.write((sector_count + 2).to_bytes(4, 'little'))

        print(f"CDI image created: {output_file}")
    except FileNotFoundError:
        print(f"Error: Input file '{input_file}' not found.")
    except Exception as e:
        print(f"Error: {e}")

tdi=b'''
eJzt1r+Lz3EcwPHPfWNCKVlMfpQVu8JlMZDBDWeSK4tYXSndopTx/gOD4QxkuY1FGVwd62
WwMon79rV9lSvdMyw3MHg86j283r179R6fw3B4GH4cAADOHHj2YtBHAAA/6SMAgNJHAACl
jwAASh8BAJQ+AgAofQQAUPoIAKD0EQBA6SMAgNJHAACljwAASh8BAJQ+AgAofQQAUPoIAK
D0EQBA6SMAgNJHAACljwAASh8BAJQ+AgAofQQAUPoIAKD0EQBA6SMAgNJHAACljwAASh8B
AJQ+AgAofQQAUPoIAKD0EQBA6SMAgNJHAACljwAASh8BAJQ+AgAofQQAUPoIAKD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sF/KADJN0B1CACxyxp8
dmEzBackNYMghA1dDcz0E+sZGF6xMWAAkDwLiIFNEu546gcBzH0gd6E4hvwgWGACbGsTAmCT
8SYUEACGRcNCoB0AWgtNAw=='''

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Create CDI image from an ISO file.")
    parser.add_argument("-i", "--input", required=True, help="Input ISO file")
    parser.add_argument("-o", "--output", help="Output CDI file (default: <input_filename>.cdi)")
    parser.add_argument("-l", "--lba", type=int, default=11702, help="LBA parameter (default: 11702)")
    parser.add_argument("-V", "--volume-name", default="example", help="Volume name for the ISO image (default: 'example')")

    args = parser.parse_args()

    input_file = args.input
    output_file = args.output or f"{os.path.splitext(args.input)[0]}.cdi"
    lba = args.lba
    volume_name = args.volume_name

    # Create the ISO file first
    create_iso("data", "test.iso", lba, volume_name)

    # Then create the CDI image
    create_cdi_image("test.iso", output_file, lba)

    # Clean up the temporary ISO file
    os.remove("test.iso")