#!/bin/sh

# customized items of package
if [ -z "$SYNOPKG_PKGNAME" ]; then
	# prior DSM 3.2, start script will be run without environment variable
	#  at boot time, thus we need a workaround to gather name and version info
	PACKAGE_NAME="RadiusServer"
	PACKAGE_VER=`get_key_value "/var/packages/${PACKAGE_NAME}/INFO" "version"`
else
	PACKAGE_NAME=$SYNOPKG_PKGNAME
	PACKAGE_VER=$SYNOPKG_PKGVER
fi

#for script debug
#SYNOPKG_PKGDEST="/var/packages/RadiusServer/target"
VPN_INF="/var/packages/VPNCenter/INFO"
RAD_SZD_LOG=$SYNOPKG_PKGDEST"/var/log/radius"
RAD_SZD_PID=$SYNOPKG_PKGDEST"/var/run/radiusd"
RAD_APP=$SYNOPKG_PKGDEST"/app"
RAD_IDX=$SYNOPKG_PKGDEST"/indexdb"
RAD_PID=$RAD_SZD_PID"/radiusd.pid"
RAD_LOG=$RAD_SZD_LOG"/radius.log"
RAD_BIN=$SYNOPKG_PKGDEST"/syno_bin/RadiusServer.sh"
LDAP_CLIENT="/usr/syno/sbin/synoldapclient"
RAD_SZF_CONF="/usr/local/synoradius/synoconf"
RAD_SZK_BACK_END_TYPE="back_end_type"
RAD_SZV_BACK_END_LDAP="ldap"
RAD_SZV_BACK_END_AD="ad"
RAD_SZV_BACK_END_LOCAL="local"

RAD_NTLM_AUTH_BIN="/usr/syno/bin/ntlm_auth"
RAD_NTLM_AUTH_BIN_32=$SYNOPKG_PKGDEST"/syno_bin/ntlm_auth/ntlm_auth_32"
RAD_NTLM_AUTH_BIN_36=$SYNOPKG_PKGDEST"/syno_bin/ntlm_auth/ntlm_auth_36"

DSM_INDEX_ADD="/usr/syno/bin/pkgindexer_add"
DSM_INDEX_DEL="/usr/syno/bin/pkgindexer_del"

RAD_F_LOG_TMP="/tmp/synoradius.install.log"
RAD_B_HAS_LOG=0

isVpnNeedUpdate() {
	if [ ! -f $VPN_INF ]; then
		return 0
	fi

	local VPN_VER=`get_key_value "${VPN_INF}" "version"|cut -d "-" -f 1`
	local VPN_BUILD=`get_key_value "${VPN_INF}" "version"|cut -d "-" -f 2`
	local VPN_MAJOR=`echo ${VPN_VER}|cut -d "." -f 1`
	local VPN_MINOR=`echo ${VPN_VER}|cut -d "." -f 2`
	if [ $VPN_MAJOR -gt 1 ]; then
		return 0
	elif [ $VPN_MAJOR -eq 1 ] && [ $VPN_MINOR -gt 0 ] && [ $VPN_BUILD -ge 2262 ]; then
		return 0
	else
		return 1
	fi
}

isNeedForceLocal() {
	local blForceLocal=1
	local domainJoined=''
	local back_end_type=''

	back_end_type=`get_key_value $RAD_SZF_CONF $RAD_SZK_BACK_END_TYPE`

	if [ "$RAD_SZV_BACK_END_LDAP" == "$back_end_type" ]; then
		$LDAP_CLIENT --status | head -n 1 | grep "is binded"
		if [ 0 -eq $? ]; then
			blForceLocal=0
		fi
	elif [ "$RAD_SZV_BACK_END_AD" == "$back_end_type" ]; then
		domainJoined=`get_key_value /etc/synoinfo.conf domainjoined`
		if [ "yes" == "$domainJoined" ]; then
			blForceLocal=0
		fi
	elif [ "$RAD_SZV_BACK_END_LOCAL" == "$back_end_type" ]; then
		blForceLocal=0
	fi

	return $blForceLocal
}

