#!/bin/sh
# 4015-migrate-to-gdi-record2.sh
# 
#  Copyright: ©2011-2012, Güralp Systems Ltd.
#  Author: Laurence Withers <lwithers@guralp.com>
#  License: GPLv3
#

RDISKCF="/etc/rdisk/rdisk.local"
OLDCF="/etc/gdi-record/default.local"
NEWCF="/etc/gdi-record/gdi-record.local"
TPL="/etc/gdi-record/gdi-record.tpl"

[ -e "${OLDCF}" ] || exit 0

echo " * Migrating from gdi-record to gdi-record2"

# normally the template would be instantiated by a later script, but since we
# need to edit the instance here we must short-circuit this process
if [ ! -e "${NEWCF}" ]
then
	cp "${TPL}" "${NEWCF}" || exit 1
fi

# copy salient user settings from gdi-record.local
transcribe() {
	local Old_sec="$1"
	local Old_var="$2"
	local New_sec="$3"
	local New_var="$4"

	local Value="`gcs_rwvar 'gcs_get_varcf2' "${Old_var}" "${OLDCF}" "${Old_sec}"`"
	[ -z "${Value}" ] && return 0
	gcs_rwvar 'gcs_set_varcf2' "${New_var}" "${NEWCF}" "${Value}" "${New_sec}"
}

transcribe 'MSEED'	'fill_threshold' 	'ms'		'threshold_pct'
transcribe 'MSEED'	'dbdir' 		'ms'		'dbdir'
transcribe 'MSEED'	'seconds_per_file' 	'ms'		'seconds_per_file'
transcribe 'GCF'	'fill_threshold' 	'gcf'		'threshold_pct'
transcribe 'GCF'	'dbdir' 		'gcf'		'dbdir'
transcribe 'GCF'	'seconds_per_file' 	'gcf'		'seconds_per_file'
transcribe 'DIRCOPY'	'fill_threshold' 	'dircopy'	'threshold_MiB'
transcribe 'DIRCOPY'	'dbdir' 		'dircopy'	'dbdir'
transcribe 'DIRCOPY'	'dname_fmt' 		'dircopy'	'dname_fmt'


transcribe_enable() {
	local Old_sec="$1"
	local New_sec="$2"
	local New_var="$3"
	local New_value="$4"	# default

	gcs_truefalse "`gcs_rwvar 'gcs_get_varcf2' 'disable' "${OLDCF}" "${Old_sec}"`" > /dev/null 2>&1
	case $? in
	0)
		# old: disable = true
		# new: enable = false
		New_value="false"
		;;
	1)
		# old: disable = false
		# new: disable = true
		New_value="true"
		;;
	# other exit codes mean the default value is inherited
	esac

	gcs_rwvar 'gcs_set_varcf2' "${New_var}" "${NEWCF}" "${New_value}" "${New_sec}"
}

transcribe_enable 'MSEED'	'ms'		'enabled'	'false'
transcribe_enable 'GCF'		'gcf'		'enabled'	'true'
	# we don't have a disable switch for dircopy any more
transcribe_enable 'LOGCOPY'	''		'enable_syslog_recording'	'false'


transcribe_dname() {
	local Old_sec="$1"
	local New_sec="$2"

	local V="`gcs_rwvar 'gcs_get_varcf2' 'dname_fmt' "${OLDCF}" "${Old_sec}"`/`gcs_rwvar 'gcs_get_varcf2' 'fname_fmt' "${OLDCF}" "${Old_sec}"`"
	[ "${V}" != '/' ] && gcs_rwvar 'gcs_set_varcf2' 'dname_fmt' "${NEWCF}" "${V}" "${New_sec}"
}

transcribe_dname 'MSEED'	'ms'
transcribe_dname 'GCF'		'gcf'


# done last, so that we can override the sections
if gcs_rwvar 'gcs_is_true_varcf2' 'disable_all' "${OLDCF}" ''
then
	gcs_rwvar 'gcs_set_varcf2' 'enabled' "${NEWCF}" 'false' 'gcf'
	gcs_rwvar 'gcs_set_varcf2' 'enabled' "${NEWCF}" 'false' 'ms'
	gcs_rwvar 'gcs_set_varcf2' 'enable_syslog_recording' "${NEWCF}" 'false' ''
fi

# copy salient user settings from rdisk.local
if [ -e "${RDISKCF}" ]
then
	RVAL=""

	# NB: rdisk_min_free was never respected; ignore for migration purposes
	gcs_rwvar 'gcs_is_true_varcf2' 'recycle' "${RDISKCF}" '' && RVAL="1024"

	gcs_rwvar 'gcs_set_varcf2' 'recycle_min_free' "${NEWCF}" "${RVAL}" ''
fi

# now clean up by removing old files
rm -rf "/etc/rdisk"
rm "${OLDCF}"
