#!/bin/sh
#
# LCM daemon helper script
#

if command -v ulcmd > /dev/null; then
    UI="ulcmd"
elif command -v ck-ui > /dev/null; then
    UI="ck-ui"
elif command -v udr-ui > /dev/null; then
    UI="udr-ui"
else
    echo "UI daemon not found"
    exit 1
fi

lcm_daemon_ctrl() {
    [ $# -eq 1 ] || return 1
    local action="$1"
    if command -v service > /dev/null; then
        service ${UI} ${action}
    elif [ -f "/etc/init.d/${UI}" ]; then
        /etc/init.d/${UI} ${action}
    elif command -v systemctl > /dev/null; then
        systemctl ${action} ${UI}
    else
        return 1
    fi
}

if [ "$#" = 0 ]; then
    echo "Parameters missing"
    ${UI} --help
    exit 1
fi

if [ "$1" = "restart" ]; then
    lcm_daemon_ctrl restart && exit 0
    echo "Failed to restart ${UI}"
    exit 1
fi

${UI} $@
