#! /bin/bash
#############################################################################
# This script adds directories to the PATH environment variable.            #
# It removes redundant entries, and -- using the -r option --               #
# removes directories from PATH.                           (HDM 12/99)      #
#############################################################################
add2path()
{
purp="Purpose: Adds directories to PATH (or removes them)."
usage="Usage: add2path [ -a | -r | -h ] [ directory1 directory2 ...  ]."
help1='-h : Print this help text.'
help2='-a : Append directories to the end of PATH, '
help3='rather than inserting them at the beginning.'
help4='-r : Remove directories from the PATH. You will be prompted'
help5='for the numbers of the directories to be removed.'
help6='Alternatively one may use "add2path -r [num1 num2 ...]".'
help7='Note: add2path called without arguments will display the PATH.'
help8='Note: Multiple (redundant) entries will be removed automatically.'
help9='Note: This is a bash function.'
remove=0
append=0
#-----------------------------------------------------------------------
#  Read the options (if any).
#-----------------------------------------------------------------------
list=$@
unset numlist
if [ "$1" = "-h" ] || [ "$2" = "-h" ] ; then
    echo $purp
    echo $usage
    echo ' '$help1
    echo ' '$help2
    echo '      '$help3
    echo ' '$help4
    echo '      '$help5
    echo '      '$help6
    echo $help7
    echo $help8
    echo $help9
    return

elif [ "$1" = "-r" ] ; then
    remove=1
    numlist=${list#"-r"}
    unset list

elif [ "$1" = "-a" ] ; then
    append=1
    list=${list#"-a"}


elif [ "$(echo $1 | cut -c1)" = "-" ] ; then
    echo 'Unknown option !  '$1
    echo $purp
    echo $usage
    echo ' '$help1
    echo ' '$help2
    echo '      '$help3
    echo ' '$help4
    echo '      '$help5
    echo '      '$help6
    echo $help7
    echo $help8
    echo $help9
    return
fi

if [ "$(echo $2 | cut -c1)" = "-" ] ; then
    echo 'There must be only one option !'
    echo '  [ -a | -r | -h ] not : '$1'  '$2
    return
fi

path=:
for i in $list ; do
  if [ -d $i ] ; then
      path=$path:$i
  else
      echo $i' is not a directory! Not added to PATH.'
  fi
done
path=${path#::}
path=${path#:}

if [ "$append" = 0 ] ; then
   path=$path:$PATH:
else
   path=$PATH:$path:
fi
path=${path#:}
path=${path%::}
path=${path%:}
path=$path:

### ----- The following lines remove redundant entries from path ------ ###

newpath=:
i=0
while [ $path ] ; do
  p1=${path%%:*}
  path=${path#*:}
  if [ "$newpath" = "${newpath%:$p1:*}" ] ; then
    newpath=$newpath$p1:
    i=$(($i+1))
    echo $i'  '$p1
  fi
done
newpath=${newpath#:}
newpath=${newpath%:}

if [ "$remove" = 0  ] ; then
  export PATH=$newpath
  return
fi

### ----------- REMOVAL OF DIRECTORIES ------------------- ###

if [ -z "$numlist" ] ; then
  echo ' Give the number(s) of the director(y|ies) to be removed: '
  echo ' Supply a space separated list. '
fi

while : ; do
   if [ -z "$numlist" ] ; then
       read -p ' remove ? (q=quit) ' numlist
   fi
   if [ "$numlist" = "q" ] ; then
         export PATH
	 return
   fi

### Check for correct input ###
   unset nlist
   for num in $numlist; do
     if [ -n "${num##[0-9]*}" ] || [ -n "${num%%*[0-9]}" ] || \
      [ -n "${num##*[0-9]}" ] || [ -n "${num%%[0-9]*}" ]; then num=0; fi
     if [ $num -gt 0 ] ; then
         nlist="$nlist $num"
     fi
   done

   echo 'removing : '$numlist
   echo 'new PATH reads:'
   unset numlist
   path=$newpath:
   newpath=:
   i=0
   j=0
   while [ $path ] ; do
       p1=${path%%:*}
       path=${path#*:}
       j=$(($j+1))
       switch=0
       for num in $nlist; do
           if [ $j -eq $num ] ; then switch=1 ; fi
       done
       if [ $switch -eq 0 ] ; then
	   newpath=$newpath$p1:
	   i=$(($i+1))
	   echo $i'  '$p1
       fi
   done
   newpath=${newpath#:}
   newpath=${newpath%:}
   PATH=$newpath
done
export PATH
return
}

