#!/bin/bash
########################################################################
####  Script Name: sm-lib-warning
####  version: 2.0.2
####  Date: April 15 2009

####  Copyright (C) Harald Hope 2005-2009
####  This program is free software; you can redistribute it and/or modify it under
####  the terms of the GNU General Public License as published by the Free Software
####  Foundation; either version 2 of the License, or (at your option) any later version.

####  This program is distributed in the hope that it will be useful, but WITHOUT
####  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
####  FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

####  Get the full text of the GPL here:
####  http://www.gnu.org/licenses/old-licenses/gpl-2.0.html

####  Script Author: Harald Hope
####  This is a library file for smxi and cannot be run independently

####  Script URL: http://smxi.org/sm/sm-lib-warning
####  Script SVN: http://code.google.com/p/smxi
####  Script Home page: http://techpatterns.com/forums/about736.html
########################################################################

# get remote server status
# Note: du needs to exit if this file isn't found, otherwise errors can occur in du
# args: $1 - print (optional)
get_status()
{
	eval $LOGUS
	local ua="-U s-tools/warning-alerts.$WARNING_STATUS"
	if [ "$1" != 'print' ];then
		wget $ua -T$TIME_OUT -t2 -q -O $SCRIPT_HOME/$WARNING_STATUS $SCRIPT_DOWNLOAD$UPGRADE_DATA$WARNING_STATUS || error_handler 'warning-status-file-missing' $FUNCNAME $WARNING_STATUS
	fi
	if [ -f $WARNING_STATUS ];then
		STATUS=$( cat $WARNING_STATUS)
	else
		error_handler 'warning-status-file-missing' $FUNCNAME $WARNING_STATUS
	fi
	eval $LOGUE
}

# get warning text if required
# args: $1 - alert/danger; $2 - print (optional)
get_warning()
{
	eval $LOGUS
	local ua="-U s-tools/warning-alerts." uaFull=''
	if [ "$STATUS" == 'danger' -o "$STATUS" == 'danger-alert' -a "$1" == 'danger' ];then
		if [ "$2" != 'print' ];then
			uaFull="$ua$WARNING_BODY"
			wget $uaFull -T$TIME_OUT -t2 -q -O $SCRIPT_HOME/$WARNING_BODY $SCRIPT_DOWNLOAD$UPGRADE_DATA$WARNING_BODY || error_handler 'warning-data-wget-error' $FUNCNAME $WARNING_BODY
		fi
		if [ -f $WARNING_BODY ];then
			cat $WARNING_BODY
		else
			error_handler 'warning-data-file-missing' $FUNCNAME $WARNING_BODY
		fi
	fi
	if [ "$STATUS" == 'alert' -o "$STATUS" == 'danger-alert' -a "$1" == 'alert' ];then
		if [ "$2" != 'print' ];then
			uaFull="$ua$ALERT_BODY"
			wget $uaFull -T$TIME_OUT -t2 -q -O $SCRIPT_HOME/$ALERT_BODY $SCRIPT_DOWNLOAD$UPGRADE_DATA$ALERT_BODY || error_handler 'warning-data-wget-error' $FUNCNAME $ALERT_BODY
		fi
		if [ -f $ALERT_BODY ];then
			cat $ALERT_BODY
		else
			error_handler 'warning-data-file-missing' $FUNCNAME $ALERT_BODY
		fi
	fi
	eval $LOGUE
}

# this isn't properly part of the warning system, but it's still reasonably
# related. This is called in pre du-1, so users can actually remember the config files
# args: $1 - print (optional)
get_configs()
{
	eval $LOGUS
	local ua="-U s-tools/warning-alerts.$CONFIGS_BODY"
	if [ "$1" != 'print' ];then
		wget $ua -T$TIME_OUT -t2 -q -O $SCRIPT_HOME/$CONFIGS_BODY $SCRIPT_DOWNLOAD$UPGRADE_DATA$CONFIGS_BODY || error_handler 'warning-data-wget-error' $FUNCNAME $CONFIGS_BODY
	fi
	if [ -f "$CONFIGS_BODY" -a -n "$( cat $CONFIGS_BODY )" ];then
		echo "${M}Answer ${C}y${M} instead of the standard ${C}n${M} if you are asked to"
		echo "replace the following config files during $DU_UPGRADE:${N}"
		cat $CONFIGS_BODY
		echo
		echo $MLINE
	else
		error_handler 'warning-data-file-missing' $FUNCNAME $CONFIGS_BODY
	fi
	eval $LOGUE
}

# args: $1 - print (optional)
print_alert_warning_data()
{
	eval $LOGUS
	get_status $1

	if [ "$STATUS" == 'alert'  -o "$STATUS" == 'danger-alert' ];then
		echo $EBAR
		echo ${E}"*******     CURRENT ALERTS & FIXES"
		echo $EBAR
		get_warning 'alert' $1
		echo
	fi

	if [ "$STATUS" == 'danger' -o "$STATUS" == 'danger-alert' ];then
		echo $WBAR
		echo "${W}*******     IT IS NOT CURRENTLY SAFE TO $( tr [a-z] [A-Z] <<< $DU_UPGRADE )"
		echo ${W}"*******     CURRENT WARNINGS(s)"
		echo $WBAR
		get_warning 'danger' $1
		echo
		echo $LINE
		echo "${S}Please wait to do the $DU_UPGRADE until you have received word "
		echo "that it is safe, or until the script runs without displaying this warning."
		if [ "$SYSTEM_BASE" == 'sid' ];then
			echo
			echo "Check the forums and news page of sidux.com for further information."
		fi
	fi

	if [ "$STATUS" == 'proceed' ];then
		echo $MBAR
		echo "${M}There are currently no known major issues with ${C}$DU_UPGRADE${S}"
	fi
	eval $LOGUE
}

# issue warning if status = danger etc
upgrade_warning()
{
	eval $LOGMS
	local ContinueText='' options='' opt=''

	print_alert_warning_data
	if [ "$STATUS" == 'alert'  -o "$STATUS" == 'danger-alert' ];then
		ContinueText="${E}Continue with ${C}$DU_UPGRADE${E}. Proceed with caution. Watch for fixes noted above."
	fi

	if [ "$STATUS" == 'danger' -o "$STATUS" == 'danger-alert' ];then
		ContinueText="${W}Continue with ${C}$DU_UPGRADE${W}. Proceed at your own risk!!!"
	fi

	if [ "$STATUS" == 'proceed' ];then
		ContinueText="${S}Continue with ${C}$DU_UPGRADE${S}."
	fi

	echo $LINE
	echo "${C}1${S} - $ContinueText"
	echo "${C}2${S} - Continue without doing the ${C}$DU_UPGRADE${S}."
	echo "    This will let you do graphics install and various other smaller functions."
	echo
	echo "${C}3${S} - Quit the script now."
	echo $LINE
	echo $SLE
	echo $LINE

	options="continue continue-no-$DU_UPGRADE quit"
	select opt in $options
	do
		eval $LOGME
		case $opt in
			continue|c)
				# start xorg install, update, dist-upgrade, xorg component install
				install_dist_upgrade 'first'
				;;
			continue-no-$DU_UPGRADE)
				echo "${S}Skipping ${C}$DU_UPGRADE${S} for now. Continuing with script.${N}"
				;;
			quit|q)
				print_quit
				;;
			*)
				print_error opt
				upgrade_warning repeat
				;;
		esac
		break
	done
}
#upgrade_warning

###**EOF**###


syntax highlighted by Code2HTML, v. 0.9.1