#!/usr/bin/env python3
 
import struct
import sys
 
 
def hex_to_sid(hex_str):
    # Remove common formatting
    hex_str = (
        hex_str.strip()
        .replace("b'", "")
        .replace("'", "")
        .replace("0x", "")
    )
 
    data = bytes.fromhex(hex_str)
 
    revision = data[0]
    sub_auth_count = data[1]
    identifier_authority = int.from_bytes(data[2:8], byteorder="big")
 
    sub_authorities = []
 
    for i in range(sub_auth_count):
        start = 8 + (i * 4)
        subauth = struct.unpack("<I", data[start:start + 4])[0]
        sub_authorities.append(str(subauth))
 
    return f"S-{revision}-{identifier_authority}-{'-'.join(sub_authorities)}"
 
 
def main():
    if len(sys.argv) != 2:
        print(f"Usage: {sys.argv[0]} <hex_string>")
        print(f"Example:")
        print(
            f"  {sys.argv[0]} "
            "0105000000000005150000005b7bb0f398aa2245ad4a1ca4f4010000"
        )
        sys.exit(1)
 
    try:
        sid = hex_to_sid(sys.argv[1])
        print(sid)
    except Exception as e:
        print(f"Error: {e}")
        sys.exit(1)
 
 
if __name__ == "__main__":
    main()