'''
daemontools service module. This module will create daemontools type
service watcher.
This module is states.service compatible so it can be used to maintain
service state via provider interface:

  - provider: daemontools
'''

# Import python libs
import os
import re

# Function alias to not shadow built-ins.
__func_alias__ = {
    'reload_': 'reload'
}

if os.path.exists('/service'):
    SERVICE_DIR = "/service"
elif os.path.exists('/var/service'):
    SERVICE_DIR = "/var/service"


def _service_path(name):
    '''
    build service path
    '''
    return '{0}/{1}'.format(SERVICE_DIR, name)


#-- states.service  compatible args
def start(name):
    '''
    Starts service via daemontools

    CLI Example::

        salt '*' daemontools.start <service name>
    '''
    __salt__['file.remove']('{0}/down'.format(_service_path(name)))
    cmd = 'svc -u {0}'.format(_service_path(name))
    return not __salt__['cmd.retcode'](cmd)


#-- states.service compatible args
def stop(name):
    '''
    Stops service via daemontools

    CLI Example::

        salt '*' daemontools.stop <service name>
    '''
    __salt__['file.touch']('{0}/down'.format(_service_path(name)))
    cmd = 'svc -d {0}'.format(_service_path(name))
    return not __salt__['cmd.retcode'](cmd)


def term(name):
    '''
    Send a TERM to service via daemontools

    CLI Example::

        salt '*' daemontools.term <service name>
    '''
    cmd = 'svc -t {0}'.format(_service_path(name))
    return not __salt__['cmd.retcode'](cmd)


#-- states.service compatible
def reload_(name):
    '''
    Wrapper for term()

    CLI Example::

        salt '*' daemontools.reload <service name>
    '''
    term(name)


#-- states.service compatible
def restart(name):
    '''
    Restart service via daemontools. This will stop/start service

    CLI Example::

        salt '*' daemontools.restart <service name>
    '''
    ret = 'restart False'
    if stop(name) and start(name):
        ret = 'restart True'
    return ret


#-- states.service compatible
def full_restart(name):
    '''
    Calls daemontools.restart() function

    CLI Example::

        salt '*' daemontools.full_restart <service name>
    '''
    restart(name)


#-- states.service compatible
def status(name, sig=None):
    '''
    Return the status for a service via daemontools, return pid if running

    CLI Example::

        salt '*' daemontools.status <service name>
    '''
    cmd = 'svstat {0}'.format(_service_path(name))
    ret = __salt__['cmd.run_stdout'](cmd)
    match = re.search(r'\(pid (\d+)\)', ret)
    try:
        pid = match.group(1)
    except Exception:
        pid = ''
    return pid


def get_all():
    '''
    Return a list of all available services

    CLI Example::

        salt '*' daemontools.get_all
    '''
    #- List all daemontools services in
    return sorted(os.listdir(SERVICE_DIR))

@@ -266,16 +266,86 @@
 ort re%0A%0A
+# Import salt libs%0Afrom salt.exceptions import CommandExecutionError%0A%0A
 # Functi
@@ -424,35 +424,64 @@
 oad'%0A%7D%0A%0A
-if os.path.exists('
+VALID_SERVICE_DIRS = %5B%0A    '/service',%0A    '/var
 /service
@@ -485,42 +485,94 @@
 ice'
-):
+,
 %0A    
-SERVICE_DIR = %22/service%22%0Ael
+'/etc/service',%0A%5D%0ASERVICE_DIR = None%0Afor service_dir in VALID_SERVICE_DIRS:%0A    
 if o
@@ -589,29 +589,30 @@
 sts(
-'/var/
 service
-'):%0A
+_dir):%0A   
     
+ 
 SERV
@@ -625,22 +625,122 @@
 R = 
-%22/var/service%22
+service_dir%0A        break%0Aif not SERVICE_DIR:%0A    raise CommandExecutionError(%22Could not find service directory.%22)
 %0A%0A%0Ad

Add Debian/Ubuntu package compatible service dir to daemontools module