#!/bin/bash
# config-scripts/src/share/rec/rec-top.sh
# 
#  Copyright: ©2011–2012, Güralp Systems Ltd.
#  Author: Laurence Withers <lwithers@guralp.com>
#  License: GPLv3
#

# "fast" navbar processing
if [ "$1" = "--navbar" ]
then
	echo "rec-top	Storage and recording"
	exit 0
fi

# load required support functions
script_dir="`dirname $0`"
. "${script_dir}/functions.sh"
. "${script_dir}/svc_funcs.sh"
. "${script_dir}/gdi2gcf_link.sh"
. "${script_dir}/gdi2miniseed_link.sh"

GDI_RECORD_CF="${CONFIGDIR}/gdi-record/gdi-record.local"
PT_STORAGE_CF="${CONFIGDIR}/Pt-storage/Pt-storage.local"



readvar() {
	local Var="$1"
	local Cffile="$2"
	local Section="$3"

	local Gcsname="o_"
	[ -n "${Section}" ] && Gcsname="${Gcsname}${Section}_"
	Gcsname="${Gcsname}${Var}"

	local Val="`gcs_get_varcf2 "${Var}" "${Cffile}" "${Section}"`"
	[ -n "${Val}" ] && gcs_var "${Gcsname}" "${Val}"
}



do_read() {
	local i Dest Sub

	# defaults
	gcs_var "d_format_fstype"		"fat32"
	gcs_var "d_ms_enabled"			"false"
	gcs_var "d_ms_seconds_per_file"		"1800"
	gcs_var "d_ms_dname_fmt"		"%Y-%j/%Y-%jT%H:%MZ-%s-%c-%n-%l.mseed"
	gcs_var "d_ms_threshold_pct"		"50"
	gcs_var "d_gcf_enabled"			"true"
	gcs_var "d_gcf_seconds_per_file"	"1800"
	gcs_var "d_gcf_dname_fmt"		"%Y-%j/%Y-%jT%H:%MZ.gcf"
	gcs_var "d_gcf_threshold_pct"		"50"
	gcs_var "d_dircopy_dname_fmt"		"%Y-%j/%h"
	gcs_var "d_dircopy_threshold_MiB"	"10"
	gcs_var "d_enable_syslog_recording"	"false"
	gcs_var "d_force_unsafe_rename"		"false"
	gcs_var "d_nfs_mount_path"		"host:/path"
	gcs_var "d_continuous_power"		"false"
	gcs_var "d_recycle_min_free"		"50"

	# read existing user values
	readvar "continuous_power"		"${PT_STORAGE_CF}" ""
	readvar "recycle_min_free"		"${GDI_RECORD_CF}" ""
	readvar "format_fstype"			"${PT_STORAGE_CF}" ""
	readvar "destination"			"${PT_STORAGE_CF}" ""
	readvar "mount_path"			"${PT_STORAGE_CF}" "nfs"
	readvar "mount_options"			"${PT_STORAGE_CF}" "nfs"
	for Sub in ms gcf
	do
		readvar "enabled"			"${GDI_RECORD_CF}" "${Sub}"
		readvar "seconds_per_file"		"${GDI_RECORD_CF}" "${Sub}"
		readvar "dname_fmt"			"${GDI_RECORD_CF}" "${Sub}"
		readvar "threshold_pct"			"${GDI_RECORD_CF}" "${Sub}"
	done
	readvar "dname_fmt"			"${GDI_RECORD_CF}" "dircopy"
	readvar "threshold_MiB"			"${GDI_RECORD_CF}" "dircopy"
	readvar "enable_syslog_recording"	"${GDI_RECORD_CF}" ""
	readvar "force_unsafe_rename"		"${GDI_RECORD_CF}" ""
	readvar "dump_uid"			"${GDI_RECORD_CF}" ""
	readvar "dump_gid"			"${GDI_RECORD_CF}" ""

	# destination selection
	i=0
	for Dest in `gcs_rwvar 'gcs_list_varcf2_sections' "${PT_STORAGE_CF}"`
	do
		echo "select_destination${i}=${Dest} `gcs_get_varcf2 'description' "${PT_STORAGE_CF}" "${Dest}"`"
		((++i))
	done

	# multiplexor selection
	gcs_gdi2gcf_iselect "`gcs_get_varcf2 'dbdir' "${GDI_RECORD_CF}" 'gcf'`"
	gcs_gdi2miniseed_iselect "`gcs_get_varcf2 'dbdir' "${GDI_RECORD_CF}" 'ms'`"
}



