#!/bin/bash
#version choose
OS=`sed -n '1p' /etc/issue|awk '{print $1}'`
if [ $OS = "Ubuntu" -o $OS = "Debian" ];then
apt-get  -y install gcc make tcl ruby unzip
elif [ $OS = "Suse" ];then
zypper -y install gcc make tcl ruby unzip
else
yum -y install gcc  make automake tcl ruby ruby-rdoc unzip
fi

#Install redis
if [ ! -d /home/tools/ ];then
mkdir -p /home/tools
else
rm -rf /home/tools && mkdir -p /home/tools
fi

cd /home/tools
wget https://github.com/antirez/redis/archive/3.0.0-rc1.tar.gz
tar zxvf 3.0.0-rc1.tar.gz
if [ $? -eq 0 ];then
cd redis-3.0.0-rc1/ && make && make  PREFIX=/opt/redis  install
fi

#Config redis
mkdir -p /opt/redis/etc
mkdir -p /opt/redis/run
mkdir -p /opt/redis/data
mkdir -p /opt/redis/log
mkdir -p /opt/redis/data/6379

\cp redis.conf /opt/redis/etc/redis.conf
\cp /opt/redis/etc/redis.conf  /opt/redis/etc/redis_6379.conf
cd /home/tools/redis-3.0.0-rc1/src
\cp redis-trib.rb /opt/redis/bin/

sed -i '37s/daemonize no/daemonize yes/' /opt/redis/etc/redis_6379.conf
sed -i '41s#/var/run/redis.pid#/opt/redis/run/redis_6379.pid#' /opt/redis/etc/redis_6379.conf
sed -i '187s#dir ./#dir /opt/redis/data/6379#' /opt/redis/etc/redis_6379.conf
sed -i '103s#logfile ""#logfile /opt/redis/log/redis_6379.log#' /opt/redis/etc/redis_6379.conf
sed -i 's/appendonly no/appendonly yes/g' /opt/redis/etc/redis_6379.conf
sed -i 's/appendfilename "appendonly.aof"/appendfilename "appendonly-6379.aof"/g' /opt/redis/etc/redis_6379.conf
sed -i 's#\# cluster-enabled yes#cluster-enabled yes#g' /opt/redis/etc/redis_6379.conf
sed -i 's#\# cluster-node-timeout 15000#cluster-node-timeout 5000#g' /opt/redis/etc/redis_6379.conf
sed -i 's#\# cluster-config-file nodes-6379.conf#cluster-config-file nodes-6379.conf#g' /opt/redis/etc/redis_6379.conf

#Adding memory optimization parameters
echo "vm.overcommit_memory = 1" >> /etc/sysctl.conf
sysctl -p

#Config start/stop script
cat > /etc/init.d/redis_6379 <#!/bin/bash
EXEC="/opt/redis/bin/redis-server"
PIDFILE="/opt/redis/run/redis_6379.pid"
CONF="/opt/redis/etc/redis_6379.conf"
REDISPORT="6379"

case "\$1" in
    start)
        if [ -f \$\$PIDFILE ]
        then
                echo "\$PIDFILE exists, process is already running or crashed."
        else
                echo "Starting Redis server..."
                \$EXEC \$CONF
        fi
        ;;
    stop)
        if [ ! -f \$PIDFILE ]
        then
                echo "\$PIDFILE does not exist, process is not running."
        else
                PID=\$(cat \$PIDFILE)
                echo "Stopping Redis Server…"
                while [ -x /proc/\${PID} ]
                do
                    kill -9 \$PID
                    sleep 1
                done
                echo "Redis stopped."
        fi
        ;;
    *)
        echo "Usage: \$0 {start|stop}" >&2
        exit 1
        ;;
esac
EOF

#Setup redis
chmod +x /etc/init.d/redis_6379 && /etc/init.d/redis_6379 start
ln -s /opt/redis/bin/*  /usr/bin


#Installrubygem
cd /home/tools/
wget http://production.cf.rubygems.org/rubygems/rubygems-2.4.2.zip
if [ $? -eq 0 ];then
unzip rubygems-2.4.2.zip
fi
cd rubygems-2.4.2/
ruby setup.rb
if [ $? -eq 0 ];then
gem install redis --version 3.0.0
fi