File nagvis-update-script.sh of Package nagvis

#!/bin/bash
#
# Do some update tasks (Changing options, notify about deprecated options)
#

# Default installation path (by RPM)
NAGVIS_PATH='/etc/nagvis'
# Path to the NagVis configuration file
NAGVIS_CONF="$NAGVIS_PATH/nagvis.ini.php"
# Log what is going on
LOGFILE="$NAGVIS_PATH/nagvis-update.log"
# Loglevel: log everything
LOGLEVEL=7
# debug messages
DEBUG="no"
# Saving current timestamp for backup when updating
DATE=$(date +%Y-%m-%d_%H:%M:%S)
# Main installation path
NAGVIS_INSTALL_PATH='/usr/share/nagvis'
# get the verison information from RPM
INSTALLER_VERSION=$(rpm -q --qf "%{VERSION}-%{RELEASE}" nagvis | head -n1)
USER='wwwrun'
GROUP='www'


# Print usage
usage() {
	cat <<EOD
	NagVis Updater $INSTALLER_VERSION
	Updates NagVis on your system.

	Usage: $0 [OPTIONS]

	General Parameters:

	  -f <PATH>     Path to the NagVis configuration file
					  Default value: $NAGVIS_CONF
	  -p <PATH>     Path to NagVis base directory 
					  Default value: $NAGVIS_PATH
	  -l <PATH>    Path to logfile 
					  Default value: $LOGFILE

	  -u <user>     User name for the ownership of the files
					  Default value: $USER
	  -g <group>    Group name for the ownership of the files
					  Default value: $GROUP

	  -v            Version information
	  -h            This message

EOD
}

# Print version information
version() {
	cat <<EOD
	NagVis updater, version $INSTALLER_VERSION
	based on the NagVis installer - see information below:

	Copyright (C) 2004-2011 NagVis Project (Contact: info@nagvis.org)

	License: GNU General Public License version 2

	Based on the work of:
	- Wolfgang Nieder
	- Lars Michelsen <lars@vertical-visions.de>

EOD
}

remove_option_from_main_config(){
	local REMOVE_OPT="$1"
    LOG "Removing $REMOVE_OPT from $NAGVIS_CONF" 3
	sed -i '/^${REMOVE_OPT}=/d' "$NAGVIS_CONF"
}

remove_option_from_maps(){
	local REMOVE_OPT="$1"
	FILES=$(grep -lr '^${REMOVE_OPT}=' "${NAGVIS_PATH}/maps/"*.cfg)
	for FILE in $FILES; do
		backup_file=$(basename $FILE)
        LOG "Removing $REMOVE_OPT from $FILE" 3
		test -f "${NAGVIS_PATH}/auth-backup/$backup_file" || cp $FILE "${NAGVIS_PATH}/auth-backup/"
		sed -i '/^${REMOVE_OPT}=/d'  "$FILE" || echo "Error in $FILE" >&2
	done
}

LOG() {
    local MESSAGE="$1"
    local LEVEL=1
    local FILE="$LOGFILE"
    if [ -n "$2" ]; then
        LEVEL="$2"
    fi
    if [ -n "$3" ]; then
        FILE="$3"
    fi
    local LOGDIR="$(dirname "$FILE")"
    local LOG_DATE="$(date "+%b %d %H:%M:%S")"
    local HOST="$(hostname -s)"
    if [ -z "$FILE" ]; then
            echo "ERROR: function LOG needs a defined LOGFILE variable" >&2
            exit 1
    fi
    if [ -z "$LOGNAME" ]; then
            LOGNAME="$(basename "$0")"
    fi
    if [ ! -d "$LOGDIR" ]; then
            mkdir -p "$LOGDIR" || exit 1
            echo ""$LOG_DATE" "$HOST" "$LOGNAME": function LOG created "$LOGDIR"." > "$FILE"
    fi
    if  [ $LEVEL -le $LOGLEVEL ]; then
        echo "$LOG_DATE $HOST $LOGNAME: $MESSAGE" >> "$FILE"
    fi
    if [ "$DEBUG" = "yes" ]; then
        echo "DEBUG: "$MESSAGE""
    fi
}


