#!/bin/bash
local_dir="/opt/packer"
sys_log="/var/log/install.log"
huayunrepo="http://117.48.207.245/cmigrate/"
ver="1.3.0"

INSTALLSUCCEES="0"
INSTALLFAILED="1"
REPOACCESSABLE="2"
REPONOTACCESSABLE="3"
NOSUITABLEPACKAGE="4"

log () {
	date=`date +"%Y-%m-%d %H:%M:%S"`
	para=$1
	echo "[$date] $1" >> $sys_log
}

is_repo_accessable () {
	urlstatus=`curl -s -m 5 -IL "$1" | grep 200`
	if [ "$urlstatus" == "" ];then
		echo "$REPONOTACCESSABLE"
	else
		echo "$REPOACCESSABLE"
	fi
}

get_os_version_full () {
	if [ -f "/etc/redhat-release" ];then
		ret=`cat /etc/redhat-release`
		if [[ "$ret" =~ "Linux" ]];then
			ret=`cat /etc/redhat-release | cut -d " " -f 4`
			echo $ret
		else
			ret=`cat /etc/redhat-release | cut -d " " -f 3`
			echo $ret
		fi
	else
		ret=`cat /etc/issue | cut -d " " -f 2`
		echo $ret
	fi
}

get_os_version_part () {
	if [ -f "/etc/redhat-release" ];then
		ret=`cat /etc/redhat-release`
		if [[ "$ret" =~ "Linux" ]];then
			ret1=`cat /etc/redhat-release | cut -d " " -f 4 | cut -f "." -f 1`
			ret2=`cat /etc/redhat-release | cut -d " " -f 4 | cut -f "." -f 2`
			echo $ret1.$ret2
		else
			ret1=`cat /etc/redhat-release | cut -d " " -f 3 | cut -f "." -f 1`
			ret2=`cat /etc/redhat-release | cut -d " " -f 3 | cut -f "." -f 2`
			echo $ret1.$ret2
		fi
	else
		ret1=`cat /etc/issue | cut -d " " -f 2 | cut -f "." -f 1`
		ret2=`cat /etc/issue | cut -d " " -f 2 | cut -f "." -f 2`
		echo $ret1.$ret2
	fi
}

localinstall_kerneldevel () {
	kernelver=`uname -r`
	kerneldev="kernel-devel-"$kernelver".rpm"
	ret=`yum localinstall -y $local_dir/$kerneldev 2>&1 >> $sys_log`
	ret=`rpm -qa | grep "kernel-devel-"$kernelver""`
	if [ -z "$ret" ];then
		log "local install kernel devel $local_dir/$kerneldev failed"
		echo "$INSTALLFAILED"
		return
	fi

	echo "$INSTALLSUCCESS"
}

huayuninstall_kerneldevel () {
	ret=`is_repo_accessable "$huayunrepo"`
	if [ "$ret" = "$REPONOTACCESSABLE" ];then
		log "huayunrepo install kernel devel failed, huayunrepo is not accessable"
		echo "$REPONOTACCESSABLE" 
		return
	fi

	kernelver=`uname -r`
	kerneldev="kernel-devel-"$kernelver".rpm"
	osversion=`get_os_version_full`
	ret=`curl $huayunrepo/kernel-devel/$osversion/$kerneldev -f -o $local_dir/$kerneldev 2>&1 >> $sys_log`
	sleep 1
	if [ ! -f "$local_dir/$kerneldev" ];then
		log "huayun repo install kernel devel $kerneldev failed, errmsg($ret)"
		echo "$NOSUITABLEPACKAGE"
		return
	fi

	ret=`yum localinstall -y $local_dir/$kerneldev 2>&1 >> $sys_log`
	ret=`rpm -qa | grep "kernel-devel-"$kernelver""`
	if [ -z "$ret" ];then
		log "huayunrepo install kernel devel $local_dir/$kerneldev failed"
		echo "$INSTALLFAILED"
		return
	fi

	echo "$INSTALLSUCCESS"
}

install_kerneldevel () {
	ret=`localinstall_kerneldevel`
	if [ "$ret" = "$INSTALLSUCESS" ];then
		log "local install kernel devel succeeded"
		echo "$ret"
		return
	fi

	log "local install kernel devel failed, keep trying from huayun repo"
	ret=`huayuninstall_kerneldevel`
	if [ "$ret" = "$INSTALLSUCCESS" ];then
		log "huayun repo install kernel devel succeeded"
		echo "$ret"
		return
	fi

	log "huayun repo install kernel devel failed"
	echo "$ret"
}

