#!/bin/sh
# $Copyright$
# Copyright (C) Fujitsu Technology Solutions 2009
# All rights reserved
#
# $Copyright$
# Copyright (C) Fujitsu Siemens Computers GmbH 2008
# All rights reserved
#
# $Id: SVclearLog.sh,v 1.3 2008-04-14 15:32:46+02 richter Exp richter $
#
# NAME
#	SVclearLog - Clear the ServerView Message Log
#
# SYNOPSIS
#	SVclearLog [-h] [file]
#
# DESCRIPTION
# 	This script saves (with SVreadLog(8)) and clears the ServerView Message Log
#	maintained by ServerView. On IPMI-based systems, the ServerView Message Log
#	includes important entries from the IPMI SEL as kept in the BMC.
#
# OPTIONS
#	If called without options, SVclearLog saves the Message Log in the supplied
#   language, to the given file suffixed with the usual (for example {de|en})
#   language abbreviation, per default /var/log/srvmagt/SVreadLog-YYYYMMTT-LANG.txt.
#	Otherwise, option
#	h	displays help information
#
# SEE ALSO
#	eecdcp(8), SVreadLog(8)
#	ServerControl Command Interface (SCCI) Specification
#
# FILES
#	/var/log/srvmagt/SVreadLog-YYYY-MM-TT-LANG
#		default file to save log to
#
# EXIT STATUS
#	0 OK
#	1 Runtime Error
#	2 Argument Error
#
# CHANGE HISTORY
#	April 2008, -rit
#		Initial implementation.

# ----------------------------------------------------------------------
# Initialization
# ----------------------------------------------------------------------

MYSELF="${0##*/}"
EECDCP=./eecdcp # myEecdcp
DEFAULTFILE="/var/log/srvmagt/SVreadLog-$(date +'%F')"
CM_ClearLog="0b03"

status=0
file=

# Internationalized Messages

USAGE="#### Usage:
    $0 [-h] [file]
    Call without options saves log messages in supported languages
    to file, then clears the log.
    Options:
    h   display this message\n"

            CannotSaveLog="Cannot save the ServerView Log. Clearing aborted.\n"
          LogSavingFailed="Saving the log to %s failed. Clearing aborted.\n"
CannotClearNonCriticalLog="Cannot clear the non-critical messages log. Clearing aborted\n"
   CannotClearCriticalLog="Cannot clear the critical error log. Clearing aborted.\n"
                 SavedLog="Saved ServerView Message Log to '%s'.\n"

# ----------------------------------------------------------------------
# Main
# ----------------------------------------------------------------------

while getopts h optChar; do
	case "$optChar" in
	h|[?])
		printf "$USAGE"
		[ "$1" = "-h" ] && exit 0
		exit 2
		;;
	esac
done


shift $(($OPTIND - 1))
file=$1
[ -z "$1" ] && file=$DEFAULTFILE

# Need these tools to perform the save and clear log

type SVreadLog > /dev/null && type eecdcp > /dev/null || {
	printf "$CannotSaveLog"
	exit 1
}

# En
fileEn=${file}"-en"
SVreadLog -e > $fileEn || {
	printf "$LogSavingFailed" $fileEn
	exit 1
}

fileDe=${file}"-de"
SVreadLog -d > $fileDe || {
	printf "$LogSavingFailed" $fileDe
	exit 1
}
printf "$SavedLog" "${file}-{de|en}"

eecdcp -c oc=$CM_ClearLog oi=0001 || {
	printf "$CannotClearNonCriticalLog"
	exit 1
}

eecdcp -c oc=$CM_ClearLog oi=0000 || {
	printf "$CannotClearCriticalLog"
	exit 1
}

# ----------------------------------------------------------------------
# Done
# ----------------------------------------------------------------------

exit 0

# EOF
