import java.io.*;
import java.util.*;

public class Shell extends Thread 
{
    private String[] cmdLine;
    private int shellCounter;
    public Shell() 
    {
        shellCounter = 1;
    }
    // exit keyword
    private boolean isExitCmd(String[] cmdLine
    {
        return !(cmdLine[0].contentEquals("Exit"|| cmdLine[0].contentEquals("EXIT"|| cmdLine[0].contentEquals("exit"));
    }
    
    public void run()
    {
        boolean isExit = true;
        do {
            SysLib.cout("My Shell" + "[" + (shellCounter+++ "]% ");
            StringBuffer args = new StringBuffer();
            try {
                SysLib.cin(args);
                cmdLine = SysLib.stringToArgs(args.toString());
            } catch (Exception e) {
                SysLib.cerr("Error exiting shell");
                break;
            }
            // enter only
            if (cmdLine.length < 1) {
                SysLib.cout("You pressed Enter, if you want to leave shell just type exit\n");
                continue;  
            }
            // tab only
            String tabCheck = args.toString().trim();
            if (tabCheck.length() < 1) {
                SysLib.cout("You only typed tab, if you want to leave shell just type exit\n");
                continue;
            }
            // exit
            isExit = isExitCmd(cmdLine);
            if (!isExit) {
                SysLib.cout("Exiting the Shell\n");
                break;
            }
            String[] semicolon = args.toString().split(";");
            String[] ampersand = args.toString().split("&");
            // split if nothing to split then is 1 else 1 + @
            boolean isContainSemi = (semicolon.length > 1? true : false;
            boolean isContainAmper = (ampersand.length > 1? true : false;
            // both ; and &
            if (isContainSemi && isContainAmper) {
                for (int i = 0; i < semicolon.length; i++) {
                    String[] amper = semicolon[i].toString().split("&");
                    // SysLib.cout(amper[0] + "\n");
                    
                    if (amper.length == 1) {
                        String[] semi = SysLib.stringToArgs(semicolon[i]);
                        SysLib.cout(semi[0+ "\n");
                        if (SysLib.exec(semi) < 0) {
                            return;    
                        }
                        SysLib.join();
                    } else {
                        int process = 0;
                        for (int j = 0; j < amper.length; j++) {
                            String[] amp = SysLib.stringToArgs(amper[j]);
                            SysLib.cout(amp[0+ "\n");
                            if (SysLib.exec(amp) < 0)  {
                                process--;
                            }
                            process++;
                        }
                        for (int k = 0; k < process; k++) {
                            SysLib.join();
                        }
                    }
                }
            }
            // just semi
            else if (isContainSemi) {
                for (int i = 0; i < semicolon.length; i++) {
                    String[] cmds = SysLib.stringToArgs(semicolon[i]);
                    // SysLib.cout(cmds[i] + "Hello World");
                    SysLib.cout(cmds[0+ "\n");
                    if (SysLib.exec(cmds) < 0) {
                        return;
                    }
                    SysLib.join();
                }
            }
            // amper
            else if (isContainAmper) {
                int process = 0;
                // SysLib.join();
                for (int i = 0; i < ampersand.length; i++) {
                    // SysLib.cout("helo");
                    String[] cmds = SysLib.stringToArgs(ampersand[i]);
                    SysLib.cout(cmds[0+ "\n");
                    // child id else -1
                    if (SysLib.exec(cmds) < 0) {
                        process--;
                    }
                    process++;
                }
                for (int j = 0; j < process; j++) {
                    SysLib.join();
                }
                // plane
            } else {
                SysLib.exec(cmdLine);
                SysLib.join();
            }
        } while(isExit);
        // finish
        SysLib.exit();
    }
}


근대 돌릴려면, 학교 threadOS 파일 필요함  ㅇㅅㅇ