#!/bin/bash



# 需要实现的功能--指定端口 安装redis
## 二进制的安装方式
## docker的安装方式

## 对外暴露的安装Redis的主函数
InstallRedis(){
  RedisPort="6379"
  RedisInstallMethod="binary"
  RedisInstallHelp="0"

  while [[ $# > 0 ]]; do
    case "$1" in
        -p|--port)
        RedisPort="${2}"
        shift # past argument
        ;;
        -m|--method)
        RedisInstallMethod="${2}"
        shift # past argument
        ;;
        -h|--help)
        RedisInstallHelp="1"
        ;;
        *)
          echo "输入的内容有误，请检查！"
        # unknown option
        ;;
    esac
    shift # past argument or value
    done

    if [ "${RedisInstallHelp}" -eq "1" ]; then
        cat - 1>& 2 << EOF
./install-release.sh [-h] [-p|--port 6379] [-m|--method binary|docker]
  -h, --help            打印此安装帮助说明
  -p, --port            安装Redis的端口，如果不指定此参数，则默为6379
  -m, --method          安装Redis的方式，binary == 源码编译安装，docker == 使用docker安装；不指定则使用binary
EOF
    fi

#    echo $RedisPort
#    echo $RedisInstallMethod

    if [[ ${RedisInstallMethod} == "binary" ]]; then
        InstallRedisBinary ${RedisPort}
    else
        InstallRedisDocker ${RedisPort}
    fi
}

InstallRedisBinary() {
  RedisPort=""
  if [[ "$1" -ne " " ]]; then
      RedisPort="$1"
      echo "Redis Port = ${RedisPort}"
  fi

  echo "InstallRedisBinary"

   CMD_REMOVE gcc
   installDemandSoftwares gcc wget

  echo "开始下载 Redis 6.2.6 的二进制包！"
  wget https://objectstorage.ap-seoul-1.oraclecloud.com/n/cnk8d6fazu16/b/seoul/o/redis-6.2.6.tar.gz

  if [ -e redis-6.2.6.tar.gz ]; then
    echo "redis源码包下载完成！"
    echo ""
    echo "开始解压缩redis的安装包！"
    tar -zvxf redis-6.2.6.tar.gz
    cd redis-6.2.6
    clear
    echo ""
    echo ""
    echo "开始执行编译安装过程！！"
    echo "开始执行编译安装过程！！"
    echo "开始执行编译安装过程！！"
    echo "取决于服务器的性能，可能花费较长的时间！！！"
    sleep 3
    echo ""
    ./configure
    make && make install
    cd redis-6.2.6

    echo "Redis已经安装成功！！"
    ehco "开始进行redis的配置修改！！"
    wget https://objectstorage.ap-seoul-1.oraclecloud.com/n/cnk8d6fazu16/b/seoul/o/redis-6.2.6.conf
    wget https://objectstorage.ap-seoul-1.oraclecloud.com/n/cnk8d6fazu16/b/seoul/o/redis-server-6.2.6.service

    if [ -e redis-6.2.6.conf ] && [ -e redis-server-6.2.6.service ]; then
        echo "redis配置文件下载成功，开始进行修改！！"
        echo ""
        touch /var/log/redis_${RedisPort}.log
        mkdir -p /var/redis/${RedisPort}
        mkdir -p /etc/redis/

        sed -i "s/RedisPort/${RedisPort}/g" redis-6.2.6.conf
        cp redis-6.2.6.conf /etc/redis/${RedisPort}.conf

        sed -i "s/RedisPort/${RedisPort}/g" redis-server-6.2.6.service
        cp redis-server-6.2.6.service /etc/init.d/redisd

        cd /etc/init.d
        chmod +x /etc/init.d/redisd

        if [ command_exists chkconfig ]; then
            chkconfig redisd on
        elif [ command_exists update-rc.d ]; then
            update-rc.d redisd defaults
        else
            echo "所需要的守护程序未安装，请手动设置！！"
        fi

        #  启动程序
        echo ""
        echo "开始启动redis-server服务……"
        service redisd start

        service redisd status

        netstat -ntlp | grep redis

    else
        echo "redis配置文件下载失败！！请手动进行修改！！"
        return 3
    fi
  else
    echo "redis源码包下载失败！"
    return 3
  fi

}

InstallRedisDocker(){
  RedisPort=""
  if [[ "$1" -ne " " ]]; then
      RedisPort="$1"
      echo "Redis Port = ${RedisPort}"
  fi

  echo "InstallRedisDocker"
  echo ""

  echo "## 为redis配置添加 ">>/etc/sysctl.conf
  echo "vm.overcommit_memory = 1">>/etc/sysctl.conf
  sysctl -p /etc/sysctl.conf

  echo "开始启动docker-redis !!"
      # https://hub.docker.com/r/bitnami/redis#configuration

  # 为redis设置密码  -e REDIS_PASSWORD=v2ryStr@ngPa.ss \
  docker run -d \
        -e ALLOW_EMPTY_PASSWORD=yes \
        -e REDIS_AOF_ENABLED=no \
        -e REDIS_PORT_NUMBER=${RedisPort} \
        --name redis-server \
        --network host \
        bitnami/redis:6.2.6
}


InstallRedis -p 36379 -m docker