package com.github.aureliano.achmed.os.service;

import org.apache.log4j.Logger;

import com.github.aureliano.achmed.command.CommandFacade;
import com.github.aureliano.achmed.command.CommandResponse;
import com.github.aureliano.achmed.exception.ServiceResourceException;
import com.github.aureliano.achmed.helper.StringHelper;

public class RedHatService extends LinuxService {

	private static final Logger logger = Logger.getLogger(RedHatService.class);
	private static final String SERVICE = "/sbin/service";
	private static final String CHKCONFIG = "/sbin/chkconfig";
	
	public RedHatService() {
		super();
	}
	
	@Override
	public CommandResponse start() {
		if (this.isRunning()) {
			logger.debug("Service " + super.properties.getName() + " is already running.");
			return null;
		}
		
		if (StringHelper.isEmpty(super.properties.getBinary())) {
			return CommandFacade.executeCommand(SERVICE, super.properties.getName(), "start");
		}
		
		return super.start();
	}
	
	@Override
	public CommandResponse stop() {
		if (!this.isRunning()) {
			logger.debug("Service " + super.properties.getName() + " is not running.");
			return null;
		}
		
		if (StringHelper.isEmpty(super.properties.getBinary())) {
			return CommandFacade.executeCommand(SERVICE, super.properties.getName(), "stop");
		}
		
		return super.stop();
	}
	
	@Override
	public CommandResponse restart() {
		if ((super.properties.getHasRestart() != null) && (super.properties.getHasRestart())) {
			return CommandFacade.executeCommand(SERVICE, super.properties.getName(), "restart");
		}
		
		return super.restart();
	}
	
	@Override
	public boolean isRunning() {
		if ((super.properties.getHasStatus() != null) && (super.properties.getHasStatus())) {
			CommandResponse res = CommandFacade.executeCommand(SERVICE, super.properties.getName(), "status");
			return (res.getExitStatusCode() == 0);
		}
		
		return super.isRunning();
	}

	@Override
	public CommandResponse enableBootstrap() {
		CommandResponse res = CommandFacade.executeCommand(CHKCONFIG, "--add", super.properties.getName());
		if (!res.isOK()) {
			throw new ServiceResourceException(res);
		}
		
		return CommandFacade.executeCommand(CHKCONFIG, super.properties.getName(), "on");
	}

	@Override
	public CommandResponse disableBootstrap() {
		return CommandFacade.executeCommand(CHKCONFIG, "--level", "0123456", super.properties.getName(), "off");
	}

	@Override
	public boolean isEnabledInBootstrap() {
		CommandResponse res = CommandFacade.executeCommand(CHKCONFIG, super.properties.getName());
		return res.isOK();
	}
}
@@ -841,38 +841,53 @@
 Binary())) %7B%0A%09%09%09
-return
+CommandResponse res =
  CommandFacade.e
@@ -941,37 +941,70 @@
 (), %22start%22);%0A%09%09
-%7D%0A
+%09super.assertServiceIsRunning();%0A%09
 %09%09%0A
+%09
 %09%09return super.s
@@ -1000,21 +1000,108 @@
 urn 
-super.start()
+res;%0A%09%09%7D%0A%09%09%0A%09%09CommandResponse res = super.start();%0A%09%09super.assertServiceIsRunning();%0A%09%09%0A%09%09return res
 ;%0A%09%7D
@@ -1331,38 +1331,53 @@
 Binary())) %7B%0A%09%09%09
-return
+CommandResponse res =
  CommandFacade.e
@@ -1434,29 +1434,62 @@
  %22stop%22);%0A%09%09
-%7D%0A
+%09super.assertServiceIsStopped();%0A%09
 %09%09%0A
+%09
 %09%09return sup
@@ -1489,20 +1489,107 @@
 urn 
-super.stop()
+res;%0A%09%09%7D%0A%09%09%0A%09%09CommandResponse res = super.stop();%0A%09%09super.assertServiceIsStopped();%0A%09%09%0A%09%09return res
 ;%0A%09%7D
@@ -1731,22 +1731,37 @@
 )) %7B%0A%09%09%09
-return
+CommandResponse res =
  Command
@@ -1829,29 +1829,62 @@
 estart%22);%0A%09%09
-%7D%0A
+%09super.assertServiceIsRunning();%0A%09
 %09%09%0A
+%09
 %09%09return sup
@@ -1884,23 +1884,110 @@
 urn 
-super.restart()
+res;%0A%09%09%7D%0A%09%09%0A%09%09CommandResponse res = super.restart();%0A%09%09super.assertServiceIsRunning();%0A%09%09%0A%09%09return res
 ;%0A%09%7D
@@ -2530,28 +2530,27 @@
 %0A%09%09%7D%0A%09%09%0A%09%09re
-turn
+s =
  CommandFaca
@@ -2609,24 +2609,86 @@
 e(), %22on%22);%0A
+%09%09super.assertServiceIsEnabledInBootstrap();%0A%09%09%0A%09%09return res;%0A
 %09%7D%0A%0A%09@Overri
@@ -2737,22 +2737,37 @@
 p() %7B%0A%09%09
-return
+CommandResponse res =
  Command
@@ -2780,32 +2780,36 @@
 .executeCommand(
+%0A%09%09%09
 CHKCONFIG, %22--le
@@ -2858,17 +2858,83 @@
 ), %22off%22
-)
+%0A%09%09);%0A%09%09super.assertServiceIsDisabledInBootstrap();%0A%09%09%0A%09%09return res
 ;%0A%09%7D%0A%0A%09@

Check whether service actions worked.