#!/bin/sh

port=6379
maxport=6400
while netstat -ln | grep -q :$port.*LISTEN
do
	if [ $port -eq $maxport ]
	then
		exit 1
	fi
	port=$(($port + 1))
done

sed -e 's!^save!#&!g' \
    -e 's!^dir .*!dir '"$PWD"'!g' \
	-e 's!^pidfile .*!pidfile '"$PWD"'/redis.pid!g' \
	-e 's!^logfile .*!logfile /dev/stdout!g' \
	-e 's!^port .*!port '$port'!g' \
	/etc/redis/redis.conf | redis-server -
echo $port
exit 0
