#### WELCOME
import os
import hashlib
import puremagic
import re

def decode_filename(filename):
    """
    Decodes a inziption filename
    Returns [password, id, extension]

    Filename Format: <string>.<id>.<extension>
    Decoder ring:
    +----+------------------+
    | ID |     PASSWORD     |
    +====+==================+
    | A  | <string>         |
    | B  | <string>.reverse |
    | C  | md5(<string>)    |
    | D  | sha1(<string>)   |
    | E  | ''               |
    +----+------------------+
    """
    print(filename)    
    split_name = os.path.basename(filename).split('.')
    if len(split_name) == 3:
        f_string = split_name[0]
        f_id = split_name[1]
        f_ext = split_name[2]
    # TODO handle when len is not 3

    if f_id == "A":
        return [f_string, f_id, f_ext]
    if f_id == "B": 
        # <string>,reverse
        return [f_string[::-1], f_id, f_ext]
    if f_id == "C":
        # should match md5sum <(echo -n f_string)
        return [hashlib.md5(f_string.encode("utf-8")).hexdigest(), f_id, f_ext]
    if f_id == "D":
        # should match sha1sum <(echo -n f_string)
        return [hashlib.sha1(f_string.encode("utf-8")).hexdigest(), f_id, f_ext]
    if f_id == "E":
        return ["''", f_id, f_ext]

def un_7z(filename, password):
    import py7zr
    with py7zr.SevenZipFile(filename, password=password) as z:
        if len(z.getnames()) == 1:
            next_file = z.getnames()[0]
            z.extractall(path="tmp")
            return f"tmp/{next_file}"
        else:
            print(f"bad 7z {filename}")

def un_zip(filename, password):
    import zipfile
    with zipfile.ZipFile(filename) as z: 
        if len(z.namelist()) == 1:
            next_file = z.namelist()[0]
            z.extract(next_file, path="tmp", pwd=bytes(password, 'utf-8'))
            return f"tmp/{next_file}"
        else:
            print(f"bad zip {filename}")

def un_gz(filename, password):
    import gzip, zipfile, magic
    next_file = re.findall(r'["](.*?)["]',magic.from_file(filename))[0]
    with gzip.GzipFile(filename) as gz: 
        ungzip_data = gz.read()
        with open(f"tmp/{next_file}", 'wb') as out:
            out.write(ungzip_data)
        return f"tmp/{next_file}"
        
def un_tar(filename, password):
    import tarfile
    with tarfile.open(filename) as z: 
        if len(z.getnames()) == 1:
            next_file = z.getnames()[0]
            z.extractall(path="tmp")
            return f"tmp/{next_file}"
        else:
            print(f"bad tar {filename}")

def unziption(filename):
    password, _, ext = decode_filename(filename)
    print(filename, password)
    ext = puremagic.from_file(filename)
    if ext == ".7z":
        return un_7z(filename, password)
    if ext == ".zip":
        return un_zip(filename, password)
    if ext == ".gz":
        return un_gz(filename, password)
    if ext == ".tar":
        return un_tar(filename, password)

zip_file = "provided/jB1qLK53r.A.7z"

while True:
    zip_file = unziption(zip_file)
