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@0: 	cat $profile \
devi@0: 		| sed '/LabMaker:START/,/LabMaker:END/ d' \
devi@0: 		> $temp_file
devi@0: 	cat <<'LM_bash_profile' >> $temp_file
devi@0: 	# LabMaker:START
devi@0: 	#LMHOME=~/.labmaker
devi@0: 	#mkdir -p ${LMHOME}
devi@0: 	#TTY=`tty`
devi@0: 	#flush="-f"
devi@0: 	#exec script $flush -q $LMHOME/${TTY##*/}-$$.script
devi@0: 	# 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_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@0: 	# LabMaker:START
devi@0: 	TTY=`tty` 
devi@0: 
devi@0: 	this_term=`w | grep "${TTY##/dev/}" | awk '{print $8;}'`
devi@0: 	# freeBSD: 
devi@8: 	#this_term=`w | grep "${TTY##/dev/tty}" | awk '{print $6;}'`
devi@0: 	if [ -n "$this_term" ] && echo $this_term | grep -qv script
devi@0: 	then	
devi@0: 		LMHOME=~/.labmaker
devi@0: 		mkdir -p ${LMHOME}
devi@0: 		flush="-f"	#linux
devi@8: 		#flush="-t 0"	#freebsd
devi@8: 		PS1='\[`	a="$?";
devi@8: 			HIDDEN=$([ "$a" = 0 ] || echo -n ^"$a")$(echo -n _${UID}_)$(echo -n _$$_)$(date\
devi@8: 				+"%j$(cat ~/.labmaker/lab 2>/dev/null) %H:%M:%S");
devi@8: 			echo $HIDDEN`\033[50D\033[K\][\u@\h:\W]\$ '
devi@0: 		exec script $flush -q $LMHOME/${TTY##*/}-$$.script
devi@0: 	fi
devi@0: 	# 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@0: LMHOME=~/.labmaker
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@0: 		mkdir -p $home/.labmaker
devi@0: 		echo $first_lab > $home/.labmaker/lab
devi@8: 		chown -R $user $home/.labmaker
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@0: 		[ -e $home/.profile ] && install_to_profile $home/.profile \
devi@0: 			&& install_to_profile $home/.profile \
devi@0: 			&& echo LabMaker is installed to $home/.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@0: 		mkdir -p $home/.labmaker
devi@0: 		echo $first_lab > $home/.labmaker/lab
devi@0: 		chown -R ${user##*/} $home/.labmaker
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