#!/bin/sh

# Use -d to deinstall labmaker
# You can specify directory list to install LabMaker as command line parameters
# or set it in $users_to_install variable

# CONFIGURABLE SECTION start
#users_to_install="/home/your-user-here /root"
# CONFIGURABLE SECTION stop

first_lab=T1
editors_to_install='/bin/vi /usr/bin/vi /usr/bin/vim /bin/ee /usr/bin/ee /usr/bin/pico /usr/bin/nano /usr/local/bin/vim'
temp_file=/tmp/lm-install-$$
arg=$@

show_usage()
{
	cat << USAGE 

$0 [-d] path...

	* Use -d to deinstall labmaker
	* You can specify directory list to install LabMaker as command line parameters
	  or set it in \$users_to_install variable in the script

Example:

	Command
	# $0 /root /home/user
	installs labmaker to /root and /home/user directories 

USAGE
}

install_to_profile()
{
	profile=$1
	cat $profile \
		| sed '/LabMaker:START/,/LabMaker:END/ d' \
		> $temp_file
	cat <<'LM_bash_profile' >> $temp_file
	# LabMaker:START
	#LMHOME=~/.labmaker
	#mkdir -p ${LMHOME}
	#TTY=`tty`
	#flush="-f"
	#exec script $flush -q $LMHOME/${TTY##*/}-$$.script
	# LabMaker:END
LM_bash_profile
	cat $temp_file > $profile
	rm $temp_file
}

uninstall_from_profile()
{
	profile=$1
	cat $profile \
		| sed '/LabMaker:START/,/LabMaker:END/ d' \
		> $temp_file
	cat $temp_file > $profile
	rm $temp_file
}

install_to_bashrc()
{
	profile=$1
	cat $profile \
		| sed '/LabMaker:START/,/LabMaker:END/ d' \
		> $temp_file
	cat <<'LM_bash_profile' >> $temp_file
	# LabMaker:START
	TTY=`tty` 

	uname -a | grep -qi bsd && bsd=yes

	this_term=`w | grep "${TTY##/dev/}" | awk '{print $8;}'`
	# freeBSD: 
	[ -n "$bsd" ] && this_term=`w | grep "${TTY##/dev/tty}" | awk '{print $6;}'`



	LMHOME=~/.labmaker
	mkdir -p ${LMHOME}
	flush="-f"	#linux
	[ -n "$bsd" ] && flush="-t 0"	#freebsd
	export PS1='\[`	a="$?";
		HIDDEN=$([ "$a" = 0 ] || echo -n ^"$a")$(echo -n _${UID}_)$(echo -n _$$_)$(date\
			+"%j$(cat ~/.labmaker/lab 2>/dev/null) %H:%M:%S");
		echo $HIDDEN`\033[50D\033[K\][\u@\h:\W]\$ '

	if [ -n "$this_term" ] && echo $this_term | grep -qv script
	then	
		exec script $flush -q $LMHOME/${TTY##*/}-$$.script
	fi
	# LabMaker:END 
LM_bash_profile
	cat $temp_file > $profile
	rm $temp_file
}

uninstall_from_bashrc()
{
	profile=$1
	cat $profile \
		| sed '/LabMaker:START/,/LabMaker:END/ d' \
		> $temp_file
	cat $temp_file > $profile
	rm $temp_file
}

install_editor()
{
editor=$1
[ -e $editor.orig ] && cp $editor.orig $editor
cp $editor $editor.orig
cat <<'editor_wrapper' | sed "s@EDITOR@$editor@" > $editor
#!/bin/sh

LMHOME=~/.labmaker
if [ "${1#-}" = "$1" -a -d "$LMHOME" ]
then
	LAB=`cat $LMHOME/lab`
	TIME="`date +%j${LAB}_%H:%M:%S`"
	DIR=""
	[ "${1#/}" = "$1" ] && DIR=$PWD/
	DIFFNAME=$PPID_${TIME}_`echo $DIR$1| sed s@_@__@ | sed 's@/@_@g'`.diff
	tmp="/tmp/lm-saved-$$"
	touch $1
	cp -- "$1" $tmp 2> /dev/null
	EDITOR.orig "$@" || ERR=1
	diff $tmp $1 > $LMHOME/$DIFFNAME 2> /dev/null
	rm $tmp 2> /dev/null
	if [ "$ERR" = 1 ]
	then
		false
	else
		true
	fi	
else
	exec EDITOR.orig "$@"
fi
editor_wrapper

}


uninstall_editor()
{
	editor=$1
	[ -e $editor.orig ] && mv $editor.orig $editor
}

if [ "$1" != "-d" ] 
then 
	# INSTALLING LM
	if [ $# -gt 0 ] 
	then
		users_to_install="$*"
	fi

	if [ -z "$users_to_install" ]
	then 
		show_usage
		exit
	fi	

	for home in $users_to_install
	do 
		# fix this!
		user=${home%/} 
		user=${user##*/}
		mkdir -p $home/.labmaker
		echo $first_lab > $home/.labmaker/lab
		chown -R $user $home/.labmaker

		#if [ ! -e $home/.bash_profile ] 
		#then
		#	echo '. ~/.bashrc' >> ~/.bash_profile
		#fi
		#[ -e $home/.bash_profile ] \
		#	&& install_to_profile $home/.bash_profile \
		#	&& echo LabMaker is installed to $home/.bash_profile

		echo Don\'t forget to check .bash_profile for .bashrc call

		[ -e $home/.profile ] && install_to_profile $home/.profile \
			&& install_to_profile $home/.profile \
			&& echo LabMaker is installed to $home/.profile

		touch $home/.bashrc
		[ -e $home/.bashrc ] && install_to_bashrc $home/.bashrc \
			&& install_to_bashrc $home/.bashrc \
			&& echo LabMaker is installed to $home/.bashrc
	done

	for editor in $editors_to_install
	do
		[ -e $editor ] \
			&& install_editor $editor \
			&& echo LabMaker is installed to $editor
	done
else
# UNINSTALLING LM
	shift
	users_to_install="$*"
	for user in $users_to_install
	do 
		home=$user		
		mkdir -p $home/.labmaker
		echo $first_lab > $home/.labmaker/lab
		chown -R ${user##*/} $home/.labmaker

		#if [ ! -e $home/.bash_profile ] 
		#then
		#	echo '. ~/.bashrc' >> ~/.bash_profile
		#fi
		#[ -e $home/.bash_profile ] \
		#	&& uninstall_from_profile $home/.bash_profile \
		#	&& echo LabMaker is uninstalled from $home/.bash_profile

		[ -e $home/.profile ] && uninstall_from_profile $home/.profile \
			&& uninstall_from_profile $home/.profile \
			&& echo LabMaker is uninstalled from $home/.profile

		touch $home/.bashrc
		[ -e $home/.bashrc ] && uninstall_from_bashrc $home/.bashrc \
			&& uninstall_from_bashrc $home/.bashrc \
			&& echo LabMaker is uninstalled from $home/.bashrc
	done

	for editor in $editors_to_install
	do
		[ -e $editor ] \
			&& uninstall_editor $editor \
			&& echo LabMaker is uninstalled from $editor
	done
fi