/*
 * Copyright 2017-2018 Baidu Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.baidu.rasp;

import com.baidu.rasp.install.Installer;
import com.baidu.rasp.install.InstallerFactory;
import com.baidu.rasp.install.linux.LinuxInstallerFactory;
import com.baidu.rasp.install.windows.WindowsInstallerFactory;
import com.baidu.rasp.uninstall.Uninstaller;
import com.baidu.rasp.uninstall.UninstallerFactory;
import com.baidu.rasp.uninstall.linux.LinuxUninstallerFactory;
import com.baidu.rasp.uninstall.windows.WindowsUninstallerFactory;
import org.apache.commons.cli.*;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;

import static com.baidu.rasp.RaspError.E10005;

/**
 * Created by OpenRASP on 5/11/17.
 */
public class App {
    public static String install;
    public static String appId;
    public static String appSecret;
    public static String baseDir;
    public static String url;

    private static InstallerFactory newInstallerFactory() {
        if (System.getProperty("os.name").startsWith("Windows")) {
            return new WindowsInstallerFactory();
        } else {
            return new LinuxInstallerFactory();
        }
    }

    private static UninstallerFactory newUninstallerFactory() {
        if (System.getProperty("os.name").startsWith("Windows")) {
            return new WindowsUninstallerFactory();
        } else {
            return new LinuxUninstallerFactory();
        }
    }

    private static void argsParser(String[] args) throws ParseException, RaspError {
        Options options = new Options();
        options.addOption("install", true, "install OpenRASP agent");
        options.addOption("uninstall", true, "uninstall OpenRASP agent");
        options.addOption("appid", true, "Value of cloud.appid");
        options.addOption("appsecret", true, "Value of cloud.appsecret");
        options.addOption("backendurl", true, "Value of cloud.backendurl");
        options.addOption("help", false, "helpMessage");
        CommandLineParser parser = new PosixParser();
        CommandLine cmd = parser.parse(options, args);
        if (cmd.hasOption("help")) {
            HelpFormatter hf = new HelpFormatter();
            hf.printHelp("Options", options);
        } else {
            if (cmd.hasOption("install") && cmd.hasOption("uninstall") || !cmd.hasOption("install") && !cmd.hasOption("uninstall")) {
                throw new RaspError(E10005 + "install and uninstall must only be selected one");
            } else if (cmd.hasOption("install")) {
                baseDir = cmd.getOptionValue("install");
                install = "install";
            } else {
                baseDir = cmd.getOptionValue("uninstall");
                install = "uninstall";
            }
            appId = cmd.getOptionValue("appid");
            appSecret = cmd.getOptionValue("appsecret");
            url = cmd.getOptionValue("backendurl");
            if (!(appId != null && appSecret != null && url != null || appId == null && appSecret == null && url == null)) {
                throw new RaspError(E10005 + "url and appId and appSecret must be set at the same time");
            }
        }
    }

    private static void showBanner() {
        String banner = "OpenRASP Installer for Java app servers - Copyright 2017-2018 Baidu Inc.\n" +
                "For more details visit: https://rasp.baidu.com/doc/install/software.html\n";
        System.out.println(banner);
    }

    private static void showHelp() {
        String helpMsg = "Usage: \n" +
                "  java -jar RaspInstall.jar -install /tomcat/\n" +
                "  java -jar RaspInstall.jar -uninstall /tomcat/\n" +
                "\n" +
                "Additional    command line arguments: \n" +
                "  -install      Specify application server path\n" +
                "  -uninstall    Specify application server path\n" +
                "  -appid        Value of cloud.appid\n" +
                "  -backendurl   Value of cloud.address\n" +
                "  -appsecret    Value of cloud.appsecret\n";
        System.out.println(helpMsg);
    }

    public static void main(String[] args) throws IOException, URISyntaxException {
        showBanner();
        try {
            argsParser(args);
            System.out.println(install+"===="+baseDir+"====="+appSecret+"====="+appId+"===="+url);
            if ("install".equals(install)) {
                File serverRoot = new File(baseDir);
                InstallerFactory factory = newInstallerFactory();
                Installer installer = factory.getInstaller(serverRoot);
                installer.install(url, appId, appSecret);
            } else if ("uninstall".equals(install)) {
                File serverRoot = new File(baseDir);
                UninstallerFactory factory = newUninstallerFactory();
                Uninstaller uninstaller = factory.getUninstaller(serverRoot);
                uninstaller.uninstall();
            } else {
                showHelp();
            }
        } catch (Exception e) {
            System.out.println("eeeeeeeeeeeeee");
            System.out.println(e.getMessage() + "\n");
            System.exit(1);
        }
    }
}

@@ -2149,38 +2149,47 @@
  true, %22
-install OpenRASP agent
+specify application server path
 %22);%0A    
@@ -2234,32 +2234,39 @@
 e, %22
-uninstall OpenRASP agent
+specify application server path
 %22);%0A
@@ -2527,19 +2527,33 @@
 e, %22
-helpMessage
+print options information
 %22);%0A
@@ -2714,91 +2714,17 @@
     
-HelpFormatter hf = new HelpFormatter();%0A            hf.printHelp(%22Options%22, options
+showHelp(
 );%0A 
@@ -4765,107 +4765,8 @@
 s);%0A
-            System.out.println(install+%22====%22+baseDir+%22=====%22+appSecret+%22=====%22+appId+%22====%22+url);%0A
     
@@ -5368,139 +5368,40 @@
    %7D
- else %7B%0A                showHelp();%0A            %7D%0A        %7D catch (Exception e) %7B%0A            System.out.println(%22eeeeeeeeeeeeee%22);
+%0A        %7D catch (Exception e) %7B
 %0A   

modify rasp install