#!/bin/sh
#

cdrompath="/dev/cdrom"
cdrootpath="/mchatmpdsk"
cdbasepath="/mchatmpdsk"
noarchpath="/mchatmpdsk"
platformstr="x86_64"

clearup(){
	basepath=$(cd `dirname $0`; pwd)
	cd $basepath
	umount /mchatmpdsk > /dev/null
	rmdir /mchatmpdsk > /dev/null
}

platformstr=`uname -i`

echo -e ""
echo -e "本脚本将自动安装 gcc,kernel-header,flex 组件,请确保插入了当前系统对应的操作系统光盘。"
echo -e "COPYRIGHT 北京微彩华创科技有限公司"
read -p "按任意键继续..." -n 1
echo "尝试挂载光盘.."
rmdir /mchatmpdsk > /dev/null
mkdir "/mchatmpdsk"
if [ $? != 0 ]; then
	echo -e "\033[31m无法建立临时挂载目录/mchatmpdsk。 \033[0m"
	clearup
	exit 2
fi
mount -r $cdrompath /mchatmpdsk
if [ $? != 0 ]; then
	echo -e "\033[31m无法自动挂载/dev/cdrom，请手动挂载并指定系统盘根目录。 \033[0m"
	read -p "请输入系统盘根目录：" cdrootpath
fi

oripath=cdrootpath

issuse=0
isel8=0
if [ -f /etc/SuSE-release ]; then
	cdrootpath="$cdrootpath/suse/x86_64"
	noarchpath="$noarchpath/suse/noarch"
	issuse=1
else


	uname -r | grep "el8" > /dev/null
	if [ $? = 0 ]; then
		isel8=1;
		cdrootpath="$cdrootpath/AppStream/Packages"
		cdbasepath="$cdbasepath/BaseOS/Packages"
	else
		uname -r | grep .ky > /dev/null
		if [ $? == 0 ]; then
			cdrootpath="$cdrootpath/KYLIN"
		else
			cdrootpath="$cdrootpath/Packages"
		fi
	fi
fi
if [ ! -d $cdrootpath ]; then
	uname -r | grep .ky > /dev/null
	if [ $? == 0 ]; then
		cdrootpath="/mchatmpdsk/Packages"
		if [ ! -d $cdrootpath ]; then
			echo -e "\033[31m无法在光盘目录中查找到Packages目录，请确保插入了正确的光盘或指定了正确的目录  2。 \033[0m"
			clearup
			exit 3
		fi
	else
		echo -e "\033[31m无法在光盘目录中查找到Packages目录，请确保插入了正确的光盘或指定了正确的目录。 \033[0m"
		clearup
		exit 3
	fi
fi
if [ $issuse == 1 ]; then
	rpm -ivh $cdrootpath/make-[0-9]*.$platformstr.rpm
	rpm -ivh $cdrootpath/mpfr-[0-9]*.$platformstr.rpm
	rpm -ivh $cdrootpath/libmpc-[0-9]*.$platformstr.rpm
	rpm -ivh $cdrootpath/libgomp-[0-9]*.$platformstr.rpm
	rpm -ivh $cdrootpath/ppl-[0-9]*.$platformstr.rpm
	rpm -ivh $cdrootpath/cloog-ppl-[0-9]*.$platformstr.rpm
	rpm -ivh $noarchpath/linux-kernel-headers-[0-9]*.rpm
	rpm -ivh $noarchpath/linux-glibc-devel-[0-9]*.rpm
	rpm -ivh $cdrootpath/glibc-headers-[0-9]*.$platformstr.rpm
	rpm -ivh $cdrootpath/glibc-devel-[0-9]*.$platformstr.rpm
	rpm -ivh $cdrootpath/zlib-devel-*.*.rpm
	rpm -ivh $cdrootpath/binutils-*.*.rpm
	rpm -ivh $cdrootpath/cpp-[0-9]*.$platformstr.rpm
	rpm -ivh $cdrootpath/gcc4[0-9]-4.[0-9].*.rpm
	rpm -ivh $cdrootpath/gcc-4.[0-9]*.$platformstr.rpm
	rpm -ivh $cdrootpath/kernel-source-[0-9]*.$platformstr.rpm
	rpm -ivh $noarchpath/kernel-devel-[0-9]*.rpm
	uname -r | grep "default" > /dev/null
	if [ $? == 0 ]; then
		rpm -ivh $cdrootpath/kernel-default-devel-[0-9]*.$platformstr.rpm
	else
		rpm -ivh $cdrootpath/kernel-xen-devel-[0-9]*.$platformstr.rpm
	fi
	rpm -ivh $cdrootpath/m4-[0-9]*.$platformstr.rpm
	rpm -ivh $cdrootpath/flex-[0-9]*.$platformstr.rpm
