/*
 * 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.teradata.prestomanager.agent;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.File;
import java.net.URL;

import static com.teradata.prestomanager.agent.CommandExecutor.executeCommand;
import static com.teradata.prestomanager.agent.PackageApiUtils.checkRpmPackage;
import static com.teradata.prestomanager.agent.PackageApiUtils.fetchFileFromUrl;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;

public final class PrestoInstaller
        implements PrestoCommand
{
    private static final Logger LOGGER = LogManager.getLogger(PrestoInstaller.class);
    private static final int SUBPROCESS_TIMEOUT = 150;

    private final PackageType packageType;
    private final URL urlToFetchPackage;
    private final boolean checkDependencies;

    public PrestoInstaller(PackageType packageType, URL urlToFetchPackage, boolean checkDependencies)
    {
        this.packageType = requireNonNull(packageType);
        this.urlToFetchPackage = requireNonNull(urlToFetchPackage);
        this.checkDependencies = requireNonNull(checkDependencies);
    }

    public void runCommand()
            throws PrestoManagerException
    {
        switch (packageType) {
            case RPM:
                File tempFile = fetchFileFromUrl(urlToFetchPackage);
                try {
                    installUsingRpm(tempFile.toString(), checkDependencies);
                }
                finally {
                    if (!tempFile.delete()) {
                        LOGGER.warn("Failed to delete the tempFile: {}", tempFile.toString());
                    }
                }
                break;
            case TARBALL:
                // TODO: Add tarball installation
                throw new UnsupportedOperationException("Tarball installation is not supported");
            default:
                throw new IllegalArgumentException(format("Unsupported package type %s", packageType));
        }
    }

    private static void installUsingRpm(String pathToRpm, boolean checkDependencies)
            throws PrestoManagerException
    {
        checkRpmPackage(pathToRpm);
        int installRpm;
        if (checkDependencies) {
            installRpm = executeCommand(SUBPROCESS_TIMEOUT, "sudo", "rpm", "-iv", pathToRpm);
        }
        else {
            installRpm = executeCommand(SUBPROCESS_TIMEOUT, "sudo", "rpm", "-iv", "--nodeps", pathToRpm);
        }
        if (installRpm != 0) {
            throw new PrestoManagerException("Failed to install presto", installRpm);
        }
        LOGGER.debug("Successfully installed presto");
    }
}

@@ -720,15 +720,98 @@
 ava.
-net.URL
+io.IOException;%0Aimport java.net.URL;%0Aimport java.nio.file.Path;%0Aimport java.nio.file.Paths
 ;%0A%0Ai
@@ -1088,16 +1088,155 @@
 format;%0A
+import static java.nio.file.Files.createDirectory;%0Aimport static java.nio.file.Files.isDirectory;%0Aimport static java.nio.file.Files.write;%0A
 import s
@@ -1428,24 +1428,102 @@
 ler.class);%0A
+    private static final Path CATALOG_DIR = Paths.get(%22/etc/presto/catalog%22);%0A
     private 
@@ -3445,32 +3445,58 @@
 Rpm);%0A        %7D%0A
+        addTpchCatalog();%0A
         LOGGER.d
@@ -3539,11 +3539,454 @@
 );%0A    %7D
+%0A%0A    private static void addTpchCatalog()%0A            throws PrestoManagerException%0A    %7B%0A        try %7B%0A            if (!isDirectory(CATALOG_DIR)) %7B%0A                createDirectory(CATALOG_DIR);%0A                write(CATALOG_DIR.resolve(%22tpch.properties%22), %22connector.name=tpch%22.getBytes());%0A            %7D%0A        %7D%0A        catch (IOException e) %7B%0A            throw new PrestoManagerException(%22Failed to add tpch catalog%22, e);%0A        %7D%0A    %7D
 %0A%7D%0A

Add tpch catalog during installation