#!/bin/sh
#
AUTHOR='J. Pety <pety@iram.fr>'
PROJECT='GILDAS <http://www.iram.fr/IRAMFR/GILDAS>'
PROGNAME=`basename $0`
#
usage() {
    cat <<EOF 1>&2

Adds an additionnal directory path to an already existing path. Try to be
clever enough:
  1. To avoid adding directories which already belongs to the path;
  2. Replace a pattern by another one to enable easy shift of used
     GILDAS version.

usage: $PROGNAME [options] path additional-path

options:
  -o old-pattern    Define the pattern that must be replaced in the 
                    directory names
  -n new-pattern    Define the pattern that will be used for replacement
  -k std-path-name  Standard path names (PATH, LD_LIBRARY_PATH, PYTHONPATH,
                    TEXINPUTS, BSTINPUTS) are passed by name instead of
                    value through this option. This is to avoid problems
                    with white space characters.
  -h                Show this help page
  -v                Show version information

EOF
}
#
showversion() {
    echo "$PROGNAME, by $AUTHOR"
    echo "Project: $PROJECT"
}
#
message() {
    echo "$PROGNAME: $1"
}
#
error_exit() {
    echo 1>&2
    echo "$PROGNAME error: $1" 1>&2
    echo 1>&2
    exit 1
}
#
###########################################################################
#
# Option parsing
#
if [ "$GAG_TARGET_KIND" = "mingw" ]; then
  temp=`getopt "hvo:n:k:" "$@"|\sed 's&\\\\&/&g;s&;&:&g;s&\([ :]\)\([A-Za-z]\):/&\1/\2/&g'`
else
  temp=`getopt "hvo:n:k:" "$@"`
fi
if [ $? -ne 0 ]; then usage; exit 1; fi
eval set -- "$temp"
unset temp
while [ $1 != -- ]; do
    case $1 in
    -o) opattern=$2; shift; replace=1 ;;
    -n) npattern=$2; shift; replace=1 ;;
    -k) pathname=$2; shift; kind=1 ;;
    -v) showversion; exit 0 ;;
    -h) usage; exit 0 ;;
    esac
    shift # Next flag
done
shift # Skip double dash
if [ "$kind" ]; then
    case $pathname in
    PATH)            path=$PATH ;;
    LD_LIBRARY_PATH) path=$LD_LIBRARY_PATH ;;
    PYTHONPATH)      path=$PYTHONPATH ;;
    TEXINPUTS)       path=$TEXINPUTS ;;
    BSTINPUTS)       path=$BSTINPUTS ;;
    *) usage; error_exit "Unknown kind of path: $pathname" ;;
    esac
    if [ $# -eq 1 ]; then
	add=$1
    else
	usage
	error_exit "Only one path is needed in addition to the -k std-path-name"
    fi
else
    if [ $# -eq 1 ]; then
	path=""
	add=$1
    elif [ $# -eq 2 ]; then
	path=$1
	add=$2
    else
	usage
	error_exit "No more than two paths are needed"
    fi
fi
set abc; shift # This line to avoid remanence effect in a portable way
#
###########################################################################
#
# Preparation:
# 1. In $add, replace : by space when needed
# 2. Add : at beginning and end of $path
#
add=`echo ${add} | \sed -e "s&:& &g"`
path=":${path}:"
#
if [ "$replace" ]; then
    opattern=`echo ${opattern} | \sed -e "s&/$&&g"`
    npattern=`echo ${npattern} | \sed -e "s&/$&&g"`
    if [ ! -z "$opattern" ]; then
	path=`echo ${path} | \sed -e "s&${opattern}&${npattern}&g"`
    fi
    npattern=$npattern/
else
    npattern=""
fi
#
# 1. Go through the directory list of the $add path
# 2. Verify the directory exist
# 3. Add the directory name into $path if not already there
#
for dirname in $add
do
    unset dir
    # The if order matters here 
    if [ -d "${npattern}${dirname}" ]; then
       dir=$npattern$dirname
    elif [ -d "${dirname}" ]; then
       dir=$dirname
    fi
    if [ "$dir" != "" ]; then
	if [ `echo ${path} | grep -c "$dir:"` -eq 0 ]; then
	    path=:$dir$path
	fi
    fi
done
#
# 1. Replace :: by :
# 2. Eliminate trailing or leading :
#
path=`echo ${path} | \sed -e "s&::*&:&g" -e "s&:$&&g" -e "s&^:&&g"`
#
# If $path has . in it, put it at starts
#
if [ `echo ${path} | grep -c ":\.:"` -ge 1 ]; then
    path=".:"`echo ${path} | \sed -e "s&:.:&:&g" -e "s&:.$&&g"`
fi
echo $path
#
###########################################################################