localinstall_datto () {
	if [ -f "/usr/bin/yum" ];then
		ret=`yum install dkms-dattobd dattobd-utils -y 2>&1 >> $sys_log && modprobe dattobd 2>&1 >> $sys_log`
		ret=`lsmod | grep dattobd`
		if [ -z "$ret" ];then
			log "local install datto failed"
			echo "$INSTALLFAILED"
			return
		fi
		echo "$INSTALLSUCCESS"
		return
	fi
	
	if [ -f "/usr/bin/apt" ];then
		ret=`apt-get install dattobd-dkms dattobd-utils -y --allow-unauthenticated 2>&1 >> $sys_log && modprobe dattobd 2>&1 >> $sys_log`
		ret=`lsmod | grep dattobd`
		if [ -z "$ret" ];then
			log "local install datto failed"
			echo "$INSTALLFAILED"
			return
		fi
		echo "$INSTALLSUCCESS"
		return
	fi

	log "internet install datto failed, neither yum nor apt-get found"
	echo "$INSTALLFAILED"
}

internetinstall_datto () {
	if [ -f "/usr/bin/yum" ];then
		ret=`yum install https://cpkg.datto.com/datto-rpm/repoconfig/datto-el-rpm-release-$(rpm -E %rhel)-latest.noarch.rpm -y 2>&1 >> $sys_log`
		ret=`rpm -qa | grep datto-el-rpm-release`
		if [ -z "$ret" ];then
			log "internet install datto failed, datto-el-rpm-release"
			echo "$INSTALLFAILED"
			return
		fi
		`sed -i "s/mirrorlist=https/mirrorlist=http/" /etc/yum.repos.d/epel.repo 2>&1 > /dev/null`
		ret=`yum install dkms-dattobd dattobd-utils -y 2>&1 >> $sys_log && modprobe dattobd 2>&1 >> $sys_log`
		ret=`lsmod | grep dattobd`
		if [ -z "$ret" ];then
			log "internet install datto failed, dattobd"
			echo "$INSTALLFAILED"
			return
		fi
		echo "$INSTALLSUCCESS"
		return
	fi
	
	if [ -f "/usr/bin/apt" ];then
		ret=`apt-get update 2>&1 >> $sys_log && apt-get install gnupg-curl -y 2>&1 >> $sys_log`
		ret=`dpkg -l 2>&1 | grep gnupg-curl 2>&1`
		if [[ ! "$ret" =~ "gnupg-curl" ]];then
			log "internet install datto failed, gnupg-curl"
			echo "$INSTALLFAILED"
			return
		fi

		ret=`apt-key adv --fetch-keys https://cpkg.datto.com/DATTO-PKGS-GPG-KEY 2>&1 >> $sys_log`
		ret=`apt-key list | grep datto`
		if [ -z "$ret" ];then
			log "internet install datto failed, fetch key"
			echo "$INSTALLFAILED"
			return
		fi

		ret=`echo "deb [arch=amd64] https://cpkg.datto.com/datto-deb/public/$(lsb_release -sc) $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/datto-linux-agent.list 2>&1 >> $sys_log`

		ret=`apt-get update 2>&1 >> $sys_log`
		
		ret=`apt-get install dattobd-dkms dattobd-utils -y --allow-unauthenticated 2>&1 >> $sys_log && modprobe dattobd 2>&1 >> $sys_log`
		ret=`lsmod | grep dattobd`
		if [ -z "$ret" ];then
			log "internet install datto failed, dattobd"
			echo "$INSTALLFAILED"
			return
		fi
		echo "$INSTALLSUCCESS"
		return
	fi

	log "internet install datto failed, neither yum nor apt-get found"
	echo "$INSTALLFAILED"
}

install_datto () {
	ret=`localinstall_datto`
	if [ "$ret" = "$INSTALLSUCCESS" ];then
		log "local install datto succeeded"
		echo "$ret"
		return
	fi

	log "local install datto failed, keep trying from internet"
	ret=`internetinstall_datto`
	if [ "$ret" = "$INSTALLSUCCESS" ];then
		log "internet install datto succeeded"
		echo "$ret"
		return
	fi

	echo "$ret"
}

install_dmidecode() {
	if [ -f "/usr/bin/yum" ];then
		ret=`yum install dmidecode -y 2>&1 >> $sys_log`
		if [ ! -f "/usr/sbin/dmidecode" ]; then
			log "install dmidecode failed"
			echo "$INSTALLFAILED"
		fi
		echo "$INSTALLSUCCESS"
		return
	fi
	
	if [ -f "/usr/bin/apt-get" ];then
		ret=`apt-get install dmidecode -y 2>&1 >> $sys_log`
		if [ ! -f "/usr/sbin/dmidecode" ]; then
			log "install dmidecode failed"
			echo "$INSTALLFAILED"
		fi
		echo "$INSTALLSUCCESS"
		return
	fi
}

