#!/bin/sh
###############################################################################
##
##  rsynctool
##
##
##  2007-01-16  Andreas Vogel (omega)
##
###############################################################################

cmd=$1
dir=$2

###############################################################################
##
## Create an intial rsyncd config file...
##
###############################################################################
RsyncConfig () {
    cfgFile=$1 ; mode=${2:-check}

    if [ "$mode" != force -a -e $cfgFile ] ; then
        return 0
    fi

    echo "RSYNCD config: creating new config $cfgFile"

    lan_addr=$(ifconfig eth1 | grep 'inet addr:' | tr -s ':' ' ' | awk '{print $3 ; exit}')
    lan_mask=$(ifconfig eth1 | grep 'inet addr:' | tr -s ':' ' ' | awk '{print $7 ; exit}')

    cat >$cfgFile <<-EOT
	#######################################################################
	##
	##  $cfgFile - RSYNCD config file
	##
	#######################################################################

	gid = users
	read only = true
	transfer logging = false
	log file = /var/log/rsyncd.log
	pid file = /var/run/rsyncd.pid
	hosts allow = $lan_addr:$lan_mask

EOT

    for dir in /raid/data/* ; do
        [ ! -d "$dir" ] && continue
        [ "$dir" = "lost+found" ] && continue
        base=$(basename "$dir")
        cat >>$cfgFile <<-EOT

	[$base]
	    comment         = $base Files
	    path            = $dir
	    read only       = no
	    list            = yes
	    uid             = nobody
	    gid             = nogroup
EOT
    done
}

###############################################################################
##
##  Main
##
###############################################################################

if [ ! -d "$dir" ] ; then
    echo "ERROR: cannot open directory '$dir'!"
    exit 1
fi

case "$cmd" in
    checkconfig)
        RsyncConfig "$dir/rsyncd.conf" check
        ;;

    newconfig)
        RsyncConfig "$dir/rsyncd.conf" force
        ;;

    *)
        echo "Usage: $0 {checkconfig|newconfig} dir"
        exit 1
        ;;
esac

exit 0
