#!/bin/sh
#
# Author: Michael Conrad <do5mc at aprs4r.org>
#

# ruby executable
if [ -z "$RUBY_BIN" ]; then 
    
    # default 
    RUBY_BIN=/usr/bin/ruby

    # check ruby1.9 binary (openwrt/debian lenny)
    if [ -x /usr/bin/ruby1.9 ]; then 
	RUBY_BIN=/usr/bin/ruby1.9
    else 
        # check for ruby1.9.X (debian squeeze/ubuntu >= 10.04)
	for version in `seq 0 9`; do 
	    if [ -x /usr/bin/ruby1.9.${version} ]; then 
		RUBY_BIN=/usr/bin/ruby1.9.${version}
	    fi
	done
    fi
fi

# set etc path
[ -z "$APRS4R_ETC" ] && APRS4R_ETC=/etc/aprs4r

# set log path 
[ -z "$APRS4R_LOG" ] && APRS4R_LOG=/var/log/aprs4r

# set lib dir
if [ -z "$APRS4R_LIB" ]; then
    APRS4R_LIB=/usr/lib/ruby/vendor_ruby
fi


LOG_FILE=$APRS4R_LOG/aprs4r-config.log


write_log() 
{ 
    timestamp=`date +'%Y-%m-%d %H:%M:%S'`

    echo "[$1] $timestamp: $2" >> $LOG_FILE 
}


for file in `ls -1 $APRS4R_ETC/*.yaml`; do 
    echo "checking configuration file $file" 
    write_log "LOG", "configuration update for file $file"

    $RUBY_BIN $APRS4R_LIB/aprs4r_config.rb $file >> $LOG_FILE
done

