#!/bin/csh

# etc/precmd - tcsh and vshnu/vsh interaction setup script
# Steve Kinzler, kinzler@cs.indiana.edu, Mar 98/Mar 99/Mar 02
# see website http://www.cs.indiana.edu/hyplan/kinzler/vshnu/
# http://www.cs.indiana.edu/hyplan/kinzler/home.html#unixcfg
# see also http://www.cs.indiana.edu/hyplan/kinzler/home.html#binp/penv

# For use with the vsh*<->tcsh integration.  This file allows directory
# changes to be communicated between vsh* and tcsh, and environment
# changes to be propogated from tcsh to vsh*.  It provides quick aliases
# for switching back to vsh*.  It assumes the `penv` command script is
# available.

# To use this, you may install this file as ~/etc/precmd and add the
# following to your ~/.tcshrc or ~/.cshrc file:
#
#	if ( $?prompt && $?tcsh ) then
#		setenv VSHNUTMP /tmp
#		alias V 'alias precmd "source ~/etc/precmd"; $VSH'
#		if ( ! $?alias_v ) then
#			alias v V
#			alias vv V
#			set alias_v
#		endif
#	endif
#	setenv VSH vshnu	# vsh* only
#
# and then start a new vsh* supplemental shell with a "v" command (the
# first time) or "V" (any time), and return to vsh* mode with "v".  "vv"
# returns to vsh* mode in the same directory you were last in there.

jobs -l > $VSHNUTMP/precmd$$
set vshpid=`sed -n '/[ (]vsh/s/.* \([0-9]*\) S[tu].*/\1/p' $VSHNUTMP/precmd$$`
\rm -f $VSHNUTMP/precmd$$
set vshtmp=$VSHNUTMP/vsh$vshpid vshenv=$VSHNUTMP/env$vshpid
unset vshpid

alias getvshdir 'set vshdir="`cat $vshtmp`"; \rm -f $vshtmp $vshenv'
alias setvshdir 'set umask=0`umask`; umask 077; echo "$cwd" > $vshtmp;' \
		'penv > $vshenv; umask $umask; unset umask'
alias vcd 'pushd "$vshdir" > /dev/null'
alias vv "alias precmd 'unalias precmd; getvshdir; vcd'; %vsh"
alias v 'setvshdir; vv'

unalias precmd; getvshdir; vcd