writevar() {
	local Var="$1"
	local Cffile="$2"
	local Section="$3"

	local Gcsname="new_"
	[ -n "${Section}" ] && Gcsname="${Gcsname}${Section}_"
	Gcsname="${Gcsname}${Var}"

	gcs_set_varcf2 "${Var}" "${Cffile}" "${!Gcsname}" "${Section}"
}



do_write() {
	local Sub

	do_check
	if (( gcs_errors > 0 ))
	then
		return
	fi

	# write fixed system defaults
	gcs_set_varcf2 "socket_path"		"${PT_STORAGE_CF}" "/var/run/Pt-storage" ""
	gcs_set_varcf2 "socket_group"		"${PT_STORAGE_CF}" "mstorage" ""
	gcs_set_varcf2 "socket_mode"		"${PT_STORAGE_CF}" "0660" ""
	gcs_set_varcf2 "state_dir"		"${GDI_RECORD_CF}" "/var/lib/gdi-record" ""
	gcs_set_varcf2 "dbdir"			"${GDI_RECORD_CF}" "/var/spool/to-rdisk" "dircopy"

	# write user-configured values
	writevar "continuous_power"		"${PT_STORAGE_CF}" ""
	writevar "recycle_min_free"		"${GDI_RECORD_CF}" ""
	writevar "format_fstype"		"${PT_STORAGE_CF}" ""
	writevar "destination"			"${PT_STORAGE_CF}" ""
	writevar "mount_path"			"${PT_STORAGE_CF}" "nfs"
	writevar "mount_options"		"${PT_STORAGE_CF}" "nfs"
	for Sub in ms gcf
	do
		writevar "enabled"			"${GDI_RECORD_CF}" "${Sub}"
		writevar "seconds_per_file"		"${GDI_RECORD_CF}" "${Sub}"
		writevar "dname_fmt"			"${GDI_RECORD_CF}" "${Sub}"
		writevar "threshold_pct"		"${GDI_RECORD_CF}" "${Sub}"
	done
	writevar "dname_fmt"			"${GDI_RECORD_CF}" "dircopy"
	writevar "threshold_MiB"		"${GDI_RECORD_CF}" "dircopy"
	writevar "enable_syslog_recording"	"${GDI_RECORD_CF}" ""
	writevar "force_unsafe_rename"		"${GDI_RECORD_CF}" ""
	writevar "dump_uid"			"${GDI_RECORD_CF}" ""
	writevar "dump_gid"			"${GDI_RECORD_CF}" ""

	# multiplexors
	gcs_set_varcf2 "dbdir"			"${GDI_RECORD_CF}" "${new_miniseedout_dbdir}" "ms"
	gcs_set_varcf2 "dbdir"			"${GDI_RECORD_CF}" "${new_gcfout_dbdir}" "gcf"

	# restart affected services
	gcs_svc_reload "Pt-storage"
	gcs_svc_reload "gdi-record"
}



do_check_fntpl() {
	local new_setting="$1"
	local check_type="$2"
	local var_name="$3"
	local reason

	if [ "${#new_setting}" -gt 100 ]
	then
		gcs_err "${var_name}" "Filename is too long. 100 chars max."
		return 1
	fi

	reason=`gcs_checkgdirecfn "${check_type}" "${new_setting}"`
	if [ $? -ne 0 ]
	then
		gcs_err "${var_name}" "${reason}"
		return 1
	fi

	return 0
}



do_check() {
	gcs_read_vars
	gcs_gdi2gcf_dereference
	gcs_gdi2miniseed_dereference

	# check for mke2fs if ext4 requested
	if [ "${new_format_fstype}" = "ext4" ]
	then
		if [ ! -x "/sbin/mke2fs" -a ! -x "/usr/sbin/mke2fs" ]
		then
			gcs_err format_fstype "mke2fs not available on this system; use FAT32"
		fi
	fi
	
	# special checking for filenames
	do_check_fntpl "${new_dircopy_dname_fmt}" 	"plain" 	"dircopy_dname_fmt"
	do_check_fntpl "${new_ms_dname_fmt}" 		"miniSEED" 	"ms_dname_fmt"
	do_check_fntpl "${new_gcf_dname_fmt}" 		"GCF" 		"gcf_dname_fmt"
}



# process command line
case "$1" in
--check)
	do_check
	;;
--write)
	do_write
	;;
--read)
	do_read
	;;
*)
	exit 1
	;;
esac

gcs_cleanup
exit 0
