#!/usr/bin/env sh

set -u  # force better programming and ability to use check for not set
if [ "${1:-}" = "-x" ] ; then set -xv; shift; fi

# parse parameters, we expect the following -
#	prelude_in_c  command

if [ $# -ne 1 ]
then
	echo "USAGE: run_misc_command  \"command to execute\""
	rtn=1
else
	command="${1}"
	# create a string without whitespace or unusual characters to use as a
	# filename
	cmtext=`echo $command|sed -e 's/[^A-Za-z0-9]//g'`
	# setup some variables
	theDirectory=$HOME/MISC
	# make this directory if it does not exist
	if [ ! -d "$theDirectory" ]
	then
	    mkdir $theDirectory
	fi
	activeFlag=$theDirectory/$cmtext
	# make sure we are not already running
	mkdir $activeFlag 1>/dev/null 2>/dev/null
	if [ $? -ne 0 ] 
	then 
		rtn=2;
	else
		eval $command;
		rtn=0
		rmdir $activeFlag 1>/dev/null 2>/dev/null
	fi
fi

exit $rtn



