#!/bin/bash
#
# Guralp Configuration System
#   Copyright (c) 2007-2010 Guralp Systems Ltd. All rights reserved.
#
#   Data service support functions



# Check whether a service is enabled in either autostart
gcs_svc_enabled() {
    local name="$1"
    local sel="$2"
    local svc

    svc="${name}.${sel}"
    [ "${sel}" == "default" ] && svc="${name}"

    grep -q "^${svc}\$" "${INITDIR}/autostart" && return 0
    grep -q "^${svc}\$" "${INITDIR2}/autostart" && return 0
    return 1
}



# Read the standard service template variables
gcs_svc_read() {
    local my_o_enable

    # Sanity check the service name
    if [ ! -r $SRVBASE/info ]
    then
        gcs_err fatal "Cannot find base service information for $sel"
        return 1
    fi

    # Load standard info for service
    . $SRVBASE/info

    # Generate default settings
    echo d_name=$name
    if [ "$sel" = "default" ]
    then
        if gcs_truefalse "${ALLOW_MULTI:-false}"
        then
            gcs_var d_desc "$DESC (default instance)"
        else
            gcs_var d_desc "$DESC"
        fi
    else
        gcs_var d_desc "$DESC (instance $(expr $sel + 1))"
    fi

    # Enable always defaults to False so that enabling the service is seen as a
    # config change to create the config file even if all the defaults are used
    gcs_var d_enable False
    gcs_var d_delete False

    # Now the current user settings if any
    if [ -r $CTLFILE ]
    then
        desc=$(gcs_get_varf DESC $CTLFILE)
        [ -n "$desc" ] && gcs_var o_desc "$desc"
    else
        gcs_var always_submit True
    fi

    if [ "${sel}" == "default" ] && grep -q "^${name}\$" "${INITDIR}/autostart"
    then
        gcs_var "change_enable" "False"
    else
        gcs_var "change_enable" "True"
    fi

    if gcs_svc_enabled "${name}" "${sel}"
    then
        gcs_var "o_enable" "True"
    else
        gcs_var "o_enable" "False"
    fi

    if [ "${sel}" == "default" ]
    then
        gcs_var "can_delete" "False"
    else
        gcs_var "can_delete" "True"
    fi

    return 0
}



# Check standard service control/info vars
gcs_svc_check() {
    enable=$(gcs_get_var new_enable)
    desc=$(gcs_get_var new_desc)
}



# Reload a service post write of config file  
gcs_svc_reload() {                            
    local svc_name=$1                                         

    if [ -n "$2" ]
    then
        # If a selection is specified, parse it to get service name
        case "$2" in
            default) ;;
        *) svc_name="$svc_name.$2" ;;
        esac
    fi
                                                
    # Call reload - to reload the service's config. Each service can override this.
    # This will only reload the config if the service is running.                  
    svc "$svc_name" "reload" > /dev/null 2>&1
}                                                                                

# These two call a helper via suexec as they need to manipulate root owned
# autostart list and init scripts.
helper="$(dirname $0)/svc_funcs_helper.sh"


# Update or create a service file from the service description template
gcs_update_svc() {

    suexec $helper update "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8"
}


# Delete a configured service
gcs_svc_delete() {                 
    local name=$1
    local sel=$2                            

    if [ $sel = "default" ]
    then
        gcs_err "msg" "Cannot delete default service instance"
        exit 1
    fi

    suexec $helper delete "$1" "$2"
    exit 0                                
}


# vim: ts=4:sw=4:expandtab
