#!/bin/sh
# @(#) addpath 1.3 Delta: 95/02/16 11:57:47 Extraction: 97/05/13 12:40:43 @(#)
HOST=`hostname`
UNAME=`uname -a`
trim='delpath|addpath|egrep|ps|cut'
case $UNAME in
*SunOS*) 
    path_signature() {
        echo "${HOST} $id"  > $1
        ps -gu | 
		egrep -v "$trim" | 
		cut -c1-14,35-38,44-48,56-80 >> $1
    }
    ;;
*)
    path_signature() {
        echo " ${HOST} $id" > $1
        ps -f | 
		egrep -v "$trim" | 
		cut -c1-21,25-42,48-80 >> $1
    }
    ;;
esac


path_checkdir() {
	pid=$1
	# Garbage collect PathDir directories
	for i in ${HOME}/.PathDir/${HOST}/*
	do
	    rm -f $i/.keep
	    if [ -f $i/.Signature ]
	    then
		while read name pid junk
		do
		    if kill -0 $pid > /dev/null 2>&1
		    then
			echo > $i/.keep    
			break
		    fi
		done < $i/.Signature
	    fi
	    if [ ! -f $i/.keep ]
	    then
		rm -rf $i
	    fi
	done
	if cmp $PATH_DIR/.Signature $PATH_DIR/.current >/dev/null 2>&1
	then
		:
	else
		cp $PATH_DIR/.current $PATH_DIR/.Signature
		count=`echo $PATH_DIR | sed -e 's/.*_//;'`
		while [ -d ${HOME}/.PathDir/${HOST}/${pid}_${count} ]
		do
			count=`expr $count + 1`
		done
		NPATH_DIR=${HOME}/.PathDir/${HOST}/${pid}_${count}
		mkdir $NPATH_DIR
		(cd $PATH_DIR ; tar cf - .) | (cd $NPATH_DIR ; tar xf - )
		PATH_DIR=$NPATH_DIR
	fi
}

path_addit() {
	# make links for all the files in $1
	#

	if [ -d "$1" ] 
	then
	    dir="$1"
	    list=`cd $dir; /bin/ls`
	else
	    eval `echo "$1" | sed -e 's/\(.*\)\/\(.*\)/dir="\1";list="\2"/'`
	fi
	cd ${PATH_DIR}
	for i in $list
	do
	    if [ -x "$i" -a -x "$dir/$i" ]
	    then
		if $overwrite
		then
		    rm -f "$i"
		    ln -s "$dir/$i" "$i"
		elif $error
		then
		    echo "$0: attempt made to link $dir/$i to existing $i" >&2
		fi
	    elif [ -x "$dir/$i" ]
	    then
		ln -s "$dir/$i" "$i"
	    fi
	done
}

path_dropit() {
	# remove all the files in PATH_DIR that are symlinks including
	# our first argument

	cd ${PATH_DIR}
	for i in *
	do
	    if ls -l $i | egrep "$1/|$1\$" > /dev/null 2>&1
	    then
		rm $i
	    fi
	done
}

add_dir() {
    path_signature ${PATH_DIR}/.current
    path_checkdir $1
    PATH=`dropit -d: ${HOME}/.PathDir`:$PATH_DIR
    path_addit $2 
    path_signature ${PATH_DIR}/.Signature
}

dropit_dir() {
    path_signature ${PATH_DIR}/.current
    path_checkdir $1 
    PATH=`dropit -d: ${HOME}/.PathDir`:$PATH_DIR
    path_dropit $2 
    path_signature ${PATH_DIR}/.Signature
}

if [ "" = "$PATH_DIR" ]
then

	HOST=`hostname`					; export HOST
	PATH_DIR="${HOME}/.PathDir/${HOST}/${$}_0" 	; export PATH_DIR
	PATH=$PATH_DIR:`dropit ${HOME}/.PathDir`
	UNAME="`uname -a`"
	if [ ! -d $PATH_DIR ]
	then
	    mkdir -p $PATH_DIR
	    path_signature ${PATH_DIR}/.Signature
	    path_signature ${PATH_DIR}/.current
	    $PATH_DIR_DIR/path_checkdir $$ > /dev/null 2>&1
	fi
fi

error=false
overwrite=false

case "x$1" in
x-e)		error=true; shift ;;
x-o)		overwrite=true; shift;;
x-*)	echo 'usage:'
	echo '	eval `addpath -[eo] $$ file file...` 	-- add files to $PATH_DIR'
	echo '	eval `delpath  $$ file file...`		-- remove files from $PATH_DIR' 
	exit 1
	;;
esac

id="$1"
shift


case "$0" in
*addpath)	
	for i in "$@"
	do
		add_dir "$id" "$i"
	done
	;;
*delpath)	
	for i in "$@"
	do
		dropit_dir "$id" "$i"
	done
	;;
esac


case "$UPS_SHELL" in
*csh) 	echo "setenv PATH '$PATH'; setenv PATH_DIR '$PATH_DIR'"
	;;
*sh)	echo "PATH='$PATH';export PATH;PATH_DIR='$PATH_DIR';export PATH_DIR"
	;;
esac