mkdir -p $RAD_SZD_LOG
mkdir -p $RAD_SZD_PID

case $1 in
	start)
		if [ "x${SYNOPKG_PKG_STATUS}" = "xINSTALL" ] && [ -f $RAD_F_LOG_TMP ]; then
			cat $RAD_F_LOG_TMP > $SYNOPKG_TEMP_LOGFILE
			RAD_B_HAS_LOG=1
			rm -f $RAD_F_LOG_TMP
		fi
		isVpnNeedUpdate
		if [ 1 -eq $? ]; then
			echo `date +"%a %b %d %T %Y"`" : Error: You need to upgrade the VPN Server to the version 1.1-2262 or later." >> ${RAD_LOG}
			if [ 1 -eq $RAD_B_HAS_LOG ]; then
				echo "You need to upgrade the VPN Server to the version 1.1-2262 or later." >> $SYNOPKG_TEMP_LOGFILE
			else
				echo "You need to upgrade the VPN Server to the version 1.1-2262 or later." > $SYNOPKG_TEMP_LOGFILE
			fi
			exit 1
		fi
		if [ ! -f $RAD_NTLM_AUTH_BIN ]; then
			local NTLM_AUTH_VER=`smbstatus -V|grep Version|cut -d " " -f 2`
			local NTLM_AUTH_MAJOR=`echo $NTLM_AUTH_VER|cut -d "." -f 1`
			local NTLM_AUTH_MINOR=`echo $NTLM_AUTH_VER|cut -d "." -f 2`
			# if samba version is 4 or later, DSM should have ntlm_auth
			if [ 3 -ne $NTLM_AUTH_MAJOR ]; then
				echo "Unknow Samba version: "$NTLM_AUTH_VER > $SYNOPKG_TEMP_LOGFILE
				exit 1
			elif [ 2 -eq $NTLM_AUTH_MINOR ]; then
				if [ -f $RAD_NTLM_AUTH_BIN_32 ]; then
					cp -f $RAD_NTLM_AUTH_BIN_32 $RAD_NTLM_AUTH_BIN
				else
					echo "ntlm_auth version not match. " > $SYNOPKG_TEMP_LOGFILE
					exit 1
				fi
			elif [ 6 -eq $NTLM_AUTH_MINOR ]; then
				if [ -f $RAD_NTLM_AUTH_BIN_36 ]; then
					cp -f $RAD_NTLM_AUTH_BIN_36 $RAD_NTLM_AUTH_BIN
				else
					echo "ntlm_auth version not match. " > $SYNOPKG_TEMP_LOGFILE
					exit 1
				fi
			else
				echo "Unknow Samba version: "$NTLM_AUTH_VER > $SYNOPKG_TEMP_LOGFILE
				exit 1
			fi
		fi

		isNeedForceLocal
		if [ 1 -eq $? ]; then
			$RAD_BIN uselocal
		fi

		$RAD_BIN restart

		# index help and add help to dsm
		${DSM_INDEX_ADD} ${RAD_APP}/index.conf ${RAD_IDX}/appindexdb
		${DSM_INDEX_ADD} ${RAD_APP}/helptoc.conf ${RAD_IDX}/helpindexdb
		exit 0
	;;
	stop)
		# remove index and help entry
		${DSM_INDEX_DEL} ${RAD_APP}/index.conf ${RAD_IDX}/appindexdb
		${DSM_INDEX_DEL} ${RAD_APP}/helptoc.conf ${RAD_IDX}/helpindexdb

		if [ -f $RAD_PID ]; then
			kill -TERM `cat ${RAD_PID}`
		fi
		exit 0
	;;
	status)
		isVpnNeedUpdate
		if [ 1 -eq $? ]; then
			exit 1
		fi
		COUNT_RAD=`ps | grep -c radiusd`
		if [ -f $RAD_PID ] && [ 0 -lt $COUNT_RAD ]; then
			exit 0
		else
			exit 1
		fi
	;;
	killall)
		killall radiusd
        ;;
	log)
		echo ${RAD_LOG}
		exit 0
	;;
esac

