/*
 * 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 io.airlift.log.Logger;

import static com.teradata.prestomanager.agent.CommandExecutor.executeCommand;
import static com.teradata.prestomanager.agent.PrestoRpmController.terminatePresto;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;

public class PrestoUninstaller
        implements PrestoCommand
{
    private static final Logger LOGGER = Logger.get(PrestoUninstaller.class);
    private static final String PRESTO_PACKAGE = "presto-server-rpm";
    private static final int SUBPROCESS_TIMEOUT = 90;

    private final PackageType packageType;
    private final boolean disableDependencyChecking;
    private final boolean forceUninstall;

    public PrestoUninstaller(PackageType packageType, boolean disableDependencyChecking, boolean forceUninstall)
    {
        this.packageType = requireNonNull(packageType);
        this.disableDependencyChecking = requireNonNull(disableDependencyChecking);
        this.forceUninstall = requireNonNull(forceUninstall);
    }

    public void runCommand()
            throws PrestoManagerException
    {
        switch (packageType) {
            case RPM:
                uninstallUsingRpm(disableDependencyChecking, forceUninstall);
                break;
            case TARBALL:
                // TODO: Add tarball uninstallation
                throw new UnsupportedOperationException("Tarball uninstall is not supported");
            default:
                throw new IllegalArgumentException(format("Unsupported package type %s", packageType));
        }
    }

    private static void uninstallUsingRpm(boolean checkDependencies, boolean forceUninstall)
            throws PrestoManagerException
    {
        int checkPackageInstalled = executeCommand("sudo", "rpm", "-q", PRESTO_PACKAGE);
        if (checkPackageInstalled != 0) {
            if (!forceUninstall) {
                throw new PrestoManagerException(format("Package '%s' is not installed", PRESTO_PACKAGE), checkPackageInstalled);
            }
            LOGGER.warn("Package '%s' is not installed; Process exited with return value: %s", PRESTO_PACKAGE, checkPackageInstalled);
            return;
        }
        if (executeCommand("service", "presto", "status") == 0) {
            if (!forceUninstall) {
                throw new PrestoManagerException("Presto is running");
            }
            LOGGER.warn("Presto is running; Presto will be forcibly stopped before attempting to uninstall.");
            terminatePresto();
        }
        int uninstallPackage;
        if (checkDependencies) {
            uninstallPackage = executeCommand(SUBPROCESS_TIMEOUT, "sudo", "rpm", "-e", PRESTO_PACKAGE);
        }
        else {
            uninstallPackage = executeCommand(SUBPROCESS_TIMEOUT, "sudo", "rpm", "-e", "--nodeps", PRESTO_PACKAGE);
        }
        if (uninstallPackage != 0) {
            throw new PrestoManagerException(format("Failed to uninstall package: %s", PRESTO_PACKAGE), uninstallPackage);
        }
        LOGGER.debug("Successfully uninstalled Presto");
    }
}

@@ -1210,39 +1210,37 @@
 e final boolean 
-disable
+check
 DependencyChecki
@@ -1232,25 +1232,19 @@
 ependenc
-yChecking
+ies
 ;%0A    pr
@@ -1342,23 +1342,21 @@
 boolean 
-disable
+check
 Dependen
@@ -1356,25 +1356,19 @@
 ependenc
-yChecking
+ies
 , boolea
@@ -1460,23 +1460,21 @@
    this.
-disable
+check
 Dependen
@@ -1474,25 +1474,19 @@
 ependenc
-yChecking
+ies
  = requi
@@ -1495,23 +1495,21 @@
 NonNull(
-disable
+check
 Dependen
@@ -1509,25 +1509,19 @@
 ependenc
-yChecking
+ies
 );%0A     
@@ -1752,15 +1752,13 @@
 Rpm(
-disable
+check
 Depe
@@ -1766,17 +1766,11 @@
 denc
-yChecking
+ies
 , fo

Rename variable disableDependencyChecking