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

############
# CHANGELOG
#===========
# 2011/03/03    CRMS00296013    Michel Sulyan
#               Increase the delay between popup to 60s
#
# 2010/11/26    CRMS00273250    Thanh lam NGUYEN
#               Increase the delay between popup to 30s
#
# 2010/10/26    CRMS00269921    Thanh lam NGUYEN
#               Do not start in upgrade mode
#
# 2010/08/24    CRMS00255212    Thanh lam NGUYEN
#               Fix the start/stop of ipdad
#
# 2010/05/06    CRMS00209257	Thanh lam NGUYEN
#               Creation
################


. /etc/init.d/rc.config

# CRMS00296013
# CRMS00273250
delaytime=60
# CRMS00273250 END
# CRMS00296013 END

bin=/sbin/ipdad
args=$delaytime
pidfile=/var/run/dupip.pid
lockfile=/var/lock/subsys/dupip

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

    : "Forward the xflag to $bin"
    if [[ "${-//x}" != "$-" ]]; then
        ( trap "" HUP && exec sh -x $bin $args >/dev/console 2>&1 ) &
# CRMS00255212
        echo "$!" > $pidfile
# CRMS00255212 END
    else
# CRMS00255212
        ( trap "" HUP && exec chpst -P $bin $args & echo "$!" > $pidfile ) &
# CRMS00255212 END
    fi
    RETVAL=$?
    if [[ $RETVAL -eq 0 ]]; then
        touch $lockfile
    else
# CRMS00255212
        [[ -f $pidfile ]] && [[ -e /proc/$(cat $pidfile) ]] && kill -TERM -$(cat $pidfile)
# CRMS00255212 END
        rm -f $pidfile >/dev/null 2>&1
    fi
    return $RETVAL
}


stop() {
    if [[ -f $pidfile ]]; then
# CRMS00255212
        kill -TERM -$(cat $pidfile)
# CRMS00255212 END
        RETVAL=$?
        if [[ $RETVAL -eq 0 ]]; then
            rm -f $pidfile $lockfile >/dev/null 2>&1
        fi
    fi
    return $RETVAL
}

condrestart() {
    if [ -f $lockfile ]; then
        stop
        start
    fi
}

restart() {
    stop
    start
}

# 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

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

exit $RETVAL
