from os import getcwd, chdir, mkdir, startfile, path, remove, listdir, system
import shutil
import subprocess
rtl=getcwd()


import subprocess

def lsblk():
    result = subprocess.run(['wmic', 'diskdrive', 'list', 'brief'], capture_output=True, text=True)
    print(result.stdout)




print('For help do "help"')
while True:

    inpcmd = input(' pyos_ROOT_USER: ')
    try:
        
        if inpcmd=="lsblk":
            print(lsblk())
        if inpcmd=="help":
            print(
                '-a - All commands list\n',
                '<cmd> - Help with a command work in progress',
            )
        if inpcmd.split()[0]=="help":
            afterspace= ' '.join(inpcmd.split()[1:])
            if afterspace=="-a":
                print(
                    '\nprint <str> - Print a string example case: print helloworld\n',
                    'vfomkf <file> - Make/read all contents of a file\n',
                    'cd <dir> - CD into a directory example if im in C: then i do cd <folder> then show where i am i would be in C:/<folder>\n',
                    'mkdir <name> - Make a folder with the name <name>\n',
                    'boot <user> - Boot as an existing user\n',
                    'adduser - Start the user creation program\n',
                    'rmf <folder/file> - Delete a folder/file\n',
                    'ls <dir> - List Items of a directory, Use "ls -h" to list items of the directory you are in\n',
                    'run <file> - Run <file>\n'
                    'lsblk - list devices\n'
                    

                )
            if afterspace=="print":
                print(
                    '\nThe print statement prints a string to the terminal\n',
                    'An example of using it would be doing "print hello world" This is\n',
                    'Different from programming languages because there are no parenthesis or ""\n',
                    'to indicate that it is a string and not a variable\n'
                )
            if afterspace=="vfomkf":
                print(
                    '\nThe vfomkf Command is a command that lets users\n',
                    'Make a file or View a file depending if it exists or not\n',
                    'For an example if i did "vfomkf helloworld.txt" With that file in my directory\n',
                    'Existing it would print the content of "helloworld.txt" to the terminal\n',
                    'But if it doesnt exist it would simply make the file "helloworld.txt" \n'
                )
            if afterspace=="cd":
                print(
                    '\nThe cd command is the command to get into another\n',
                    'Directory. for example if you  were in "C:" but then in bash\n',
                    'you type in "cd MyFolder" you would be in C:MyFolder\n',
                    'And if you type in "vfomkf helloworld" you would make a file \n',
                    'inside of C:MyFolder.\n'
                )
        

        if inpcmd.split()[0] == "print":
            afterspace= ' '.join(inpcmd.split()[1:])
            print(afterspace)
        if inpcmd.split()[0] =="vfomkf":
            afterspace= ' '.join(inpcmd.split()[1:])
            try:
                with open(afterspace, "r")as f:
                    print("\n")
                    print(f.read())
                    print('\n')
            except FileNotFoundError:
                try:
                    with open(afterspace,"w"):
                        pass
                    print(f'File: {afterspace} Made Successfully')
                except FileNotFoundError:
                    print('You need to include a filename.')
        if inpcmd == "wai":
            print(getcwd())
        if inpcmd.split()[0] == "cd":
            try:

                afterspace= ' '.join(inpcmd.split()[1:])
                chdir(afterspace)
            except FileNotFoundError:
                print('Directory not found.')
        if inpcmd.split()[0] == "mkdir":
            afterspace= ' '.join(inpcmd.split()[1:])
            mkdir(afterspace)
        if inpcmd.split()[0] == "boot":
            afterspace= ' '.join(inpcmd.split()[1:])
            llc=getcwd()
            chdir(rtl)
            try:
                chdir('home/' + afterspace + '/passwords')

                startfile('pincodething.py')
                chdir(llc)
            except FileNotFoundError:
                print(f"User:'{afterspace}' Not Found.")
                chdir(llc)
        if inpcmd=="adduser":
            llc=getcwd()
            chdir(rtl)
            chdir('home')
            startfile('usercreate.py')
            chdir(llc)
        if inpcmd.split()[0]== "rmf":
            
            try:
                afterspace= ' '.join(inpcmd.split()[1:])
                if path.isfile(afterspace):  # Check if it's a file
                    remove(afterspace)
                    print(f"File: '{afterspace}' Deleted.")
                elif path.isdir(afterspace):  # Check if it's a directory
                    shutil.rmtree(afterspace)  # Remove directory and its contents recursively
                    print(f"Folder: '{afterspace}' Deleted.")
            except FileNotFoundError:
                print(f"File/Folder: '{afterspace}' not found.")
            except PermissionError:
                print(f"No permission to delete: '{afterspace}'.")
        if inpcmd.split()[0]=="ls":
            try:
                afterspace= ' '.join(inpcmd.split()[1:])
                if afterspace=="-h":
                    print(listdir())
                else:
                    print(listdir(afterspace))

                
            except FileNotFoundError:
                print('Directory not found')
        if inpcmd.split()[0] == "run":
            afterspace= ' '.join(inpcmd.split()[1:])
            try:
                startfile(afterspace)
            except FileNotFoundError:
                print('File not found.')
        if inpcmd.split()[0] == "webd":
            args = inpcmd.split()[1:]
            if args:
                url = args[0]
                system(f'curl -O -k "{url}"')



        

    except IndexError:
        pass
        
                
        
