#!/bin/bash
#Date 2022/1/10
#mail it-arch
[ $(id -u) != "0" ] && echo "Error: You must be root to run this script" && exit 1
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
clear
echo "##########################################"
echo "#					       #"
echo "#   Auto compile install keepalived      #"
echo "#					       #"
echo "##########################################"
echo "1 Install keepalived-1.3.5"
echo "2 Install keepalived-1.3.0"
echo "3 Install keepalived-1.2.24"
echo "4 EXIT"
read -p "Please input your choice:" I
if [ "$I" == "1" ];then
        URL="http://www.keepalived.org/software/keepalived-1.3.5.tar.gz"
	VER="keepalived-1.3.5"
elif [ "$I" == "2" ];then
        URL="http://www.keepalived.org/software/keepalived-1.3.0.tar.gz"
	VER="keepalived-1.3.0"
elif [ "$I" == "3" ];then
        URL="http://www.keepalived.org/software/keepalived-1.2.24.tar.gz"
	VER="keepalived-1.2.24"
elif [ "$I" == "4" ];then
        echo "you choce channel!"
        exit 1;
else
        echo "input Error! Place input{1|2|3|4}"
        exit 0;
fi
echo -e "\033[32m You choice install $VER.Install dirname:$DIR \033[0m"
echo -e "\033[32m Press any key to start install $VER... \033[0m"
read -n 1
if [ -d /tools ];then
	cd /tools
else
	mkdir /tools && cd /tools
fi
yum install -y gcc gcc-c++ popt-devel openssl openssl-devel libssl-dev libnl-devel popt-devel kernel kernel-devel libnfnetlink-devel
linux=`ls /usr/src/kernels`
ln -s /usr/src/kernels/${linux} /usr/src/linux
/usr/bin/wget -c $URL
tar zxf ${VER}.tar.gz
cd ${VER}
./configure
make && make install
cp /usr/local/sbin/keepalived /usr/sbin/
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
cp -r /usr/local/etc/keepalived/ /etc/
if [ "${VER}" == "keepalived-1.2.24" ];then
	cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/ && chmod +x /etc/init.d/keepalived
else
	cp /tools/${VER}/keepalived/etc/init.d/keepalived /etc/init.d/
fi
echo -e "\033[32m $VER is install success! \033[0m"
