#!/bin/bash
# to be sourced by Bash

function addbintopath () {
  if [ -d $1/bin ]
  then
    export PATH=$1/bin:$PATH
  fi
}

function addtopath () {
  if [ -d $1 ]
  then
    if [ -d $1/bin ]
    then
      export PATH=$1/bin:$PATH
    fi
    if [ -d $1/scripts ]
    then
      export PATH=$1/scripts:$PATH
    fi
    if [ -d $1/man ]
    then
      export MANPATH=$1/man:$MANPATH
    else if [ -d $1/share/man ]
      then
        export MANPATH=$1/share/man:$MANPATH
      fi
    fi
    if [ -d $1/lib ]
    then
      # avoid trailing colon:
      export LD_LIBRARY_PATH=$1/lib${LD_LIBRARY_PATH:+":$LD_LIBRARY_PATH"}
    fi
  fi
}

export -f addtopath addbintopath

function isabsolute () {
  DIR=`dirname $1`
  [ $DIR == "/" ] || ([ $DIR != "." ] && isabsolute $DIR)
}

function fullpath () {
  if isabsolute $1
  then echo $1
  else echo $PWD/$1
  fi
}

export -f isabsolute fullpath

function filterFileExists () {
  for i in $*
  do
    if [ -f $i ]
    then
      echo $i
    fi
  done
}

export -f filterFileExists
