#!/bin/sh
#
# chkconfig: 345 41 60
#
# the template file of init.d script
# start or stop services

############
# CHANGELOG
#===========
# 2010/10/26    CRMS00269921    Thanh lam NGUYEN
#               Do not start in upgrade mode
#
# 2010/05/27    Creation	Thanh lam NGUYEN
################


. /etc/init.d/rc.config

bin=/sbin/linkd
pidfile=/var/run/linkd.pid
lockfile=/var/lock/subsys/linkd
args="-p $pidfile"

start()
{
    local RET=0

    : "Already running ?"
    [[ -f $pidfile ]] && [[ -e /proc/$(cat $pidfile) ]] && RET=1 && return $RET

    : "Forward the xflag to $bin"
    if [[ "${-//x}" != "$-" ]]; then
        sh -x $bin $args
    else
        $bin $args
    fi
    RET=$?
    if [[ $RET -eq 0 ]]; then
        touch $lockfile
    else
        [[ -f $pidfile ]] && [[ -e /proc/$(cat $pidfile) ]] && kill $(cat $pidfile)
        rm -f $pidfile >/dev/null 2>&1
    fi
    return $RET
}

stop()
{
    local RET=0

    if [[ -f $pidfile ]]; then
        kill $(cat $pidfile)
        RET=$?
        if [[ $RET -eq 0 ]]; then
            rm -f $pidfile $lockfile >/dev/null 2>&1
        fi
    fi
    return $RET
}

condrestart()
{
    local RET=0

    if [ -f $lockfile ]; then
        stop
        start
        RET=$?
    fi
    return $RET
}

restart()
{
    local RET=0

    stop
    start
    RET=$?
    return $RET
}

# CRMS00269921
#if upgrade in progress then exit
upgrade_check(){
    STATUS=/usr/sbin/upgd_status
    if [ -x $STATUS ]; then
        $STATUS >/dev/null 2>&1
        if [ $? -eq 1 ]; then
           echo "upgrade in progress: not starting"
           exit 0
        fi
    fi
}
# CRMS00269921 END

param=$1
shift
case "$param" in
    start|"") : "$param"
# CRMS00269921
        upgrade_check
# CRMS00269921 END
        start "$@"
        RETVAL=$?
    ;;
    stop) : "$param"
        stop "$@"
        RETVAL=$?
    ;;
    restart) : "$param"
        restart "$@"
        RETVAL=$?
    ;;
    condrestart) : "$param"
        condrestart "$@"
        RETVAL=$?
    ;;
    *) : "$param"
        echo "Usage $0 {start|stop|restart|condrestart}" >&2
        RETVAL=1
    ;;
esac
exit $RETVAL
