from os import startfile, chdir, mkdir, getcwd, listdir, rmdir, remove, path, system
import shutil
import subprocess
import time


bashlocation = getcwd()
print("You are in the virtual enviroment of PYOS, Do help for more information.\n")
chdir('..')
homelocation = getcwd()
print(homelocation)
chdir('passwords')
passlocation=getcwd()
with open('usernamefile.txt', "r") as f:
    readusername = f.read().strip()
chdir('..')
try:
    with open('user/apps/vpx/comp.txt',"r") as compl:
        compl.readline()
        complpath=compl.readline()
except FileNotFoundError:
    pass

chdir(bashlocation)
def lsblk():
    result = subprocess.run(['wmic', 'diskdrive', 'list', 'brief'], capture_output=True, text=True)
    print(result.stdout)





while True:

    inpcmd = input(f' User:{readusername} ')
    try:
        if inpcmd.split()[0] == "webd":
            args = inpcmd.split()[1:]
            if args:
                url = args[0]
                system(f'curl -O -k "{url}"')
        
        if inpcmd=="lsblk":
            print(lsblk())
        if inpcmd=="help":
            print(
                '\nhelp -a - help for all commands\n',
                'help <cmd> - help with a command work in progress \n'
            )
        
        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',
                    
                    'dsktop - run desktop\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'
                    'vpx -c <file> - run a file using vpx\n'
                    'vpx -sc <path> - set compiler path for vpx compiler\n'
                    'fex - opens file explorer\n'
                    'pydo -app- - installs an app into your app folder as a ZIP, for different downloads, please visit pydo.pages.dev \n'
                    
                    
                    )
        if inpcmd.split()[0] == "pydo":
            afterspace= ' '.join(inpcmd.split()[1:])
            llc = getcwd()
            chdir(homelocation)
            chdir("user")
            chdir("apps")
            system(f'curl -O -k "https://pydo.pages.dev/apps/{afterspace}"')
            print("Successfully downloaded ZIP app into:", getcwd())
            chdir(llc)

        if inpcmd.split()[0] == "print":
            afterspace= ' '.join(inpcmd.split()[1:])
            print(afterspace)
        if inpcmd=="cmd":
            break
        if inpcmd.split()[0] =="vfomkf":
            afterspace= ' '.join(inpcmd.split()[1:])
            try:
                with open(afterspace, "r")as f:
                    print(f.read())
            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] == "fex":
            llc = getcwd()
            chdir(homelocation)
            startfile("fex.py")
            chdir(llc)
        if inpcmd.split()[0] == "vpx":
            afterspace = ' '.join(inpcmd.split()[1:])
            if "-c" in afterspace:
                try:

                    twospace = ' '.join(afterspace.split()[1:])
                    
                    with open(twospace,"r") as f:
                        readfile = f.read()



                        # Write content to a file in the temporary directory
                    with open('compile.py', "w") as cmpl:
                        cmpl.write('import os\n')
                        cmpl.write(f'import sys\n')
                        cmpl.write(f'sys.path.append(r"{complpath}")\n')
                        cmpl.write('from code_1 import *\n')  # Import code_1 from the current directory


                        cmpl.write(readfile)
                    startfile('compile.py')
                    time.sleep(1)
                    remove('compile.py')
                except FileNotFoundError:
                    print('File not found or compiler destination not found')
            if "-sc" in afterspace:
                try:
                    llc=getcwd()
                    twospace = ' '.join(afterspace.split()[1:])
                    chdir(homelocation)
                    chdir('user')
                    chdir('apps')
                    mkdir('vpx')
                    chdir('vpx')
                    with open('comp.txt', "w") as cmple:
                        cmple.truncate(0)
                        cmple.write('\n')
                        cmple.write(twospace)
                    with open('comp.txt',"r") as cample:
                        cample.readline()
                        complpath=cample.readline()
                    chdir(llc)
                    print(f'Dictionary path set as: {complpath}')
                except FileExistsError:
                    print('Dictionary path already set')
                    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]=="dsktop":
            llc=getcwd()
            chdir(homelocation)
            startfile('homemenubash.py')
            chdir(llc)
    except IndexError:
        pass
while 2==2:
    inpcmd==input(f'CMDPROMPT_{readusername}')
    system(f'{inpcmd}')
        
            
    
