#!/usr/bin/env python
"""
Script to generate a XML tree with a deep hierarchy.
"""
import sys
import os
if len(sys.argv) < 2:
    print(f"Call syntax: {sys.argv[0]} [WANTED_DEEPNESS]")
    print(f"    Example: {sys.argv[0]} 10")
    sys.exit(1)
wanted_deepness = int(sys.argv[1])
output_file = "/tmp/sample.xml"
leaf_content = "<content>XXX</content>"
content = '<?xml version="1.0" encoding="UTF-8"?><root>'
print(f"[+] Generate XML tree with a deep of {wanted_deepness} hierarchy...")
for i in range(wanted_deepness):
    progress = str(round((i*100) / wanted_deepness)).zfill(2)
    print(f"\r{progress}%", end="", flush=True)
    if i == (wanted_deepness-1):
        content += leaf_content
    else:
        content += f"<level deep=\"{str(i+1).zfill(len(sys.argv[1]))}\">"
content += "</level>"*(wanted_deepness-1)
content += "</root>"
print("\r100%")
print("[+] Saving content to file...")
with open(output_file, mode="w", encoding="utf-8") as f:
    f.write(content)
file_stats = os.stat(output_file)
print(f"[V] File saved to '{output_file}' ({round(file_stats.st_size / (1024 * 1024))} MB).")
