commasep() {
	read line
	if [ -n "$line" ]
	then
		echo -n "$line"
		while [ -n "$line" ]
		do
			read line
			if [ -n "$line" ]
			then
				echo -n ",$line"
			fi
		done
	fi
	return 0
}

killOtherSMCSessionsWithSig()
{
	sigid="$1"
	mysession=`cat /proc/$BASHPID/stat | awk 'BEGIN {FS=" "}{print $6}'`
	#echo "mysession=$mysession"
	#echo "All sessions: "`ps --noheadings -U smc -o sess | sort -u | commasep`
	othersessions=`ps --noheadings -U smc -o sess | egrep -v "$mysession" | sort -u | commasep`
	if [ -z "$othersessions" ]
	then
		return 0
	fi
	#echo "Other sessions: $othersessions"
	killpids=`ps --noheadings --sid "$othersessions" -o pid`
	if [ -z "$killpids" ]
	then
		return 0
	fi
	#echo "kill $sigid $killpids"
	sudo kill $sigid $killpids
	return 7
}

if [ -z "$1" ]
then
	setsid "$0" --newsid
	exit 0
elif [ "$1" = "--newsid" ]
then
	cd ~
	killOtherSMCSessionsWithSig

	if [ "$?" -eq 7 ]
	then
		sleep 10
		killOtherSMCSessionsWithSig -9
		
		if [ "$?" -eq 7 ]
		then
			sleep 10
		fi
	fi

	./startup
else
	echo "Unexpected parameter $1"
	exit 1
fi


