import org.apache.tools.ant.Main;
import java.util.*;
import java.io.*;

public class MainSh {

    public static void main(String[] args) throws Exception {
        MainSh mainSh = new MainSh(args);
        mainSh.run();
    }

    private String[] _cmdLineArgs;
    private List _previousCmds;

    public MainSh(String[] cmdLineArgs) {
        _cmdLineArgs = cmdLineArgs;
        _previousCmds = new ArrayList();
    }

    public void run() throws Exception {
        boolean exit = false;
        BufferedReader br = new  BufferedReader(new InputStreamReader(System.in));
        while (!exit) {
            prompt();
            String line = br.readLine().trim();

            if (line.equals("exit")) {
                exit = true;
            }
            else
            if (line.equals("defaults")) {
                defaults(_cmdLineArgs);
            }
            else
            if (line.equals("help")) {
                help();
            }
            else
            if (line.startsWith("ant")) {
                execute(line);
            }
            else
            if (line.equals("history")) {
                history();
            }
            else
            if (line.startsWith("!")) {
                int index = -1;
                if (line.equals("!!")) {
                    index = _previousCmds.size()-1;
                }
                else {
                    try {
                        index = Integer.parseInt(line.substring(1));
                    }
                    catch (Exception ex) {
                    }
                }
                if (index>-1 && index<_previousCmds.size()) {
                    line = (String) _previousCmds.get(index);
                    execute(line);
                }
                else {
                    shellError();
                }
            }
            else {
                shellError();
            }
        }
        br.close();
    }

    public void execute(String line) throws Exception {
        String[] shArgs = getCallArguments(_cmdLineArgs,line);
        Main.start(shArgs,null,null,false);
        _previousCmds.add(line);
    }

    public void prompt() {
        System.out.print("\nantsh>");
        System.out.flush();
    }

    public void help() {
        System.out.println();
        System.out.println("    exit");
        System.out.println("    help");
        System.out.println("    defaults");
        System.out.println("    history");
        System.out.println("    !!  (last ant command)");
        System.out.println("    !## (## ant command)");
        System.out.println("    ant [Ant options and targets]");
    }

    public void shellError() {
        System.out.println();
        System.out.println("Invalid antsh command!");
    }

    public void defaults(String[] args) {
        System.out.println();
        for (int i=0;i<args.length;i++) {
            System.out.print(args[i]+" ");
        }
        System.out.println();
    }

    public void history() {
        int begin = (_previousCmds.size()>20) ? _previousCmds.size()-20 : 0;
        int end = _previousCmds.size();
        if (_previousCmds.size()>0) {
            System.out.println();
            for (int i=begin;i<end;i++) {
                System.out.println("  "+i+": "+_previousCmds.get(i));
            }
        }
    }

    public String[] getCallArguments(String[] cmdLineArgs,String shellLine) {
        List argList = new ArrayList(cmdLineArgs.length+10);
        for (int i=0;i<cmdLineArgs.length;i++) {
            argList.add(cmdLineArgs[i]);
        }
        ArgumentTokenizer at = new ArgumentTokenizer(shellLine);
        at.next(); // consume ant
        while (at.hasNext()) {
            argList.add(at.next());
        }
        String[] args = new String[argList.size()];
        argList.toArray(args);
        return args;
    }

    private static class ArgumentTokenizer {
        private String _line;
        private int    _pos;

        public ArgumentTokenizer(String line) {
            _line = line.trim();
            _pos  = 0;
        }

        public boolean hasNext() {
            return _pos<_line.length();
        }

        public String next() {
            String arg = "";
            while (_pos<_line.length() && _line.charAt(_pos)==' ') {
                _pos++;
            }
            boolean endOfArg = false;
            boolean betweenQuotes = false;
            while (!endOfArg && _pos<_line.length()) {
                char currentChar = _line.charAt(_pos);
                switch (currentChar) {
                    case ' ':
                        if (!betweenQuotes) {
                            endOfArg = true;
                        }
                        else {
                            arg += currentChar;
                        }
                        break;
                    case '"':
                        betweenQuotes = !betweenQuotes;
                        break;
                    default:
                        arg += currentChar;
                        break;
                }
                _pos++;
            }
            return arg;
        }
    }

}
