#!/bin/sh
###################################################
#Author: Created by randolph 2016/10
#Blog:http://randolph.blog.51cto.com
#Function: This scripts function is install haproxy
#Version:4.1.2
###################################################
#Source function library.
. /etc/init.d/functions

TOOLS=/home/zhangyiling/tools/haproxy
BACKAGE01=$TOOLS/haproxy-1.4.24.tar.gz
BACKAGE02=$TOOLS/haproxy_config.tar.gz

#Require root to run this script.
uid=`id|awk -F "[=(]+" '{print $1}'`
if [ $uid -ne 0 ];then
  action "You need to be root to perform the script." /bin/false
  exit 1
fi
#check $TOOLS.
[ -d $TOOLS ] || {
    mkdir -p $TOOLS
}
#start install.
LANG=en
yum install -y gcc gcc-devel
[ -f /etc/init.d/httpd ] && {
    /etc/init.d/httpd stop
} 
if [ -f $BACKAGE01 ]; then
    tar xf $BACKAGE01 -C $TOOLS
else
    echo "pls check install backage."
fi
cd $TOOLS/haproxy-1.4.24
make TARGET=linux26 ARCH=x86_64
make PREFIX=/application/haproxy-1.4.24 install
ln -s  /application/haproxy-1.4.24/ /application/haproxy
#The kernel forwarding.
sed  -i 's#net.ipv4.ip_forward = 0#net.ipv4.ip_forward = 1#g' /etc/sysctl.conf
#create user.
useradd haproxy -s /sbin/nologin -M
#configure LOG
echo -ne "#Haproxy\nlocal0.* /application/haproxy-1.4.24/logs/haproxy.log" >>/etc/rsyslog.conf 
#configure conf.
tar xf $BACKAGE02 -C /
/etc/init.d/rsyslog restart
/application/haproxy/bin/haproxyd restart