#!/bin/bash

INCLUDE_PATH="/usr/include/llvm-3.4/:/usr/include/llvm-c-3.4/"
LIB_PATH="/usr/lib/llvm-3.4/lib/"

if [ -f ~/.bash_profile ]; then
	# If the person has already a .bash_profile remove existing llvm entries

	# Save the original .bash_profile if has not already been done
	if [ ! -f ~/.bash_profile.llvm_save ]; then
		cp ~/.bash_profile ~/.bash_profile.llvm_save
	fi

	# Effectively remove the previous entries that exist
	grep -v "llvm-3.4" ~/.bash_profile > ~/.bash_profile.tmp
	grep -v "export C_INCLUDE_PATH" ~/.bash_profile.tmp > ~/.bash_profile.tmp2
	grep -v "export CPLUS_INCLUDE_PATH" ~/.bash_profile.tmp2 > ~/.bash_profile.tmp
	grep -v "export LIBRARY_PATH" ~/.bash_profile.tmp > ~/.bash_profile.tmp2
	grep -v "export LD_LIBRARY_PATH" ~/.bash_profile.tmp2 > ~/.bash_profile.tmp
	grep -v "LLVM labwork" ~/.bash_profile.tmp > ~/.bash_profile

	# Remove the temporary files that have been created
	rm ~/.bash_profile.tmp ~/.bash_profile.tmp2

	# Append the start of the new section to be added
	echo "# .bash_profile section defined for LLVM labwork" >> ~/.bash_profile
else
	# If no .bash_profile exists create one and start the new section
	echo "# .bash_profile section defined for LLVM labwork" > ~/.bash_profile
fi

# Add the necessary path modifications at the end of the new .bash_profile
echo "C_INCLUDE_PATH=$INCLUDE_PATH:\$C_INCLUDE_PATH" >> ~/.bash_profile
echo "CPLUS_INCLUDE_PATH=$INCLUDE_PATH:\$CPLUS_INCLUDE_PATH" >> ~/.bash_profile
echo "export C_INCLUDE_PATH" >> ~/.bash_profile
echo "export CPLUS_INCLUDE_PATH" >> ~/.bash_profile
echo "LIBRARY_PATH=$LIB_PATH:\$LIBRARY_PATH" >> ~/.bash_profile
echo "LD_LIBRARY_PATH=$LIB_PATH:\$LD_LIBRARY_PATH" >> ~/.bash_profile
echo "export LIBRARY_PATH" >> ~/.bash_profile
echo "export LD_LIBRARY_PATH" >> ~/.bash_profile
printf "# end-of LLVM labwork section" >> ~/.bash_profile
