!747

m {
  static L<S> startableBots = litlist("Text Bot.", "#1001417");
  
  p {
    makeAndroid3("Bot Starter.");
  }
  
  static synchronized S answer(S s, L<S> history) {
    new Matches m;
    
    if (match3("please start bot * and return port", s, m)) {
      S botName = unquote(m.m[0]);
      int port = getBotPort(botName);
      if (port == 0)
        port = startBot(botName);

      if (port == 0)
        return format3("Sorry. Could not start bot *", botName);
      
      return format3("OK. Port: *", port);
    }
    
    ret null;
  }
  
  // This maybe blocks stuff a little long... well.
  
  static int startBot(S botName) {
    for (int i = 0; i < startableBots.size(); i += 2) {
      S name = startableBots.get(i);
      if (name.startsWith(botName)) {
        S botID = startableBots.get(i+1);
        final Class c = hotwire(botID);
        thread { callMain(c); }
        ret waitForBotStartUp(botName);
      }
    }
    
    ret 0;
  }
}