#!/bin/bash
# Author:  EinicYeo <einicyeo AT gmail.com>
# BLOG:  https://www.infvie.com
#
# Notes: EnvoyinStack for CentOS/RedHat 6+

Install_fastdfs() {
  pushd ${envoyinstack_dir}/src/fastdfs > /dev/null
  tar xzf libfastcommon-v${libfastcommon_ver}.tar.gz
  pushd libfastcommon-v${libfastcommon_ver} > /dev/null
  ./make.sh && ./make.sh install
  if [ -e /usr/lib64/libfastcommon.so ];then
     if [ ! -e /usr/local/lib/libfastcommon.so ]||[ ! -e /usr/local/lib/libfdfsclient.so ]|| [ ! -e /usr/lib/libfdfsclient.so ];then
        ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so;
        ln -s /usr/lib64/libfdfsclient.so /usr/local/lib/libfdfsclient.so;
        ln -s /usr/lib64/libfdfsclient.so /usr/lib/libfdfsclient.so;
     elif [ ! -e /usr/local/lib/libfastcommon.so ]||[ ! -e /usr/local/lib/libfdfsclient.so ]|| [ ! -e /usr/lib/libfdfsclient.so ];then
        echo "${CFAILURE}libfastcommon install failed, Please contact the author! ${CEND}"
        exit
     else
        echo "${CSUCCESS}libfastcommon installed successfully! ${CEND}"
     fi
  else
     echo "${CFAILURE}libfastcommon install failed, Please contact the author! ${CEND}"
     exit
  fi
  popd > /dev/null
  rm -rf libfastcommon-v${libfastcommon_ver}
  
  tar xzf fastdfs-v${fastdfs_ver}.tar.gz   
  pushd fastdfs-v${fastdfs_ver} > /dev/null
  ./make.sh && ./make.sh install && cp conf/http.conf /etc/fdfs/ && cp conf/mime.types /etc/fdfs/ && cd /etc/fdfs/ && cp client.conf.sample client.conf && cp storage.conf.sample storage.conf && cp tracker.conf.sample tracker.conf
  mkdir -p /data/fastDFS/{tracker,storage,data,client}
  sed -i '/^base_path/c\base_path=/data/fastDFS/tracker' /etc/fdfs/tracker.conf
  sed -i '/^max_connections/c\max_connections=4096' /etc/fdfs/tracker.conf
  sed -i '/^min_buff_size/c\min_buff_size = 128KB' /etc/fdfs/tracker.conf
  sed -i '/^max_buff_size/c\max_buff_size = 4096KB' /etc/fdfs/tracker.conf
  sed -i '/^store_group/c\store_group=group1' /etc/fdfs/tracker.conf 
  chkconfig fdfs_trackerd --add && service fdfs_trackerd restart 
  if [ -e /etc/init.d/fdfs_trackerd ];then
     echo "${CSUCCESS}fastdfs_tracker installed successfully! ${CEND}"
  else
     echo "${CFAILURE}fastdfs_tracker install failed, Please contact the author! ${CEND}"
     exit
  fi
 
  sed -i '/^base_path/c\base_path=/data/fastDFS/storage' /etc/fdfs/storage.conf
  sed -i '/^max_connections/c\max_connections=4096' /etc/fdfs/storage.conf
  sed -i '/^min_buff_size/c\min_buff_size = 1024KB' /etc/fdfs/storage.conf
  sed -i '/^max_buff_size/c\max_buff_size = 10240KB' /etc/fdfs/storage.conf
  sed -i '/^store_group/c\store_group=group1' /etc/fdfs/storage.conf
  sed -i '/^store_path0/c\store_path0=/data/fastDFS/data' /etc/fdfs/storage.conf
  sed -i '/^tracker_server/c\tracker_server='${IPADDR}':22122' /etc/fdfs/storage.conf  
  chkconfig fdfs_storaged --add && service fdfs_storaged restart 
  if [ -e /etc/init.d/fdfs_storaged ];then
     echo "${CSUCCESS}fdfs_storaged installed successfully! ${CEND}"
  else
     echo "${CFAILURE}fdfs_storaged install failed, Please contact the author! ${CEND}"
     exit
  fi
  
  sed -i '/^base_path/c\base_path=/data/fastDFS/client' /etc/fdfs/client.conf
  sed -i '/^tracker_server/c\tracker_server='${IPADDR}':22122' /etc/fdfs/client.conf
 
  popd > /dev/null
  rm -rf fastdfs-v${fastdfs_ver}
  
  fastdfs=`netstat -antlp|grep 22122|wc -l`
  if [ $fastdfs -ne 1 ];then
    echo "${CSUCCESS}fastdfs start successfully! ${CEND}"
  else
    echo "${CFAILURE}fastdfs start failed, Please contact the author! ${CEND}"
    exit
  fi
  
  tar zxf fastdfs-nginx-module-v${fdfsngxmodule_ver}.tar.gz
  pushd fastdfs-nginx-module-v${fdfsngxmodule_ver}/src > /dev/null
  cp mod_fastdfs.conf /etc/fdfs/  
  sed -i '/^base_path/c\base_path=/data/fastDFS/storage' /etc/fdfs/mod_fastdfs.conf
  sed -i '/^tracker_server/c\tracker_server='${IPADDR}':22122' /etc/fdfs/mod_fastdfs.conf
  sed -i '/^url_have_group_name/c\url_have_group_name = true' /etc/fdfs/mod_fastdfs.conf
  sed -i '/^store_path0/c\store_path0=/data/fastDFS/data' /etc/fdfs/mod_fastdfs.conf
  sed -i '/^nginx_modules_options/c\nginx_modules_options="--add-module='`pwd`'"' ../../../../config/options.conf
  ../../../../install.sh --nginx_option 1 
  mkdir -p /usr/local/nginx/conf/vhost
  sed -i '/^nginx_modules_options/c\nginx_modules_options=' ../../../../config/options.conf
  popd > /dev/null
  rm -rf fastdfs-nginx-module-v${fdfsngxmodule_ver}

  cat >${nginx_install_dir}/conf/vhost/storage.conf <<EOF
 server {
    listen 8088;
    server_name _;
    include location.conf;
    location ~ /group([0-9])/M00 {
    root /data/fastDFS/data/data;
    index index.html index.htm;
    ngx_fastdfs_module;
    }
  }
EOF
 if [ -e ${nginx_install_dir}/conf/vhost/default.conf ];then
   mv ${nginx_install_dir}/conf/vhost/default.conf ${nginx_install_dir}/conf/vhost/default.conf.bak
 fi
 cat >${nginx_install_dir}/conf/vhost/tracker.conf <<EOF
  upstream fdfs_group1 {
   server 127.0.0.1:8088 weight=1 max_fails=2 fail_timeout=30s;
  }
  server {
   listen 80;
   server_name _;
   include location.conf;
   location ~ /group([0-9])/M00 {
     proxy_next_upstream http_502 http_504 error timeout invalid_header;
     proxy_pass http://fdfs_group1;
     expires 30d;
   }
 }
EOF

  if [ -e /usr/local/nginx/conf/vhost/storage.conf ] && [ -e /usr/local/nginx/conf/vhost/tracker.conf ] ;then
    systemctl restart nginx
    if [ -e /var/run/nginx.pid ];then
       echo "${CSUCCESS}ngx_fastdfs_module installed successfully! ${CEND}"
    else
       echo "${CFAILURE}ngx_fastdfs_module install failed, Please contact the author! ${CEND}"
       exit
    fi
  else
       echo "${CFAILURE}ngx_fastdfs_module install failed, Please contact the author! ${CEND}"
       exit
  fi

 tar zxf db-${berkeleydb_ver}.tar.gz 
 pushd db-${berkeleydb_ver} > /dev/null
 ./dist/configure --prefix=/usr/local/berkeleydb && make && make install
 if [ -d /usr/local/berkeleydb ];then
    echo "${CSUCCESS}Derkeleydb installed successfully! ${CEND}"
 else
    echo "${CFAILURE}Derkeleydb install failed, Please contact the author! ${CEND}"
    exit
 fi
 popd > /dev/null
 rm -rf db-${berkeleydb_ver}
 
 unzip -o fastdht-master.zip
 pushd fastdht-master > /dev/nul
 sed -i 's@-D_GNU_SOURCE@& -I/usr/local/berkeleydb/include/ -L/usr/local/berkeleydb/lib/@' make.sh
 ./make.sh && ./make.sh install
 mkdir -p /data/fastDFS/fastdht
 cat >/etc/fdht/fdht_servers.conf <<EOF
group_count = 1
group0 = ${IPADDR}:11411
EOF

 sed -i '/^base_path/c\base_path=/data/fastDFS/fastdht' /etc/fdht/fdhtd.conf
 sed -i '/^max_connections/c\max_connections=1000' /etc/fdht/fdhtd.conf
 sed -i '/^max_pkg_size/c\max_pkg_size=1024KB' /etc/fdht/fdhtd.conf
 sed -i '/^min_buff_size/c\min_buff_size=10240KB' /etc/fdht/fdhtd.conf
 sed -i '/^base_path/c\base_path=/data/fastDFS/fastdht' /etc/fdht/fdht_client.conf
 sed -i '/^check_file_duplicate/c\check_file_duplicate=1' /etc/fdfs/storage.conf
 sed -i '/^keep_alive/c\keep_alive=1' /etc/fdfs/storage.conf
 sed -i '/^#include fdht_servers.conf/c\#include /etc/fdht/fdht_servers.conf' /etc/fdht/fdht_client.conf
 sed -i '/^##include /home/yuqing/fastdht/conf/fdht_servers.conf/c\#include /etc/fdht/fdht_servers.conf' /etc/fdfs/storage.conf
 ln -s /usr/local/berkeleydb/lib/libdb-18.1.so /usr/lib && ldconfig
 popd > /dev/null
 rm -rf fastdht-master

 cp ../../init.d/fdhtd /etc/init.d/ && chmod +x /etc/init.d/fdhtd && chkconfig fdhtd --add && chkconfig fdhtd on
 service fdhtd start
 if [ -e /usr/local/bin/fdhtd ];then
    echo "${CSUCCESS}fdhtd installed successfully! ${CEND}"
 else
   echo "${CFAILURE}fdhtd install failed, Please contact the author! ${CEND}"
   exit
 fi
  echo "####################verification sleep 3s########################"
  sleep 3s
  echo "`/usr/bin/fdfs_test /etc/fdfs/client.conf upload infvie.png`" 
}
