#!/bin/bash

current_script=$(cd `dirname $0`;pwd)
jyn_install_path=$(dirname "$current_script")
arr_svr_name=(jynsafed jynzdfy)
RC_LOCAL_PATH="/etc/rc.d/rc.local"
RC_LOCAL_PATH2="/etc/rc.local"
system_path="/lib/systemd/system/"

init_system_path()
{
    # 兼容麒麟V11
    if [ -f /etc/os-release ] && [ -d /opt/system/lib/systemd/system ]; then
        system=`cat /etc/os-release | grep "VERSION_US=" | cut -d '=' -f 2`
        [[ $system =~ "Kylin" ]] && system_path="/opt/system/lib/systemd/system"
    fi
    #issueinfo=`cat /etc/issue`
    #if [[ $issueinfo =~ "uos" || $issueinfo =~ "UOS" || $issueinfo =~ "UnionTech" ]] ; then
    #    system_path="$jyn_install_path/../entries/services"
	#	mk#dir -p $system_path
    #fi
}

write_local()
{
	local_path=$1
	svr_name=$2
	if test -f $local_path; then
		cnt=`grep -c $svr_name $local_path`
		HAS_EXIT=`grep -c '^exit 0' $local_path`
		if test $cnt -eq 0; then
			if [ $HAS_EXIT -eq 1 ]; then
				#在exit 0 前插入运行bash
				sed -i "/^exit 0/i${jyn_install_path/bin//\//\\\/}\/$svr_name start" $local_path
			else
				echo $jyn_install_path/bin/$svr_name start >> $local_path
			fi
			cp $jyn_install_path/bin/$svr_name /usr/local/bin/
			return 0
		elif test $cnt -eq 1; then
			return 1
		fi
	fi
	return 1
}

add_local()
{
	svr_name=$1
	#susu 特殊处理
	if test -f /etc/SuSE-release; then
		echo "system: suse"
		if test ! -f /etc/init.d/after.local; then
			echo "create after.local"
			touch /etc/init.d/after.local
			chmod +x /etc/init.d/after.local
		fi

		cnt=`grep -c $svr_name /etc/init.d/after.local`
		if test $cnt -eq 0; then
			echo "write after.local"
			echo $jyn_install_path/bin/$svr_name start >> /etc/init.d/after.local
			cp $jyn_install_path/bin/$svr_name /usr/local/bin/
		elif test $cnt -eq 1; then
			echo "already setted"
		fi
	else
		echo "system: common"
		write_local $RC_LOCAL_PATH $svr_name
		ret1=$?
		write_local $RC_LOCAL_PATH2 $svr_name
		ret2=$?
		if [ ${ret1} == 0 ] ; then
			echo "add $svr_name local to: $RC_LOCAL_PATH"
		elif [ "${ret2}" == "0" ] ; then
			echo "add $svr_name local to: $RC_LOCAL_PATH2"
		else
			echo "startup not suppored yet"
		fi
	fi

	#check x
	[ -f /etc/rc.d/rc.local ]   && chmod +x /etc/rc.d/rc.local
    [ -f /etc/rc.local ]        && chmod +x /etc/rc.local
}


wipeoff_local()
{
	local_path=$1
	svr_name=$2
	if test -f $local_path; then
		sed -i "/$svr_name/d" $local_path
		rm /usr/local/bin/$svr_name
	fi
	return 1
}

