#!/bin/sh
# Pt-baselayout/src/baselayout-files/all/usr/lib/upgrade/4018-Pt-storage-destination-checker.sh
# 
#  Copyright: ©2011, Güralp Systems Ltd.
#  Author: Laurence Withers <lwithers@guralp.com>
#  License: GPLv3
#
# Fish reported at least one system where the destination set in the Pt-storage
# configuration file did not match a valid section. As it is unknown how this
# could have occurred, and to protect users against such invalid configurations
# arising in any manner, this script will check for such situations and attempt
# to resolve them.
#

CF="/etc/Pt-storage/Pt-storage.local"

[ -r "${CF}" ] || exit 0

SECTIONS="`gcs_rwvar gcs_list_varcf2_sections "${CF}"`"
[ -n "${SECTIONS}" ] || exit 0

DESTINATION="`gcs_rwvar gcs_get_varcf2 destination "${CF}" ""`"
VALID=0
FIRST_SECTION=""

for section in ${SECTIONS}
do
    # get first mentioned section; we prefer not to use NFS
    [ -z "${FIRST_SECTION}" -o "${FIRST_SECTION}" = "nfs" ] && FIRST_SECTION="${section}"

    if [ "${DESTINATION}" = "${section}" ]
    then
        VALID=1
        break
    fi
done

if [ ${VALID} -eq 0 ]
then
    echo " * Warning: changing Pt-storage recording destination" >&2
    echo " * Warning: (from ${DESTINATION} to ${FIRST_SECTION})." >&2
    echo " * Warning: Please check the new configuration." >&2
    gcs_rwvar gcs_set_varcf2 "destination" "${CF}" "${FIRST_SECTION}" ""
fi

# vim: expandtab:sw=4:ts=4
