#!/bin/bash 
#===============================================================================
#
#          FILE:  error_handling.sh
# 
#         USAGE:  . error_handling.sh
# 
#   DESCRIPTION:  function to ease catching return codes of processes/commands
# 
#        AUTHOR:  Ryan Schulze (rs), ryan@dopefish.de
#       CREATED:  05/13/2013 02:53:01 PM CDT
#===============================================================================

# use the text() function for making pretty output
. colortext.sh

#command_status is global
command_status=
function try() { #{{{
	if [ "$#" = "0" ]
	then # not enough parameters were provided
		echo "ERROR: sytnax is 'try <silent|warn|fatal> command'"
		exit 1
	fi
	local returncode=0
	local severity=${1}
	shift
	local cmd="${@}"

	$cmd
	returncode=$?

	if [[ ${returncode} -gt 0 ]]
	then
		case ${severity} in
			silent  ) 
				command_status=${returncode}
				;;
			warn  ) 
				printf "%s: '%s' failed with return code -%s-\n" $(text yellow "Warning") "$(text blue "${cmd}")" $(text yellow "${returncode}") 
				command_status=${returncode}
				;;
			fatal ) 
				printf "%s: '%s' failed with return code -%s-\n" $(text red "Error") "$(text blue "${cmd}")" $(text yellow "${returncode}")
				exit ${returncode} 
				;;
			* )
				printf "%s: Wrong usage of the try() function\n" $(text red "Error")
				exit 1
				;;
		esac
	fi
} #}}}

# example usages:
#
#	try silent ls -al doesnotexist
#	echo ${command_status}
#	try warn   ls -al doesnotexist
#	try warn   false
#	try fatal  ls -al doesnotexist

#	try fatal cmake -DCMAKE_INSTALL_PREFIX=${installprefix%/} .
#	try fatal make
#	try warn  make man
#	try warn  make doc
#	try fatal make install

