#!/bin/sh
# dm24-support/src/adc-command/adc-command
#
#  Copyright 2011-2017 Guralp Systems Limited.
#  Author: Laurence Withers <lwithers@guralp.com>
#

usage() {
	cat <<EOF
usage: adc-command <module> <command> [options...]

======== Available ADC modules ========
EOF
	sensor-control --list
	cat <<EOF

======== Available commands ========
mass-centre:    Centre the instrument's masses.
mass-lock:      Lock the masses for transport.
mass-unlock:    Unlock the masses after installation.

calib-sine:     Perform a calibration using a sine wave.
calib-step:     Perform a calibration using a square wave.
                The signal consists of a positive step of the given duration,
                followed by a negative step of the same duration.
calib-noise:    Perform a calibration using white noise.
calib-brnoise:  Perform a calibration using brown noise.

Run 'adc-command help <command>' for more details.
EOF
	exit 1
}

help_mass_centre() {
	cat <<EOF
======== Help (mass-centre) ========

Centre the instrument's masses.

No options.
EOF
}

help_mass_lock() {
	cat <<EOF
======== Help (mass-lock) ========

Lock the masses for transport.

No options.
EOF
}

help_mass_unlock() {
	cat <<EOF
======== Help (mass-unlock) ========

Unlock the masses after installation.

No options.
EOF
}

help_calib_sine() {
	cat <<EOF
======== Help (calib-sine) ========

Perform a calibration using a sine wave.

Available options:

component:      Component
                Available values: (e.g. 'component=ALL')
                ALL:    All
                Z:      Vertical
                N/S:    North/South
                E/W:    East/West
duration:       Duration in minutes
                (e.g. 'duration=2')
amplitude:      Amplitude (percentage)
                (e.g. 'amplitude=100')
freq:           Frequency (Hz) or period (seconds)
                (e.g. 'freq=1')
freq_or_period: Units
                Available values: (e.g. 'freq_or_period=HZ')
                HZ:     Hz
                SECOND: sec
EOF
}

help_calib_step() {
	cat <<EOF
======== Help (calib-step) ========

Perform a calibration using a square wave.
The signal consists of a positive step of the given duration,
followed by a negative step of the same duration.

Available options:

component:      Component
                Available values: (e.g. 'component=ALL')
                ALL:    All
                Z:      Vertical
                N/S:    North/South
                E/W:    East/West
duration:       Duration in minutes
                (e.g. 'duration=2')
amplitude:      Amplitude (percentage)
                (e.g. 'amplitude=100')
EOF
}

help_calib_noise() {
	cat <<EOF
======== Help (calib-noise) ========

Perform a calibration using white noise.

Available options:

component:      Component
                Available values: (e.g. 'component=ALL')
                ALL:    All
                Z:      Vertical
                N/S:    North/South
                E/W:    East/West
duration:       Duration in minutes
                (e.g. 'duration=2')
amplitude:      Amplitude (percentage)
                (e.g. 'amplitude=100')
EOF

}

help_calib_brnoise() {
	cat <<EOF
======== Help (calib-brnoise) ========

Perform a calibration using brown noise.

Available options:

component:      Component
                Available values: (e.g. 'component=ALL')
                ALL:    All
                Z:      Vertical
                N/S:    North/South
                E/W:    East/West
duration:       Duration in minutes
                (e.g. 'duration=2')
amplitude:      Amplitude (percentage)
                (e.g. 'amplitude=100')
EOF

}



#
# Process commandline options
#

[ $# -lt 2 ] && usage

if [ "$1" = "help" -o "X$1" = "X--help" -o "X$1" = "X-h" ]
then
	case "$2" in
	mass-centre)	help_mass_centre	;;
	mass-lock)	help_mass_lock		;;
	mass-unlock)	help_mass_unlock	;;
	calib-sine)	help_calib_sine		;;
	calib-step)	help_calib_step		;;
	calib-noise)	help_calib_noise	;;
	calib-brnoise)	help_calib_brnoise	;;
	*)	echo "Unrecognised control!"
		echo "Options: mass-centre, mass-lock, mass-unlock,"
		echo "         calib-sine, calib-step or calib-noise"
		;;
	esac
	exit 0
fi

sensor_id="$1"

case "$2" in
mass-centre)
	sensor-control "${sensor_id}" mass_centre
	exit $?
	;;
mass-lock)
	sensor-control "${sensor_id}" mass_lock
	exit $?
	;;
mass-unlock)
	sensor-control "${sensor_id}" mass_unlock
	exit $?
	;;
calib-sine)
	cal_type="calib_single_freq"
	;;
calib-step)
	cal_type="calib_step"
	;;
calib-noise)
	cal_type="calib_bbnoise"
	;;
calib-brnoise)
	cal_type="calib_brnoise"
	;;
*)
	echo "Unrecognised control $2!"
	exit 1
esac

shift
shift
cal_invert_freq="0"
while [ $# -ne 0 ]
do
	case "$1" in
	component=*)
		cal_component="`echo $1 | cut -d= -f2`"
		case "${cal_component}" in
		N/S) cal_component="N" ;;
		E/W) cal_component="E" ;;
		esac
		;;
	duration=*)
		cal_duration="`echo $1 | cut -d= -f2`"
		cal_duration="`dc 60 ${cal_duration} '*' p`"
		;;
	amplitude=*)
		cal_amplitude="`echo $1 | cut -d= -f2`"
		;;
	freq=*)
		cal_frequency="`echo $1 | cut -d= -f2`"
		;;
	freq_or_period=*)
		t="`echo $1 | cut -d= -f2`"
		case "${t}" in
		HZ)
			true
			;;
		SECOND)
			cal_invert_freq="1"
			;;
		*)
			echo "$1 invalid: expecting HZ or SECOND"
			exit 1
		esac
		;;
	*)
		echo "Unrecognised parameter $1!"
		exit 1
	esac
	shift
done

if [ "${cal_invert_freq}" -ne 0 ]
then
	cal_frequency="`dc 1 ${cal_frequency} / p`"
fi

cal_params=""
[ -n "${cal_component}" ] && cal_params="${cal_params} component=${cal_component}"
[ -n "${cal_duration}" ] && cal_params="${cal_params} duration=${cal_duration}"
[ -n "${cal_amplitude}" ] && cal_params="${cal_params} amplitude=${cal_amplitude}"
[ -n "${cal_frequency}" ] && cal_params="${cal_params} frequency=${cal_frequency}"

exec sensor-control "${sensor_id}" "${cal_type}" ${cal_params}