del_local()
{
	svr_name=$1
	#susu 特殊处理
	if test -f /etc/SuSE-release; then
		echo "system: suse"
		if test -f /etc/init.d/after.local; then
			cnt=`grep -c $svr_name /etc/init.d/after.local`
			if test $cnt -eq 0; then
				echo $jyn_install_path/bin/$svr_name start >> /etc/init.d/after.local
				sed -i "/$svr_name/d" /etc/init.d/after.local
			elif test $cnt -eq 1; then
				rm /usr/local/bin/$svr_name
			fi
		fi
	else
		echo "system: common"
		wipeoff_local $RC_LOCAL_PATH $svr_name
		ret1=$?
		wipeoff_local $RC_LOCAL_PATH2 $svr_name
		ret2=$?
		if [ ${ret1} == 0 ] ; then
			echo "del $svr_name local from : $RC_LOCAL_PATH"
		elif [ ${ret2} == 0 ] ; then
			echo "del $svr_name local from : $RC_LOCAL_PATH2"
		else
			echo "startup not suppored yet"
		fi
	fi
}

install()
{	
	for svr_name in ${arr_svr_name[@]}; do
		if command -v "systemctl" &> /dev/null ; then
			systemctl daemon-reload	
			#做个容错
			if test -f /etc/init.d/${svr_name}; then
				rm -f /etc/init.d/${svr_name}
			fi
			if [[ -d $system_path && -f $jyn_install_path/scripts/${svr_name}.service ]] ; then
				cp $jyn_install_path/scripts/${svr_name}.service $system_path
				systemctl enable ${svr_name}.service
				echo "systemctl enable ${svr_name}.service successful"
			else
				[ -d "$system_path" ] || echo "$system_path is not found!!!"
				[ -f "$jyn_install_path/scripts/${svr_name}.service" ] || echo "$jyn_install_path/scripts/${svr_name}.service is not found!!!"
			fi
			systemctl daemon-reload	
		else
			if test -f $jyn_install_path/scripts/$svr_name ; then
				if command -v "update-rc.d" &> /dev/null ; then
					cp -vf $jyn_install_path/scripts/$svr_name /etc/init.d/$svr_name
				elif command -v "chkconfig" &> /dev/null ; then
					cp -vf $jyn_install_path/scripts/$svr_name /etc/init.d/$svr_name
				else
					echo "enable rc.local $svr_name"
					add_local $svr_name
				fi
			fi
			if test -f /etc/init.d/$svr_name; then
				if command -v "update-rc.d" &> /dev/null ; then
					update-rc.d $svr_name defaults
					echo "update-rc.d $svr_name"
				elif command -v "chkconfig" &> /dev/null ; then
					chkconfig --add $svr_name
					echo "chkconfig $svr_name"
				fi
			fi
		fi
	done
}

uninstall()
{
	for svr_name in ${arr_svr_name[@]}; do
		#涉密版本使用systemctl
		if command -v "systemctl" &> /dev/null ; then
			echo "systemctl disable ${svr_name}.service"
			if [ "`systemctl list-unit-files | grep ${svr_name}.service`" != "" ]; then
				systemctl disable ${svr_name}
				rm -f /etc/systemd/system/multi-user.target.wants/${svr_name}.service
				rm -f $system_path/${svr_name}.service
				echo "uninstall systemctl:"
				echo $system_path/${svr_name}.service
			fi
			systemctl daemon-reload	
		#非密版本使用updatercd/chkconfig
		else
			if test -f /etc/init.d/$svr_name ; then
				if command -v "update-rc.d" &> /dev/null ; then
					echo "disable update-rc.d $svr_name"
					update-rc.d -f $svr_name remove
					if [ "$1"e = ""e ] ; then
						rm /etc/init.d/$svr_name
                			fi
				elif command -v "chkconfig" &> /dev/null ; then
					echo "disable chkconfig $svr_name"
					chkconfig --del $svr_name
					if [ "$1"e = ""e ] ; then
						rm -f /etc/init.d/$svr_name
                			fi
				else
					echo "disable rc.local $svr_name"
					del_local $svr_name
				fi
				rm -f /etc/init.d/$svr_name
			fi
		fi
	done
}

case "$1" in
    install)
		init_system_path
        install
        ;;
    uninstall)
		init_system_path
        uninstall $2
        ;;
    *)
        echo $"Usage: $0 {install|uninstall}"
        exit 1
esac

exit 0
