devi@0: #!/bin/sh
devi@0: 
devi@0: # Use -d to deinstall labmaker
devi@0: # You can specify directory list to install LabMaker as command line parameters
devi@0: # or set it in $users_to_install variable
devi@0: 
devi@0: # CONFIGURABLE SECTION start
devi@0: #users_to_install="/home/your-user-here /root"
devi@0: # CONFIGURABLE SECTION stop
devi@0: 
devi@0: first_lab=T1
devi@0: 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'
devi@0: temp_file=/tmp/lm-install-$$
devi@0: arg=$@
devi@0: 
devi@0: show_usage()
devi@0: {
devi@0: 	cat << USAGE 
devi@0: 
devi@0: $0 [-d] path...
devi@0: 
devi@0: 	* Use -d to deinstall labmaker
devi@0: 	* You can specify directory list to install LabMaker as command line parameters
devi@0: 	  or set it in \$users_to_install variable in the script
devi@0: 
devi@0: Example:
devi@0: 
devi@0: 	Command
devi@0: 	# $0 /root /home/user
devi@0: 	installs labmaker to /root and /home/user directories 
devi@0: 
devi@0: USAGE
devi@0: }
devi@0: 
devi@0: install_to_profile()
devi@0: {
devi@0: 	profile=$1
devi@65: 	cat <<'LM_bash_profile' > $temp_file
devi@32: # LabMaker:START
devi@32: /usr/local/bin/l3-agent
devi@32: # LabMaker:END
devi@0: LM_bash_profile
devi@65: 	cat $profile \
devi@65: 		| sed '/LabMaker:START/,/LabMaker:END/ d' \
devi@65: 		>> $temp_file
devi@0: 	cat $temp_file > $profile
devi@0: 	rm $temp_file
devi@0: }
devi@0: 
devi@0: uninstall_from_profile()
devi@0: {
devi@0: 	profile=$1
devi@0: 	cat $profile \
devi@0: 		| sed '/LabMaker:START/,/LabMaker:END/ d' \
devi@0: 		> $temp_file
devi@0: 	cat $temp_file > $profile
devi@0: 	rm $temp_file
devi@0: }
devi@0: 
devi@0: install_to_bashrc()
devi@0: {
devi@0: 	profile=$1
devi@0: 	cat $profile \
devi@0: 		| sed '/LabMaker:START/,/LabMaker:END/ d' \
devi@0: 		> $temp_file
devi@0: 	cat <<'LM_bash_profile' >> $temp_file
devi@27: # LabMaker:START
devi@32: LMHOME=~/.lilalo
devi@27: mkdir -p ${LMHOME}
devi@0: 
devi@27: uname -a | grep -qi bsd && bsd=yes
devi@27: flush="-f"			#linux
devi@27: [ -n "$bsd" ] && flush="-t 0"	#freebsd
devi@17: 
devi@27: tty=`tty` 
devi@27: this_term=`w | grep "${tty##/dev/}" | awk '{print $8;}'`
devi@27: # freeBSD: 
devi@27: [ -n "$bsd" ] && this_term=`w | grep "${tty##/dev/tty}" | awk '{print $6;}'`
devi@17: 
devi@16: 
devi@27: export PS1='\[`	
devi@27: 	a="$?";
devi@27: 	HIDDEN=$([ "$a" = 0 ] || echo -n ^"$a")$(echo -n _${UID}_)$(echo -n _$$_)$(date\
devi@27: 		+"%j$(cat ${LMHOME}/lab 2>/dev/null) %H:%M:%S");
devi@27: 	echo $HIDDEN`\033[50D\033[K\][\u@\h:\W]\$ '
devi@16: 
devi@27: if [ -n "$this_term" ] && echo $this_term | grep -qv script
devi@27: then	
devi@27: 	session_id=${tty##*/}-$$
devi@27: 	parent=`cat /proc/$PPID/cmdline 2> /dev/null`
devi@27: 	system=`uname -rs`
devi@27: 	login_from=`who | grep "${tty##/dev/}" | awk '{print $6;}' | tr -d '()'`
devi@39: 	#[ -n "$bsd" ] && login_from="" #FIXME!
devi@27: 	start_time=`date +%s`
devi@38: 	hostname=`hostname -f 2> /dev/null`
devi@38: 	[ -n "$bsd" ] && hostname=`hostname`
devi@16: 
devi@27: 	cat <<INFO > $LMHOME/${session_id}.info
devi@27: <session>
devi@32: <local_session_id>$session_id</local_session_id>
devi@27: <hostname>$hostname</hostname>
devi@27: <user>$USER</user>
devi@65: <uid>$UID</uid>
devi@27: <login_from>$login_from</login_from>
devi@27: <tty>$tty</tty>
devi@27: <system>$system</system>
devi@27: <parent>$parent</parent>
devi@27: <ppid>$PPID</ppid>
devi@27: <pid>$$</pid>
devi@27: <start_time>$start_time</start_time>
devi@27: </session>
devi@27: INFO
devi@27: 
devi@27: 	exec script $flush -q $LMHOME/${session_id}.script
devi@27: fi
devi@27: # LabMaker:END 
devi@0: LM_bash_profile
devi@0: 	cat $temp_file > $profile
devi@0: 	rm $temp_file
devi@0: }
devi@0: 
devi@0: uninstall_from_bashrc()
devi@0: {
devi@0: 	profile=$1
devi@0: 	cat $profile \
devi@0: 		| sed '/LabMaker:START/,/LabMaker:END/ d' \
devi@0: 		> $temp_file
devi@0: 	cat $temp_file > $profile
devi@0: 	rm $temp_file
devi@0: }
devi@0: 
devi@0: install_editor()
devi@0: {
devi@0: editor=$1
devi@0: [ -e $editor.orig ] && cp $editor.orig $editor
devi@0: cp $editor $editor.orig
devi@0: cat <<'editor_wrapper' | sed "s@EDITOR@$editor@" > $editor
devi@0: #!/bin/sh
devi@0: 
devi@32: LMHOME=~/.lilalo
devi@0: if [ "${1#-}" = "$1" -a -d "$LMHOME" ]
devi@0: then
devi@0: 	LAB=`cat $LMHOME/lab`
devi@0: 	TIME="`date +%j${LAB}_%H:%M:%S`"
devi@0: 	DIR=""
devi@0: 	[ "${1#/}" = "$1" ] && DIR=$PWD/
devi@0: 	DIFFNAME=$PPID_${TIME}_`echo $DIR$1| sed s@_@__@ | sed 's@/@_@g'`.diff
devi@0: 	tmp="/tmp/lm-saved-$$"
devi@0: 	touch $1
devi@0: 	cp -- "$1" $tmp 2> /dev/null
devi@0: 	EDITOR.orig "$@" || ERR=1
devi@0: 	diff $tmp $1 > $LMHOME/$DIFFNAME 2> /dev/null
devi@0: 	rm $tmp 2> /dev/null
devi@0: 	if [ "$ERR" = 1 ]
devi@0: 	then
devi@0: 		false
devi@0: 	else
devi@0: 		true
devi@0: 	fi	
devi@0: else
devi@0: 	exec EDITOR.orig "$@"
devi@0: fi
devi@0: editor_wrapper
devi@0: 
devi@0: }
devi@0: 
devi@0: 
devi@0: uninstall_editor()
devi@0: {
devi@0: 	editor=$1
devi@0: 	[ -e $editor.orig ] && mv $editor.orig $editor
devi@0: }
devi@0: 
devi@0: if [ "$1" != "-d" ] 
devi@0: then 
devi@0: 	# INSTALLING LM
devi@0: 	if [ $# -gt 0 ] 
devi@0: 	then
devi@0: 		users_to_install="$*"
devi@0: 	fi
devi@0: 
devi@0: 	if [ -z "$users_to_install" ]
devi@0: 	then 
devi@0: 		show_usage
devi@0: 		exit
devi@0: 	fi	
devi@0: 
devi@8: 	for home in $users_to_install
devi@0: 	do 
devi@8: 		# fix this!
devi@8: 		user=${home%/} 
devi@8: 		user=${user##*/}
devi@32: 		mkdir -p $home/.lilalo
devi@32: 		echo $first_lab > $home/.lilalo/lab
devi@32: 		chown -R $user $home/.lilalo
devi@0: 
devi@8: 		#if [ ! -e $home/.bash_profile ] 
devi@8: 		#then
devi@8: 		#	echo '. ~/.bashrc' >> ~/.bash_profile
devi@8: 		#fi
devi@8: 		#[ -e $home/.bash_profile ] \
devi@8: 		#	&& install_to_profile $home/.bash_profile \
devi@8: 		#	&& echo LabMaker is installed to $home/.bash_profile
devi@8: 
devi@8: 		echo Don\'t forget to check .bash_profile for .bashrc call
devi@0: 
devi@34: 		[ -e $home/.bash_profile ] && install_to_profile $home/.bash_profile \
devi@34: 			&& install_to_profile $home/.bash_profile \
devi@34: 			&& echo LabMaker is installed to $home/.bash_profile
devi@0: 
devi@0: 		touch $home/.bashrc
devi@0: 		[ -e $home/.bashrc ] && install_to_bashrc $home/.bashrc \
devi@0: 			&& install_to_bashrc $home/.bashrc \
devi@0: 			&& echo LabMaker is installed to $home/.bashrc
devi@0: 	done
devi@0: 
devi@0: 	for editor in $editors_to_install
devi@0: 	do
devi@0: 		[ -e $editor ] \
devi@0: 			&& install_editor $editor \
devi@0: 			&& echo LabMaker is installed to $editor
devi@0: 	done
devi@0: else
devi@0: # UNINSTALLING LM
devi@0: 	shift
devi@0: 	users_to_install="$*"
devi@0: 	for user in $users_to_install
devi@0: 	do 
devi@0: 		home=$user		
devi@32: 		mkdir -p $home/.lilalo
devi@32: 		echo $first_lab > $home/.lilalo/lab
devi@32: 		chown -R ${user##*/} $home/.lilalo
devi@0: 
devi@8: 		#if [ ! -e $home/.bash_profile ] 
devi@8: 		#then
devi@8: 		#	echo '. ~/.bashrc' >> ~/.bash_profile
devi@8: 		#fi
devi@8: 		#[ -e $home/.bash_profile ] \
devi@8: 		#	&& uninstall_from_profile $home/.bash_profile \
devi@8: 		#	&& echo LabMaker is uninstalled from $home/.bash_profile
devi@0: 
devi@0: 		[ -e $home/.profile ] && uninstall_from_profile $home/.profile \
devi@0: 			&& uninstall_from_profile $home/.profile \
devi@0: 			&& echo LabMaker is uninstalled from $home/.profile
devi@0: 
devi@0: 		touch $home/.bashrc
devi@0: 		[ -e $home/.bashrc ] && uninstall_from_bashrc $home/.bashrc \
devi@0: 			&& uninstall_from_bashrc $home/.bashrc \
devi@0: 			&& echo LabMaker is uninstalled from $home/.bashrc
devi@0: 	done
devi@0: 
devi@0: 	for editor in $editors_to_install
devi@0: 	do
devi@0: 		[ -e $editor ] \
devi@0: 			&& uninstall_editor $editor \
devi@0: 			&& echo LabMaker is uninstalled from $editor
devi@0: 	done
devi@0: fi