#!/usr/bin/perl
### BEGIN INIT INFO
# Provides:          ebox 
# Required-Start:    networking 
# Required-Stop:     networking
# Default-Start:     2 3 4 5
# Default-Stop:      0 6
# Short-Description: eBox platform (network services framework) 
### END INIT INFO

use strict;
use warnings;

use EBox::Global;
use EBox;
use EBox::Sudo;
use Error qw(:try);

EBox::init();
my $global = EBox::Global->getInstance(1);

sub usage {
	print "Usage: $0 start|stop|status|restart\n";
	print "       $0 <module> start|stop|restart\n";
	exit 1;
}

sub start() {
	my @names = grep (! /^apache$/ ,@{$global->modNames});
	push(@names, 'apache');

	foreach my $modname (@names) {
	  moduleAction($modname, 'restartService', 'start');
	}
}

sub stop() {
	my @names = grep (! /^apache$/ ,@{$global->modNames});
	push(@names, 'apache');

	foreach my $modname (@names) {
	  moduleAction($modname, 'stopService', 'stop');
	}
}


sub moduleAction
{
  my ($modname, $action, $actionName) = @_;
  my $mod = $global->modInstance($modname);
  if (!defined $mod) {
    print STDERR "$modname is not a valid module name\n";
    exit 2;
  }

  if ($actionName eq 'start' and $modname eq 'network' and $mod->isEnabled()) {
      try {
        EBox::Sudo::root("/etc/init.d/networking stop");
      } otherwise {};
  }

  my $success;
  my $errorMsg;
  try {
    $mod->$action();
    $success = 1;
  } 
  catch EBox::Exceptions::Base with {
      my $ex = shift;
      $success = 0;
      $errorMsg =  $ex->text;
  };

  printModuleMessage($modname, $actionName, $success, $errorMsg);
}

sub status
{
  my ($modname, $action, $actionName) = @_;
  my $mod = $global->modInstance($modname);
  if (!defined $mod) {
    print STDERR "$modname is not a valid module name\n";
    exit 2;
  }   
  
  my $status;
  my $msg = "EBox: status module $modname:\t\t\t";
  if ($mod->isEnabled()) {
   print STDOUT $msg . "[ ENABLED ]\n";
   exit 0;
  } else {
   print STDOUT $msg . "[ DISABLED ]\n";
   exit 1;
  }

}

sub printModuleMessage
{
  my ($modname, $action, $success, $errorMsg) = @_;

  my $successMsg = $success ?  '   [ OK ]' : '[ ERROR ]';

  print STDOUT "EBox: $action $modname:\t\t\t$successMsg\n";

  if ($errorMsg) {
    print STDERR $errorMsg, "\n";
  }
}


sub moduleRestart {
  my ($modname) = @_;

  moduleAction($modname, 'restartService', 'restart');
}

sub moduleStop {
  my ($modname) = @_;
 
  moduleAction($modname, 'stopService', 'stop');
}

sub main
{
  if (@ARGV == 1) {
    if ($ARGV[0] eq 'start') {
      start();
    } 
    elsif ($ARGV[0] eq 'restart') {
      stop();
      start();
    }
    elsif ($ARGV[0] eq 'force-reload') {
      stop();
      start();
    } 
    elsif ($ARGV[0] eq 'stop') {
      stop();
    } else {
      usage();
    }
  } 
  elsif (@ARGV == 2) {
    # action upon one module mode
    my ($modName, $action) = @ARGV;

    if (($action eq 'restart') or ($action eq 'start')) {
      moduleRestart($modName);
    } 
    elsif ($action eq 'stop') {
      moduleStop($modName);
    } elsif ($action eq 'status') {
      status($modName);
    } else {
      usage();
    }
  } 
  else {
    usage();
  }
}

main();
1;
