#!/bin/sh

. /etc/init.d/functions

service="rsync"
basedir="/var/run"
lockdir="/var/lock"
execute="/usr/bin/rsync"
cfgpath="/etc/rsyncd.conf"
passfile="/etc/rsyncd.passwd"
exeopts=" --daemon --config=${cfgpath}"

service_status(){
        ps ax | grep $execute | grep -v grep > /dev/null 2>&1
        if [ $? -eq 0 ]; then
                echo $service "running"
                exit 1

        else
                echo $service "stopped"
        fi
}

service_prepare(){
	[ ! -d $basedir ] && mkdir -p $basedir
	[ ! -d $lockdir ] && mkdir -p $lockdir
	chmod 600 ${passfile}
	/sbin/svrcfg-all -c rsync
	# ssh 免密传输...
	rm -fr /var/run/rsyncd.pid
}

service_start(){
    service_prepare
    runprocess $service $execute $exeopts
}

service_stop(){
	stoprocess $service $execute $exeopts
}

service_restart(){
	service_stop
	sleep 1
	service_start
}

case $1 in
        start)
                service_start
                exit 0 ;;
        stop)
                service_stop
                exit 0 ;;
        restart)
                service_restart
                exit 0 ;;
        status)
                service_status
                exit 0 ;;
        *)
                echo "Usage: `basename $0` {start|stop|restart|status}"
                exit 1 ;;
esac
