#!/bin/bash
## Simple function for exiting a bash script with a given return value.
## Uses bash snippet logger.

## Cleanup and exit with exit value given.
## param 1 Exit value.
function clean_up() {
	local _exit_val=$1;
	echo "Cleaning up on exit $_exit_val.";
	
	# add cleanup code here	

	echo "Exiting on $_exit_val.";
	exit $_exit_val;
}

## Test the clean_up() function.
Param=$1;
Test="test"
if [[ ${Param} = $Test ]]; then
	printf "Running tests for clean_up() with Param ${Param}.\n";
	## testing exit code 1
	exit_code1=1;
	printf "Test1 with exit code $exit_code1.\n";
	./clean_up.sh $exit_code1;
	ret1=$?;
	if [[ ${ret1} -eq ${exit_code1} ]]; then
		printf "Test1 with ${exit_code1} ok.\n"
	else
		printf "Test1 with ${exit_code1} FAILED: got ${ret1}.\n"		
	fi
	## testing exit code 0
	exit_code2=0;
	printf "Test2 with exit code $exit_code2.\n";
	./clean_up.sh $exit_code2;
	ret2=$?;
	if [[ ${ret2} -eq ${exit_code2} ]]; then
		printf "Test2 with ${exit_code2} ok.\n"
	else
		printf "Test2 with ${exit_code2} FAILED: got ${ret2}.\n"		
	fi
	exit 0;
else
	printf "Cleanup test with ${Param}.\n";
	clean_up ${Param};
	exit 0;
fi
