#!/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 <<'LM_bash_profile' > $temp_file
# LabMaker:START
/usr/local/bin/l3-agent
# LabMaker:END
LM_bash_profile
	cat $profile \
		| sed '/LabMaker:START/,/LabMaker:END/ d' \
		>> $temp_file
	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
LMHOME=~/.lilalo
mkdir -p ${LMHOME}

uname -a | grep -qi bsd && bsd=yes
flush="-f"			#linux
[ -n "$bsd" ] && flush="-t 0"	#freebsd

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


export PS1='\[`	
	a="$?";
	HIDDEN=$([ "$a" = 0 ] || echo -n ^"$a")$(echo -n _${UID}_)$(echo -n _$$_)$(date\
		+"%j$(cat ${LMHOME}/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	
	session_id=${tty##*/}-$$
	parent=`cat /proc/$PPID/cmdline 2> /dev/null`
	system=`uname -rs`
	login_from=`who | grep "${tty##/dev/}" | awk '{print $6;}' | tr -d '()'`
	#[ -n "$bsd" ] && login_from="" #FIXME!
	start_time=`date +%s`
	hostname=`hostname -f 2> /dev/null`
	[ -n "$bsd" ] && hostname=`hostname`

	cat <<INFO > $LMHOME/${session_id}.info
<session>
<local_session_id>$session_id</local_session_id>
<hostname>$hostname</hostname>
<user>$USER</user>
<uid>$UID</uid>
<login_from>$login_from</login_from>
<tty>$tty</tty>
<system>$system</system>
<parent>$parent</parent>
<ppid>$PPID</ppid>
<pid>$$</pid>
<start_time>$start_time</start_time>
</session>
INFO

	exec script $flush -q $LMHOME/${session_id}.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=~/.lilalo
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/.lilalo
		echo $first_lab > $home/.lilalo/lab
		chown -R $user $home/.lilalo

		#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/.bash_profile ] && install_to_profile $home/.bash_profile \
			&& install_to_profile $home/.bash_profile \
			&& echo LabMaker is installed to $home/.bash_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/.lilalo
		echo $first_lab > $home/.lilalo/lab
		chown -R ${user##*/} $home/.lilalo

		#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