#!/bin/bash

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

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//\//\\\/}\/$svr_name start" $local_path
			else
				echo $jyn_install_path/$svr_name start >> $local_path
			fi
			cp $jyn_install_path/$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/$svr_name start >> /etc/init.d/after.local
			cp $jyn_install_path/$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
	if [ -f /etc/rc.d/rc.local ]; then
		chmod +x /etc/rc.d/rc.local
	fi

	if [ -f /etc/rc.local ]; then
		chmod +x /etc/rc.local
	fi
}


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/$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()
{	
	svr_condig_path=Config/svrconfig
	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/${svr_condig_path}/${svr_name}.service ]] ; then
				cp $jyn_install_path/${svr_condig_path}/${svr_name}.service $system_path
				systemctl enable ${svr_name}.service
				echo "systemctl enable ${svr_name}.service successful"
			fi
			systemctl daemon-reload	
		#非密版本使用updatercd/chkconfig
		else
			#做个容错
			if [ "`systemctl list-unit-files | grep ${svr_name}.service`" != "" ]; then
				rm -f /etc/systemd/system/multi-user.target.wants/${svr_name}.service
			fi
			if test -f $jyn_install_path/${svr_condig_path}/$svr_name ; then
				if command -v "update-rc.d" &> /dev/null ; then
					cp -vf $jyn_install_path/${svr_condig_path}/$svr_name /etc/init.d/$svr_name
				elif command -v "chkconfig" &> /dev/null ; then
					cp -vf $jyn_install_path/${svr_condig_path}/$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
			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)
        install
        ;;
    uninstall)
        uninstall $2
        ;;
    *)
        echo $"Usage: $0 {install|uninstall}"
        exit 1
esac

exit 0
