#!/sbin/svc


MYLISTING="Data acquisition process"
CONTROL_GROUP="conf"
CFGFILE="/etc/das-in/das-in.local"
MYVERSION="1.0"

depend() {
	# always injects data into the default gdi-base instance
	depends_on gdi-base
}

start() {
	# This is made more complicated because sometimes a unit is brought up minus any adc
	# boards and so /etc/ioline.local will exist but the adc's won't have been detected.

	run_pinmask=0

	if [ ! -e "/etc/ioline/ioline.local" -o ! -e "/etc/das-in/adc-eeprom.local" ]
	then
		run_pinmask=1
		logger "das-in pinmask-change: Missing config files"
	else

		# This file indicates there is an adc present but we need to work out
		# if we have already run or not.  Check for second then first adc.
		if [ -e "/var/run/gsl-eeprom/adc2" ]
		then
			count="`cf2sections "/etc/das-in/adc-eeprom.local" | grep -c "board_2"`"
			if [ $count -ne 1 ]
			then
				run_pinmask=1
				logger "das-in pinmask-change: Missing board_2 in file"
			fi

		elif [ -e "/var/run/gsl-eeprom/adc1" ]
		then
			count="`cf2sections "/etc/das-in/adc-eeprom.local" | grep -c "board_1"`"
			if [ $count -ne 1 ]
			then
				run_pinmask=1
				logger "das-in pinmask-change: Missing board_1 in file"
			fi
		fi
	fi

	if [ $run_pinmask -ne 0 ]
	then
		echo " * Applying hardware pinmask"
		pinmask-change --confirm

		if [ $? -eq 0 ]
		then
			echo "   - completed"
		else
			echo "*** WARNING: Pinmask did not complete successfully."
			echo "*** WARNING: Please send the above output to support@guralp.com ."
		fi
	fi


	# must run as root as it needs to interact with lots of hardware
	daemonize_orig /usr/sbin/das-in --config "${CFGFILE}"
}

stop() {
	kill_pid
}
