#!/bin/bash
#
# ServHA install script for Ubuntu, Debian.
# By WANG LONG MicroColor
# Latest 20221220
#

basepath=$(cd `dirname $0`; pwd)
cd $basepath

installblockmir=1
installpathmir=1
installdrbdmir=1

# 检查root权限
if [ "$EUID" -ne 0 ]; then
    echo "遗憾: ServHA安装脚本需要以root权限运行。"
    exit 1
fi

echo "[输入数字，以选择要安装的镜像组件 Choose the mirroring component to install]"
select usersel in "全部安装 All" "仅卷镜像 Only volume mirroring comonent" "仅目录镜像 Only directory mirroring component"; do
    case $usersel in
        	"全部安装 All" ) break;;
        	"仅卷镜像 Only volume mirroring comonent" ) installpathmir=0;  break;;
        	"仅目录镜像 Only directory mirroring component" ) installblockmir=0;  break;;
    esac
done



if [ $installblockmir -eq 1 ]; then

	#echo "您选择了安装卷镜像，是否额外安装drbd镜像组件：卷镜像支持在原有分区上直接建立镜像同步; drbd镜像组件将带来额外的功能：在原有分区之上，建立一个虚拟块设备进行镜像同步。"
	#echo "输入y表示安装drbd镜像组件。"
	#read -n 1 -p "是否安装drbd镜像组件（y/n）？" usersel
	#if [ "$usersel" = "y" ]; then
	#	echo -e "\n您选择安装drbd镜像组件。"
	#	installdrbdmir=1
	#else
	#	echo -e "\n您选择不要安装drbd镜像组件。"
	#	installdrbdmir=0
	#fi

	installdrbdmir=1
fi

echo "正安装ServHA.."

dpkg -i Packages/servha-mirror_3.5-*_amd64.deb

if [ $installdrbdmir == 1 ]; then
#uname -a | grep -E "20.04|21.04|22.04" | grep "Ubuntu"
    KERNEL_VERSION=$(uname -r)
    MAJOR_VERSION=$(echo "$KERNEL_VERSION" | cut -d '.' -f1)
    if [ "$MAJOR_VERSION" -ge 5 ]; then
        #比较新版本的友邦图用新的包。
        echo | dpkg  -i /usr/share/servha/drbd/ubuntu/drbd-utils_9.11.0-1build1_amd64.deb
    else
        echo | dpkg  -i /usr/share/servha/drbd/ubuntu/drbd-utils_8.9.6-1_amd64.deb
    fi
#查询是否安装成功
    dpkg -l drbd8-utils > /dev/null 2>/dev/null
    if [ $? -ne 0 ]; then
        #本地包安装失败，尝试在线安装
        echo "尝试使用apt-get在线安装有关依赖包..."
        echo N | apt-get install drbd8-utils
    fi
fi
dpkg -l curl > /dev/null 2>/dev/null
if [ $? -ne 0 ]; then
    echo | apt-get install curl
fi
dpkg -l arping > /dev/null 2>/dev/null
if [ $? -ne 0 ]; then
    echo | apt-get install arping
fi

dpkg -l net-tools > /dev/null 2>/dev/null
if [ $? -ne 0 ]; then
    echo | apt-get install net-tools
fi

if [ $installpathmir == 1 ]; then
    /usr/share/servha/insr0
fi

if [ $installblockmir -eq 1 ]; then
	/usr/share/servha/insmir
fi

systemctl start servmird.service
systemctl start servhad.service

isblockmirready=0
isdrbdready=0
ispathmirready=0

MOD_DIR="/lib/modules/$(uname -r)"
if find "$MOD_DIR" -name "drbd.ko*" -print -quit | grep -q .
 then
	isdrbdready=1
fi
MOD_DIR="/lib/modules/$(uname -r)/updates"
if find "$MOD_DIR" -name "mcmirflt.ko*" -print -quit | grep -q .
 then
	isblockmirready=1
fi
if find "$MOD_DIR" -name "mcnsr0.ko*" -print -quit | grep -q .
 then
	ispathmirready=1
fi

echo -e "以下是镜像功能相关内核模块的安装结果："
if [ $isblockmirready -eq 1 ]; then
	echo -e "  -- 卷镜像驱动模块：\t\033[32m成功安装\033[0m"
else
	echo -e "  -- 卷镜像驱动模块：\t\033[31m未安装\033[0m"
fi

if [ $isdrbdready -eq 1 ]; then
	echo -e "  -- drbd镜像驱动模块：\t\033[32m成功安装\033[0m"
else
	echo -e "  -- drbd镜像驱动模块：\t\033[31m未安装\033[0m"
fi
if [ $ispathmirready -eq 1 ]; then
	echo -e "  -- 目录镜像驱动模块：\t\033[32m成功安装\033[0m"
else
	echo -e "  -- 目录镜像驱动模块：\t\033[31m未安装\033[0m"
fi

if [ $isblockmirready -eq 0 -o $ispathmirready -eq 0  ]; then
	echo -e "存在失败项，将影响对应镜像功能的使用。您可以联系技术支持人员，提供您的内核版本信息：$(uname -r)获取支持。"
fi

if [ $isdrbdready -eq 0 ]; then
	echo -e "如需安装旧版drbd镜像功能支持模块，请手工运行/usr/share/servha/drbd/build.sh"
fi

if [ $installblockmir == 1 ]; then
	dpkg -l drbd8-utils > /dev/null 2>/dev/null
	if [ $? -ne 0 ]; then
    		echo "## [ 警告 ] 无法自动安装drbd8-utils，请手动安装该依赖包."
	fi
fi
dpkg -l curl > /dev/null 2>/dev/null
if [ $? -ne 0 ]; then
    echo "## [ 警告 ] 无法自动安装curl，请手动安装该依赖包."
fi

dpkg -l net-tools > /dev/null 2>/dev/null
if [ $? -ne 0 ]; then
    echo "## [ 警告 ] 无法自动安装net-tools，请手动安装该依赖包."
fi
#dpkg -l arping > /dev/null 2>/dev/null
#if [ $? -ne 0 ]; then
#    echo "## [ 警告 ] 无法自动安装arping，请手动安装该依赖包."
#fi

echo "==============================================="
echo "  输入 'servha' 命令启动配置监控端开始配置热备系统。"
echo "  ServHA Mirror安装脚本运行完毕，请继续后续配置！"
echo "==============================================="
exit 0