# Process command line options
if [ $# -gt 0 ]; then
	while getopts 'f:p:l:vu:g:h' OPTION ; do
		case $OPTION in
			f)
				NAGVIS_CONF="$OPTARG"
			;;
			g)
				GROUP="$OPTARG"
			;;
			p)
				NAGVIS_PATH="${OPTARG%/}"
			;;
			l)
				LOGFILE="$OPTARG"
			;;
			u)
				USER="$OPTARG"
			;;
			v)
				version
				exit 0
			;;
			h)
				usage
				exit 0
			;;
			*)
				echo "Error: Unknown option in $@." >&2
				usage
				exit 1
			;;
		esac
	done
fi
shift $(( OPTIND - 1 ))

# create backup
test -d "$NAGVIS_PATH/auth-backup" || mkdir -p "$NAGVIS_PATH/auth-backup"
LOG "Creating backup of $NAGVIS_CONF in $NAGVIS_PATH/auth-backup/$BACKUP_NAGVIS_CONF" 4
BACKUP_NAGVIS_CONF=$(basename "$NAGVIS_CONF")-$DATE
cp -f "$NAGVIS_CONF" "$NAGVIS_PATH/auth-backup/$BACKUP_NAGVIS_CONF"

# no automaps any more with NagVis >= 1.7
# move the directory and the default map 
if [ -d "$NAGVIS_PATH"/automaps ]; then
    LOG "Adapting automap configuration - backup is in $NAGVIS_PATH/auth-backup/automaps" 4
	cp -r "$NAGVIS_PATH"/automaps "$NAGVIS_PATH/auth-backup/" || { echo "Could not copy automaps to backup directory" >&2; exit 1; }
	if [ -f "$NAGVIS_PATH"/automaps/__automap.cfg ]; then
		sed -e "s|^alias=Default.*Automap.*|alias=Default Automap\nsources=automap|g" "$NAGVIS_PATH"/automaps/__automap.cfg > "$NAGVIS_PATH"/maps/__automap.cfg
	fi
    LOG "Removing outdated $NAGVIS_PATH/automaps directory" 3
	rm -rf "$NAGVIS_PATH"/automaps
fi

# update main config
for option in 	\
				allowedforconfig \
				allowed_for_config \
				allowed_user \
				autoupdatefreq \
				displayheader \
				hovertimeout \
				hover_timeout \
				htmlwuijs \
				showautomaps \
				showinlists \
				wuijs \
				usegdlibs
	do
	remove_option_from_main_config "$option"
done

# update map configs
for option in \
				allowed_for_config \
				allowed_user \
				hover_timeout \
				usegdlibs
	do
		remove_option_from_maps "$option"
done

if [ ! -L "$NAGVIS_INSTALL_PATH/share/userfiles" ]; then
	if [ ! -d "$NAGVIS_PATH/userfiles" ]; then
		LOG "Moving userfiles below $NAGVIS_PATH" 3
		mv "$NAGVIS_INSTALL_PATH/share/userfiles" "$NAGVIS_PATH/"
	else
		LOG "directory $NAGVIS_PATH/userfiles exists. Not trying to move files from $NAGVIS_INSTALL_PATH/share/userfiles here" 4
	fi
	pushd "$NAGVIS_INSTALL_PATH/share/" >/dev/null
	ln -s "$NAGVIS_PATH/userfiles" .
	popd >/dev/null
fi

chown $USER:$GROUP "$NAGVIS_PATH"/maps/*.cfg 2>/dev/null
chown $USER:$GROUP "$NAGVIS_PATH"/nagvis.ini.php 2>/dev/null
chown $USER:$GROUP "$NAGVIS_PATH"/geomap/*.xml 2>/dev/null
chown $USER:$GROUP "$NAGVIS_PATH"/conf.d/*.php 2>/dev/null
chown -R root:root "$NAGVIS_PATH/userfiles" 2>/dev/null
openSUSE Build Service is sponsored by