#!/bin/bash
shopt -s expand_aliases
Font_Black="\033[30m";
Font_Red="\033[31m";
Font_Green="\033[32m";
Font_Yellow="\033[33m";
Font_Blue="\033[34m";
Font_Purple="\033[35m";
Font_SkyBlue="\033[36m";
Font_White="\033[37m";
Font_Suffix="\033[0m";
local_ip=`curl -s http://ipv4.icanhazip.com`
source /etc/os-release &>/dev/null
# 系统检测、仅支持 Debian8+ 和 Ubuntu16.04+
check_system(){
    if [[ "${ID}" == "debian" && ${VERSION_ID} -ge 8 ]];then
        echo -e "${OK} ${GreenBG} 当前系统为 Debian ${VERSION_ID} ${Font_Suffix} "
    elif [[ "${ID}" == "ubuntu" && `echo "${VERSION_ID}" | cut -d '.' -f1` -ge 16 ]];then
        echo -e "${OK} ${GreenBG} 当前系统为 Ubuntu ${VERSION_ID} ${Font_Suffix} "
    else
        echo -e "${Error} ${RedBG} 当前系统为不在支持的系统列表内，安装中断 ${Font_Suffix} "
        exit 1
    fi
}
# 判定是否为root用户
is_root(){
    if [ `id -u` == 0 ]
        then echo -e "${OK} ${GreenBG} 当前用户是root用户，进入安装流程 ${Font_Suffix} "
        sleep 1
    else
        echo -e "${Error} ${RedBG} 当前用户不是root用户，请切换到root用户后重新执行脚本 ${Font_Suffix}" 
        exit 1
    fi
}
function debian_source(){
    # 添加源
    apt-get update 
    apt install apt-transport-https ca-certificates curl software-properties-common gnupg2 wget -y
    echo "deb https://download.docker.com/linux/debian/ buster stable" | tee --append /etc/apt/sources.list 
    add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable"
    # 添加key
    wget --no-check-certificate https://www.dotdeb.org/dotdeb.gpg
    if [[ -f dotdeb.gpg ]];then
        curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add -
        if [[ $? -eq 0 ]];then
            echo -e "${OK} ${GreenBG} 导入 GPG 秘钥成功 ${Font_Suffix}"
            sleep 1
        else
            echo -e "${Error} ${RedBG} 导入 GPG 秘钥失败 ${Font_Suffix}"
            exit 1
        fi
    else
        echo -e "${Error} ${RedBG} 下载 GPG 秘钥失败 ${Font_Suffix}"
        exit 1
    fi
    # 源更新
    apt-get update -y
    apt-get install docker-scan-plugin -y
}
# 执行函数
# 检查是否安装docker

function docker_check(){
	echo "检查Docker......"
	docker -v
    if [ $? -eq  0 ]; then
        echo "检查到Docker已安装!"
        sleep 1
    else
        echo -e "${Error} ${Font_Yellow} 无docker环境 请先安装docker ${Font_Suffix}" 
        sleep 1
    fi
    # 创建公用网络==bridge模式
    #docker network create share_network
} 
       # 执行函数
docker_check
# 安装docker

docker_install(){
	echo "检查Docker......"
	docker -v
    if [ $? -eq  0 ]; then
        echo "检查到Docker已安装!"
    else
    	echo "安装docker环境..."
        apt install docker-ce -y
        echo "安装docker环境...安装完成!"
    fi
    # 创建公用网络==bridge模式
    #docker network create share_network
} 