else
	if [ $isel8 = 1 ]; then
		rpm -ivh $cdbasepath/make-[0-9]*.$platformstr.rpm
		rpm -ivh $cdbasepath/mpfr-[0-9]*.$platformstr.rpm
		rpm -ivh $cdrootpath/libmpc-[0-9]*.$platformstr.rpm
		rpm -ivh $cdbasepath/libgomp-[0-9]*.$platformstr.rpm
		rpm -ivh $cdrootpath/ppl-[0-9]*.$platformstr.rpm
		rpm -ivh $cdrootpath/cloog-ppl-[0-9]*.$platformstr.rpm
		rpm -ivh $cdbasepath/kernel-headers-[0-9]*.$platformstr.rpm
		rpm -ivh $cdbasepath/libxcrypt-[0-9]*.$platformstr.rpm
		rpm -ivh $cdbasepath/libxcrypt-devel-[0-9]*.$platformstr.rpm --nodeps --force
		rpm -ivh $cdbasepath/glibc-devel-[0-9]*.$platformstr.rpm --nodeps --force
		rpm -ivh $cdbasepath/glibc-headers-[0-9]*.$platformstr.rpm
		rpm -ivh $cdrootpath/cpp-[0-9]*.$platformstr.rpm
		rpm -ivh $cdrootpath/isl-[0-9]*.$platformstr.rpm
		rpm -ivh $cdrootpath/gcc-[0-9]*.$platformstr.rpm
		rpm -ivh $cdbasepath/kernel-devel-[0-9]*.$platformstr.rpm
		rpm -ivh $cdbasepath/m4-[0-9]*.$platformstr.rpm
		rpm -ivh $cdrootpath/flex-[0-9]*.$platformstr.rpm
		rpm -ivh $cdbasepath/zlib-[0-9]*.$platformstr.rpm
		rpm -ivh $cdbasepath/zlib-devel-[0-9]*.$platformstr.rpm
		rpm -ivh $cdbasepath/elfutils-libelf-devel-[0-9]*.$platformstr.rpm
	else
		rpm -ivh $cdrootpath/make-[0-9]*.$platformstr.rpm
		rpm -ivh $cdrootpath/mpfr-[0-9]*.$platformstr.rpm
		rpm -ivh $cdrootpath/libmpc-[0-9]*.$platformstr.rpm
		rpm -ivh $cdrootpath/libgomp-[0-9]*.$platformstr.rpm
		rpm -ivh $cdrootpath/ppl-[0-9]*.$platformstr.rpm
		rpm -ivh $cdrootpath/cloog-ppl-[0-9]*.$platformstr.rpm
		rpm -ivh $cdrootpath/kernel-headers-[0-9]*.$platformstr.rpm
		rpm -ivh $cdrootpath/glibc-headers-[0-9]*.$platformstr.rpm
		rpm -ivh $cdrootpath/glibc-devel-[0-9]*.$platformstr.rpm
		rpm -ivh $cdrootpath/cpp-[0-9]*.$platformstr.rpm
		rpm -ivh $cdrootpath/gcc-[0-9]*.$platformstr.rpm
		rpm -ivh $cdrootpath/kernel-devel-[0-9]*.$platformstr.rpm
		rpm -ivh $cdrootpath/m4-[0-9]*.$platformstr.rpm
		rpm -ivh $cdrootpath/flex-[0-9]*.$platformstr.rpm
	fi
fi
echo "相关安装包已经成功安装完毕，您可以执行install.sh开始安装ServHA Mirror了！"

clearup
exit 0
