#!/bin/bash
#############
#Date 2022/6/8
#mail it-arch
#############
echo "##########################################"
echo "Auto Install Redis                      ##"
echo "Press Ctrl + C to cancel                ##"
echo "Any key to continue                     ##"
echo "##########################################"
read -n 1
/etc/init.d/iptables status >/dev/null 2>&1
if [ $? -eq 0 ]
then
iptables -I INPUT -p tcp --dport 6379 -j ACCEPT && 
iptables-save >/dev/null 2>&1
else
    echo -e "\033[32m iptables is stopd\033[0m"
fi
sed -i "s/SELINUX=enforcing/SELINUX=disabled/"  /etc/selinux/config
setenforce 0
################################
if [ -d /tools ];then
    cd /tools
else
    mkdir /tools && cd /tools
fi
yum -y install wget
yum groupinstall "Development tools" -y
wget -c -O /tools/redis http://download.redis.io/releases/redis-3.0.2.tar.gz
tar -zxvf redis
cd redis-3.0.2
wget http://downloads.sourceforge.net/tcl/tcl8.6.0-src.tar.gz
tar -zxvf tcl8.6.0-src.tar.gz
cd tcl8.6.0
cd unix &&
./configure --prefix=/usr \
            --mandir=/usr/share/man \
            --without-tzdata \
            $([ $(uname -m) = x86_64 ] && echo --enable-64bit) &&
make &&

sed -e "s@^\(TCL_SRC_DIR='\).*@\1/usr/include'@" \
    -e "/TCL_B/s@='\(-L\)\?.*unix@='\1/usr/lib@" \
    -i tclConfig.sh

make install &&
make install-private-headers &&
ln -v -sf tclsh8.6 /usr/bin/tclsh &&

make
cd src/
make test

make install
cd ..
cp redis.conf /etc/
cat > /etc/init.d/redis <<EOF
#!/bin/bash
#
# Init file for redis
#
# chkconfig: - 80 12
# description: redis daemon
#
# processname: redis
# config: /etc/redis.conf
# pidfile: /var/run/redis.pid
source /etc/init.d/functions
#BIN="/usr/local/bin"
BIN="/usr/local/bin"
CONFIG="/etc/redis.conf"
PIDFILE="/var/run/redis.pid"
### Read configuration
[ -r "$SYSCONFIG" ] && source "$SYSCONFIG"
RETVAL=0
prog="redis-server"
desc="Redis Server"
start() {
        if [ -e $PIDFILE ];then
            echo "$desc already running...."
            exit 1
        fi
        echo -n $"Starting $desc: "
        daemon $BIN/$prog $CONFIG
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
        return $RETVAL
}
stop() {
        echo -n $"Stop $desc: "
        killproc $prog
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog $PIDFILE
        return $RETVAL
}
restart() {
        stop
        start
}
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        restart
        ;;
  condrestart)
        [ -e /var/lock/subsys/$prog ] && restart
        RETVAL=$?
        ;;
  status)
        status $prog
        RETVAL=$?
        ;;
  *)
        echo $"Usage: $0 {startstoprestartcondrestartstatus}"
        RETVAL=1
esac
exit $RETVAL
EOF
chmod 755 /etc/init.d/redis
sed -i "s/daemonize no/daemonize yes/g" /etc/redis.conf
chkconfig redis on
service redis start
echo -e "\033[32m Redis install success\033[0m"