# 安装Aria2NG
aria2ng(){
docker run -d \
    --name ariang \
    --restart unless-stopped \
    --log-opt max-size=1m \
    -p 6880:6880 \
    p3terx/ariang
}
# 安装Aria2 Pro
aria2p(){
docker run -d \
    --name aria2-pro \
    --restart unless-stopped \
    --log-opt max-size=1m \
    --network host \
    -e PUID=$UID \
    -e PGID=$GID \
    -e RPC_SECRET=Wangqiang1342 \
    -e RPC_PORT=6805 \
    -e LISTEN_PORT=6888 \
    -v /root/config/aria2:/config \
    -v /root/share:/downloads \
    p3terx/aria2-pro
}
# 安装FareSolverr
flareSolverr(){
docker run -d \
  --name=flaresolverr \
  -p 8191:8191 \
  -e LOG_LEVEL=info \
  --restart unless-stopped \
  flaresolverr/flaresolverr:latest
}
# 安装Jackett
jackett(){
docker run -d \
  --name=jackett \
  -e PUID=1000 \
  -e PGID=1000 \
  -e TZ=Asia/Shanghai \
  -p 9117:9117 \
  -v /root/config/j-config:/config \
  -v /root/share:/downloads \
  --restart unless-stopped \
  linuxserver/jackett
}
# 安装Qbittorrent
qbittorrent(){
docker run -d \
    --name=qbittorrent  \
    -e WEBUI_PORT=8999  \
    -e UID=0  \
    -e GID=0  \
    -p 52422:52422 \
    -p 52422:52422/udp  \
    -p 8999:8999  \
    -v /root/config/qb-config:/config  \
    -v /root/share:/downloads  \
    --restart unless-stopped  \
   linuxserver/qbittorrent
}
# 安装Sonarr
sonarr(){
docker run -d \
  --name=sonarr \
  -e PUID=0\
  -e PGID=0 \
  -e TZ=Asia/Shanghai \
  -p 8989:8989 \
  -v /root/config/sonarr:/config \
  -v /home/GD/剧集:/tv \
  -v /root/share:/downloads \
  --restart unless-stopped \
 linuxserver/sonarr:latest
}
# 安装Radarr
radarr(){
    # 安装radarr
    docker run -d \
  --name=radarr \
  -e PUID=0 \
  -e PGID=0 \
  -e TZ=Asia/Shanghai \
  -p 7878:7878 \
  -v /root/config/radarr:/config \
  -v /home/GD/电影:/movies \
   -v /root/share:/downloads \
  --restart unless-stopped \
 linuxserver/radarr:latest
}
main(){
    check_system
    is_root
	sleep 2
	echo -e "${Font_Yellow} ======请选择安装内容====== ${Font_Suffix}"
	echo -e "${Font_SkyBlue}输入数字【1】： Ragarr--自动获取电影${Font_Suffix}"
	echo -e "${Font_SkyBlue}输入数字【2】： Sonarr--自动追剧${Font_Suffix}"
	echo -e "${Font_SkyBlue}输入数字【3】： Qbittorrent--下载神器${Font_Suffix}"
	echo -e "${Font_SkyBlue}输入数字【4】： Jackett--资源搜刮${Font_Suffix}"
	echo -e "${Font_SkyBlue}输入数字【5】： FlareSolverr--绕过代理验证${Font_Suffix}"
	echo -e "${Font_SkyBlue}输入数字【6】： Aria2 Pro--更好用的Aria2${Font_Suffix}"
	echo -e "${Font_SkyBlue}输入数字【7】： Aria2NG--前端面板( GUI、WebUI )${Font_Suffix}"
	echo -e "${Font_SkyBlue}输入数字【8】： Docker容器安装 ${Font_Suffix}"
	echo -e "${Font_SkyBlue}输入数字【9】： -- 一把梭-安装全部 -- ${Font_Suffix}"
	read -p "       请输入数字:" number 
	case ${number} in
		1)
            radarr
        if [[ $? -eq 0 ]];then
            echo -e "${OK} ${Font_Green} Ranarr 安装成功 
            存储路径：/home/GD
            下载路径：/root/share
            外网端口：/7878
            WEB UI:http://${local_ip}:7878${Font_Suffix}"
            sleep 1
        else
            echo -e "${Error} ${Font_Red} Ranarr 安装失败 ${Font_Suffix}"
            exit 1
        fi  
			;;
		2)
            sonarr
        if [[ $? -eq 0 ]];then
            echo -e "${OK} ${Font_Green} Sonarr 安装成功 
            存储路径：/home/GD
            下载路径：/root/share
            外网端口：/8989
            WEB UI:http://${local_ip}:8989${Font_Suffix}"
            sleep 1
        else
            echo -e "${Error} ${Font_Red} Sonarr 安装失败 ${Font_Suffix}"
            exit 1
        fi  
			;;
		3)
            qbittorrent
        if [[ $? -eq 0 ]];then
            echo -e "${OK} ${Font_Green} Qbittorrent 安装成功 
            下载路径：/root/share
            外网端口：/8999
            WEB UI:http://${local_ip}:8999${Font_Suffix}"
            sleep 1
        else
            echo -e "${Error} ${Font_Red} Qbittorrent 安装失败 ${Font_Suffix}"
            exit 1
        fi  
			;;
		4)
            jackett
        if [[ $? -eq 0 ]];then
            echo -e "${OK} ${Font_Green} Jackett 安装成功 
            外网端口：/9117
            WEB UI:http://${local_ip}:9117${Font_Suffix}"
            sleep 1
        else
            echo -e "${Error} ${Font_Red} Jackett 安装失败 ${Font_Suffix}"
            exit 1
        fi  
			;;
		5)
            flareSolverr
        if [[ $? -eq 0 ]];then
            echo -e "${OK} ${Font_Green} FlareSolverr 安装成功 
            外网端口：/8191
            WEB UI:http://${local_ip}:8191${Font_Suffix}"
            sleep 1
        else
            echo -e "${Error} ${Font_Red} FlareSolverr 安装失败 ${Font_Suffix}"
            exit 1
        fi  
			;;
		6)
            aria2p
        if [[ $? -eq 0 ]];then
            echo -e "${OK} ${Font_Green} Aria2 Pro 安装成功 
            存储路径：/root/share
            RPC_PORT：/6805
            外网端口：/6888${Font_Suffix}"
            sleep 1
        else
            echo -e "${Error} ${Font_Red} Aria2 Pro 安装失败${Font_Suffix}"
            exit 1
        fi  
			;;
		7)
            aria2ng
        if [[ $? -eq 0 ]];then
            echo -e "${OK} ${Font_Green} Aria2NG 安装成功
            外网端口：/6880
            WEB UI:http://${local_ip}:6880${Font_Suffix}"
            sleep 1
        else
            echo -e "${Error} ${Font_Red} Aria2NG 安装失败 ${Font_Suffix}"
            exit 1
        fi  
			;;
		8)
            debian_source
            docker_install
        if [[ $? -eq 0 ]];then
            echo -e "${OK} ${Font_Green} Docker 安装成功${Font_Suffix}"
            sleep 1
        else
            echo -e "${Error} ${Font_Red} Docker 安装失败 ${Font_Suffix}"
            exit 1
        fi  
			;;
		9)
		  
            debian_source
            docker_install
            radarr
            sonarr
            qbittorrent
            jackett
            flareSolverr
            aria2p
            aria2ng
            
        if [[ $? -eq 0 ]];then
            echo -e "${OK} ${Font_Green} 全部 安装成功
            存储路径：/home/GD
            下载路径：/root/share
            外网端口：/7878
            WEB UI:http://${local_ip}:7878${Font_Suffix}"
            sleep 1
        else
            echo -e "${Error} ${Font_Red}  安装失败 ${Font_Suffix}"
            exit 1
        fi  
			;;
		*)
			echo -e "${Error} ${Font_Red} 请输入正确的序号 ${Font_Suffix}"
			exit 1
			;;
	esac
   
}

main