localinstall_packer () {
	uname=`uname -a`
	if [[ "$uname" =~ "el6" ]]; then
		if [ -f "$local_dir/packer-$1-linux-centos6.tgz" ];then
			tar -pzxvf $local_dir/packer-$1-linux-centos6.tgz -C / > /dev/null
			echo "$INSTALLSUCCESS"
		else
			log "local install packer packer-$1-linux-centos6.tgz failed, errmsg(package not exits!)"
			echo "$INSTALLFAILED"
		fi
	else
		if [ -f "$local_dir/packer-$1-linux-generic.tgz" ];then
			tar -pzxvf $local_dir/packer-$1-linux-generic.tgz -C / > /dev/null
			echo "$INSTALLSUCCESS"
		else
			log "local install packer packer-$1-linux-generic.tgz failed, errmsg(package not exits!)"
			echo "$INSTALLFAILED"
		fi
	fi
}

huayuninstall_packer () {
	ret=`is_repo_accessable "$huayunrepo"`
	if [ "$ret" = "$REPONOTACCESSABLE" ];then
		log "huayunrepo install packer failed, huayunrepo is not accessable"
		echo "$REPONOTACCESSABLE" 
		return
	fi

	uname=`uname -a`
	if [[ "$uname" =~ "el6" ]]; then
		ret=`curl $huayunrepo/v$1/packer-$1-linux-centos6.tgz -f -o $local_dir/packer-$1-linux-centos6.tgz 2>&1 >> $sys_log`
		sleep 1
		if [ ! -f "$local_dir/packer-$1-linux-centos6.tgz" ];then
			log "huayun repo install packer packer-$1-linux-centos6.tgz failed, errmsg($ret)"
			echo "$NOSUITABLEPACKAGE"
			return
		fi
		tar -pzxvf $local_dir/packer-$1-linux-centos6.tgz -C / > /dev/null
	else
		ret=`curl $huayunrepo/v$1/packer-$1-linux-generic.tgz -f -o $local_dir/packer-$1-linux-generic.tgz 2>&1 >> $sys_log`
		sleep 1
		if [ ! -f "$local_dir/packer-$1-linux-generic.tgz" ];then
			log "huayun repo install packer packer-$1-linux-generic.tgz failed, errmsg($ret)"
			echo "$NOSUITABLEPACKAGE"
			return
		fi
		tar -pzxvf $local_dir/packer-$1-linux-generic.tgz -C / > /dev/null
	fi

	echo "$INSTALLSUCCESS"
}

install_packer () {
	ret=`localinstall_packer $1`
	if [ "$ret" = "$INSTALLSUCCESS" ];then
		log "local install packer succeeded"
		packer_service start > /dev/null
		echo "$ret"
		return
	fi

	log "local install packer failed, keep trying from huayun repo"
	ret=`huayuninstall_packer $1`
	if [ "$ret" = "$INSTALLSUCCESS" ];then
		log "huayun repo install succeed succeeded"
		packer_service start > /dev/null
		echo "$ret"
		return
	fi

	log "huayun repo install packer failed"
	echo "$ret"
}

install() {
	if [ -f "/usr/bin/yum" ];then
		echo "Install kernel devel"
		ret=`install_kerneldevel`
		if [ "$ret" = "$INSTALLFAILED" ];then
			echo "Install terminated, install kernel devel failed, please check log for more details"
			exit
		elif [ "$ret" = "$REPONOTACCESSABLE" ];then
			echo "Unable to connect to "$huayunrepo". please check your internet connection and try again."
			exit
		elif [ "$ret" = "$NOSUITABLEPACKAGE" ];then
			echo "Unable to get the kernel-devel installation package for the kernel version, please contact the technician for processing."
			exit
		else
			echo "Install kernel devel success"
		fi
	fi

	echo "Install datto"
	ret=`install_datto`
	if [ "$ret" = "$INSTALLFAILED" ];then
		echo "Install terminated, install datto failed, please check log for more details"
		exit
	elif [ "$ret" = "$REPONOTACCESSABLE" ];then
		echo "Unable to connect to "$huayunrepo". please check your internet connection and try again."
		exit
	elif [ "$ret" = "$NOSUITABLEPACKAGE" ];then
		echo "Unable to get the datto installation package, please contact the technician for processing."
		exit
	else
		echo "Install datto success"
	fi

	echo "Install dmidecode"
	ret=`install_dmidecode`
	if [ "$ret" = "$INSTALLFAILED" ];then
		echo "Install terminated, install dmidecode failed, please check log for more details"
		exit
	else
		echo "Install dmidecode success"
	fi

	echo "Install packer"
	ret=`install_packer $1`
	if [ "$ret" = "$INSTALLFAILED" ];then
		echo "Install terminated, install packer failed, please check log for more details"
		exit
	elif [ "$ret" = "$REPONOTACCESSABLE" ];then
		echo "Unable to connect to "$huayunrepo". please check your internet connection and try again."
		exit
	elif [ "$ret" = "$NOSUITABLEPACKAGE" ];then
		echo "Unable to get the packer installation package, please contact the technician for processing."
		exit
	else
		echo "Install packer success"
	fi
}

main () {
	echo "Install start, ver:$ver"
	install $ver
	echo "Successful installation!"
}

if [ ! -d "$local_dir" ];then
	mkdir -p $local_dir
fi

main
