#!/usr/bin/env python3

import os
import subprocess
import sys

RED = "\033[0;31m"
RESET = "\033[0m"

def run(cmd, fail_msg):
    try:
        subprocess.run(cmd, check=True)
    except subprocess.CalledProcessError:
        print(f"{RED}[!] {fail_msg}{RESET}")
        sys.exit(1)

def main():
    if os.geteuid() != 0:
        print(f"{RED}[!] Superuser access is required{RESET}")
        sys.exit(1)

    print(f"{RED}[*] Unmounting /mnt/usb...{RESET}")
    run(["umount", "/mnt/usb"], "Failed to unmount /mnt/usb")

    print(f"{RED}[*] Closing encrypted device...{RESET}")
    run(["cryptsetup", "close", "sda1_crypt"], "Failed to close mapper device")

    print(f"{RED}[*] Unmounted and closed successfully!{RESET}")

if __name__ == "__main__":
    main()
