#!/bin/sh
##############################################################################
#
#  Copyright (c) 2011, Doremi Labs, Inc.
#    All rights reserved.
#
#  Redistribution in source or binary forms, with or without
#  modification, are NOT permitted.
#
#    Doremi Labs, Inc.
#    1020 Chestnut St.
#    Burbank, CA 91506
#    Tel : (818) 562 1101
#    Fax : (818) 562 1109
#
#  HMO: add variable number of drives support (from 1 to 6)
#  HMO: fix incorrect OID 10 (driveSmartTestPassed)
# 
##############################################################################

# for definition of motherboard_model()
. /etc/init.d/drmfunctions
. /doremi/etc/init.d/snmp.sh

DOREMI_PATH=/var/doremi
LDBCTL=/doremi/sbin/ldbctl.out

detect_playback_status()
{
	if [ -r /dev/shm/odeticsd.out ]; then
		status=`od -x -N1 -An /dev/shm/odeticsd.out | cut -c5`
		if [ "$status" = "0" ]; then
			echo eject
		elif [ "$status" = "1" ]; then
			echo play
		elif [ "$status" = "2" ]; then
			echo pause
		else
			echo unknown
		fi
	else
		echo eject
	fi
}

sense_dst_and_timezone()
{
	TMP_ZDUMP="/tmp/zdump.tmp.$$"
	
	CURRENT_TIMEZONE=$(cat /etc/timezone)
	CURRENT_GMT_OFFSET_SECONDS="0"
	NEXT_DST_CHANGE="None"
	
	zdump -v /etc/localtime > $TMP_ZDUMP
	
	CURRENT_UTC_DATE_SECONDS=$(date -u +%s)
	DST_UTC_DATE_SECONDS="0"
	
	while read dst; do
		# Get the UTC date of the DST, in seconds
		DST_UTC_DATE=$(echo "$dst" | awk -F "/etc/localtime " '{printf $2}' | awk -F " = " '{printf $1}')
		DST_UTC_DATE_SECONDS=$(date -d "$DST_UTC_DATE" +%s 2> /dev/null)
		
		# Search for the next DST available
		if [ $(echo "$DST_UTC_DATE_SECONDS >= $CURRENT_UTC_DATE_SECONDS" | bc) -eq 1 ]; then
			if ! echo "$dst" | grep -q "2038"; then
				# Get the next DST change
				NEXT_DST_CHANGE=$(echo "$dst" | awk -F " = " '{printf $2}' | awk -F " isdst=" '{printf $1}')
			else
				# We can't find the next DST
				NEXT_DST_CHANGE="None"
			fi
			
			break # Exit while
		else
			#Get the current GMT offset (the last execution is the good value)
			CURRENT_GMT_OFFSET_SECONDS=$(echo "$dst" | awk -F "gmtoff=" '{printf $2}')
		fi
	
	done < $TMP_ZDUMP
	
	set_string_value "$SNMP_PATH/$DOREMI_OID.1.3.14.1.0" "$CURRENT_TIMEZONE"
	set_string_value "$SNMP_PATH/$DOREMI_OID.1.3.14.2.0" "$NEXT_DST_CHANGE"
	set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.14.3.0" "$((CURRENT_GMT_OFFSET_SECONDS/60))"
	
	# check if we are near to changing DST
	local next_dst=$(date -d "$NEXT_DST_CHANGE" +%s 2>/dev/null)
	local cur_time=$(date +%s)
	if [ -n "$next_dst" ]; then
		if [ $(( $cur_time + (3600*48) )) -gt $next_dst ]; then
			if [ ! -e $ALARM_PATH/DST_warning ]; then
				log_alarm "DST_warning alarm raised"
			fi
			touch $ALARM_PATH/DST_warning
		else
			if [ -e $ALARM_PATH/DST_warning ]; then
				log_alarm "DST_alarm alarm cleared"
				rm -f $ALARM_PATH/DST_warning;
			fi
		fi
	else
		if [ -e $ALARM_PATH/DST_warning ]; then
			log_alarm "DST_alarm alarm cleared"
			rm -f $ALARM_PATH/DST_warning;
		fi
	fi
	
	rm -f $TMP_ZDUMP
}

run_harddiskstatus()
{
	if ! test -x /doremi/sbin/harddiskstatus.sh; then
		return 0
	fi
	st1=`detect_playback_status`
	if [ "$st1" = "play" ]; then
		azzz=0
	elif [ "$st1" = "pause" ]; then
		# retry in few seconds
		sleep 5
		st2=`detect_playback_status`
		if [ "$st2" = "play" ]; then
			azzzz=0
		elif [ "$st2" = "pause" ]; then
			/doremi/sbin/harddiskstatus.sh sense-smart
		else
			/doremi/sbin/harddiskstatus.sh sense-smart
		fi
	else
		/doremi/sbin/harddiskstatus.sh sense-smart
	fi
}


check_splvalidator()
{
	local SPLVALIDATOR_CFG="/doremi/etc/default/drmdevmgr.config"
	local str
	local value

	if [ ! -e ${SPLVALIDATOR_CFG} ]; then
		return 0
	fi

	str="$(awk '/<Name>ui.add.splvalidator.visible<\/Name>/,/<\/Parameter>/' /doremi/etc/default/drmdevmgr.config | awk '/<Value>/,/<\/Value>/' | sed "s@.*>\(.*\)<.*@\1@" | awk '{ print tolower($0) }')"

	if [ "${str}" == "true" ]; then
		active_feature "splvalidator"
	fi
}

#If an argument is provided, execute ONLY the required function, then exit. 
#Else if no/bad argument is provided, execute the rest of the script (so, all the functions).
EXITREQUIRED=0
while test $# -gt 0; do
	case "$1" in
		--timezone)
			shift; sense_dst_and_timezone
			EXITREQUIRED=1;;
		*)
			break;;
	esac
done
if [ "${EXITREQUIRED}" -eq 1 ]; then exit 0; fi


run_harddiskstatus

sense_dst_and_timezone

check_features

check_splvalidator
