#!/bin/sh
src_dir=/usr/src
start_time(){
start_time="$(date +%s)"
echo -e "\033[35;1m$(date) Start install!\033[0m"
}
end_time(){
end_time="$(date +%s)"
total_s=$(($end_time - $start_time))
total_m=$(($total_s / 60))
if [ $total_s -lt 60 ]; then
       time_en="${total_s} Seconds"
else
       time_en="${total_m} Minutes"
fi
echo -e "\033[35;1m$0 runtime: ${time_en}\033[0m "
echo -e "\033[35;1mTotal runtime: ${time_en}\033[0m"
echo -e "\033[35;1m$(date) ALL Finish install!\033[0m"
}
start_time
echo -e  "\033[32;5m###Disabled SElinux###\033[0m"
if [ `getenforce` == "Disabled" ];then
    echo "SElinux is Disabled"
else
    sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
    /usr/sbin/setenforce 0
fi
echo -e  "\033[32;5m###Check Perl###\033[0m"
if [ -s /usr/bin/perl ];then
    perlversion=`perl -v |awk 'NR==2{print $4}'|sed 's/v//g'`
    echo -e "\033[34mPerlversion:$perlversion\033[0m"
else
    yum -y install perl
    if [ $? -eq 0 ];then
        echo -e "\033[34m Install Perl SUCCESSFUL\033[0m"
        perlversion=perl -v |awk 'NR==2{print $4}'|sed 's/v//g'
        echo -e "\033[34mPerlversion:$perlversion\033[0m"
    else
        echo -e "\033[34m Install Perl FAILED\033[0m"
    fi
fi
#echo $perlversion
download_src(){
cd $src_dir
if [ -s  rrdtool-1.4.7.tar.gz ];then
    echo -e "\033[34mrrdtool-1.4.7.tar.gz exist\033[0m"
else
    wget http://oss.oetiker.ch/rrdtool/pub/rrdtool-1.4.7.tar.gz
fi
if [ -s  cgilib-0.7.tar.gz ];then
    echo -e "\033[34mcgilib-0.7.tar.gz exist\033[0m"
else
    wget http://www.infodrom.org/projects/cgilib/download/cgilib-0.7.tar.gz
fi
if [ -s  fping-3.4.tar.gz ];then
    echo -e "\033[34mfping-3.4.tar.gz exist\033[0m"
else
    wget http://fping.org/dist/fping-3.4.tar.gz
fi
if [ -s  echoping-6.0.2.tar.gz ];then
    echo -e "\033[34mechoping-6.0.2.tar.gz exist\033[0m"
else
    wget http://sourceforge.net/projects/echoping/files/echoping/6.0.2/echoping-6.0.2.tar.gz/download
fi
if [ -s  FCGI-0.74.tar.gz ];then
    echo -e "\033[34mFCGI-0.74.tar.gz exist\033[0m"
else
    wget http://cpan.communilink.net/authors/id/F/FL/FLORA/FCGI-0.74.tar.gz
fi 
if [ -s  smokeping-2.6.8.tar.gz ];then
    echo -e "\033[34msmokeping-2.6.8.tar.gz exist\033[0m"
else
    wget http://oss.oetiker.ch/smokeping/pub/smokeping-2.6.8.tar.gz
fi
}
init_server(){
yum -y install gcc  automake libtool libxml2-devel libpng-devel glib pango pango-devel freetype freetype-devel fontconfig cairo cairo-devel libart_lgpl libart_lgpl-devel && yum update -y
if [ $? -eq 0 ];then
    echo -e "\033[34mInstall Required Packages SUCCESSFUL\033[0m"
else
    echo -e "\033[33mInsatall Required Packages FAILED\033[0m"
    exit 1
fi
}
echo -e "\033[32;5m###Init Server###\033[0m"
init_server
if [ $? -eq 0 ];then
    echo -e "\033[36mInit Server SUCCESSFUL\033[0m"
else
    echo -e "\033[35mInit Server Faild\033[0m"
    exit 1
fi
#echo -e "\033[32;5m###Install Required Packages###\033[0m"
install_packages(){
cd $src_dir
echo -e "\033[32;5mInsatll rrdtool\033[0m"
if [ -s /usr/local/rrdtool/bin/rrdtool ];then
    cp -rf /usr/local/rrdtool/lib/perl/$perlversion/x86_64-linux-thread-multi/ /usr/lib64/perl5/$perlversion/x86_64-linux-thread-multi/
    echo -e "\033[34m;rrdtool exist\033[0m"
else
    tar xf rrdtool-1.4.7.tar.gz -C $src_dir
    cd rrdtool-1.4.7
    ./configure --prefix=/usr/local/rrdtool --disable-tcl --disable-python && make && make install
    if [ $? -eq 0 ];then
        cp -rf /usr/local/rrdtool/lib/perl/$perlversion/x86_64-linux-thread-multi/ /usr/lib64/perl5/$perlversion/x86_64-linux-thread-multi/
        echo -e "\033[34mInstall rrdtool SUCCESSFUL\033[0m"
    else
        echo -e "\033[33mInstall rrdtool FAILED\033[0m"
        exit 1
    fi
fi
cd $src_dir
echo -e "\033[32;5mInstall cgilib\033[0m"
if [ -s /usr/local/bin/cgitest ];then
    echo -e "\033[34m;cgilib exist\033[0m"
else
    tar xf cgilib-0.7.tar.gz
    cd cgilib-0.7
    aclocal && autoconf && libtoolize --force && automake --add-missing --foreign && ./configure &&  make &&  make install
    if [ $? -eq 0 ];then
            echo -e "\033[34mInstall cgilib SUCCESSFUL\033[0m"
    else
            echo -e "\033[33mInstall cgilib FAILED\033[0m"
            exit 1
    fi
fi
cd $src_dir
echo -e "\033[32;5mInstall fping\033[0m"
if [ -s /usr/local/sbin/fping ];then
    cp /usr/local/sbin/fping /usr/sbin/fping
    echo -e "\033[34mfping exist\033[0m"
else
    tar xf fping-3.4.tar.gz
    cd fping-3.4
    ./configure && make && make install
    if [ $? -eq 0 ];then
        cp /usr/local/sbin/fping /usr/sbin/fping
            echo -e "\033[34mInstall fping SUCCESSFUL\033[0m"
    else
            echo -e "\033[33mInstall fping FAILED\033[0m"
            exit 1
    fi
fi
cd $src_dir
echo -e "\033[32;5mInstall echoping\033[0m"
if [ -s /usr/local/bin/echoping ];then
    echo -e "\033[34mechoping exist\033[0m"
else
    tar xf echoping-6.0.2.tar.gz
    cd echoping-6.0.2
    ./configure --without-libidn && make && make install
    if [ $? -eq 0 ];then
            echo -e "\033[34mInstall echoping SUCCESSFUL\033[0m"
    else
            echo -e "\033[33mInstall echoping FAILED\033[0m"
            exit 1
    fi
fi
cd $src_dir
echo -e "\033[32;5mInstall FCGI\033[0m"
if [ -d /usr/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi/auto/FCGI/ ];then
    echo -e "\033[34mFCGI exist\033[0m"
else
    tar xf FCGI-0.74.tar.gz
    cd FCGI-0.74
    perl Makefile.PL && make && make install
    if [ $? -eq 0 ];then
            echo -e "\033[34mInstall FCGI SUCCESSFUL\033[0m"
    else
            echo -e "\033[33mInstall FCGI FAILED\033[0m"
            exit 1
    fi
fi
cd $src_dir
echo -e "\033[32;5mInstall Smokeping\033[0m"
export PERL5LIB=/usr/lib64/perl5/$perlversion/x86_64-linux-thread-multi/
tar xf smokeping-2.6.8.tar.gz
cd smokeping-2.6.8
./setup/build-perl-modules.sh /usr/local/smokeping/thirdparty && ./configure --prefix=/usr/local/smokeping && make && make install
if [ $? -eq 0 ];then
        echo -e "\033[34mInstall Smokeping SUCCESSFUL\033[0m"
else
        echo -e "\033[33mInstall Smokeping FAILED\033[0m"
        exit 1
fi
}
echo -e "\033[32;5m###Check Required Packages###\033[0m"
download_src
if [ $? -eq 0 ];then
    echo -e "\033[36mDownload Packages SUCCESSFUL\033[0m"
else
    echo -e "\033[35mDownload Packages FAILD\033[0m"
    exit 1
fi
echo -e "\033[32;5m###Install Packages###\033[0m"
install_packages
if [ $? -eq 0 ];then
        echo -e "\033[36mInstall Packages SUCCESSFUL\033[0m"
else
        echo -e "\033[35mInstall Packages FAILD\033[0m"
        exit 1
fi
echo -e "\033[34mConfigure Smokeping\033[0m"
mkdir -v /usr/local/smokeping/data
chmod -v 777 /usr/local/smokeping/data
mkdir -v /usr/local/smokeping/cache
chmod -v 777 /usr/local/smokeping/cache
mkdir -v /usr/local/smokeping/var
cd /usr/local/smokeping/etc/
cp -av config.dist config
sed -i 's#owner    = Peter Random#owner = ChinaCache GNOC#g' config
sed -i 's#contact  = some@address.nowhere#contact = ccna-noc@chinacache.com#g' config
sed -i 's#imgcache = /usr/local/smokeping/cache#imgcache = /usr/local/smokeping/cache#g' config
sed -i 's#imgurl   = cache#imgurl   = ../cache#g' config
sed -i 's*use lib qw(/usr/lib64/perl5/5.8.8/x86_64-linux-thread-multi/); # PERL5LIB*use lib qw(/usr/local/rrdtool/lib/perl);*g' /usr/local/smokeping/bin/smokeping
sed  -i   "8 a\use lib qw(\/usr\/local\/smokeping\/lib);" /usr/local/smokeping/bin/smokeping
#datadir  = /usr/local/smokeping/data
#piddir  = /usr/local/smokeping/var
#binary = /usr/sbin/fping
cat>>/usr/local/smokeping/etc/secret.txt<<EOF
qwer%TGB
EOF
chmod -v 600 /usr/local/smokeping/etc/secret.txt
end_time