#!/bin/bash

if [ $# -eq 0 ] || [ $1 = '-h' ] || [ $1 = '--help' ]; then
    cat <<"END"
Usage: gled-config [--prefix] [--bindir] [--libdir] [--datadir]

  --prefix      Print installation prefix; if this was not set during
                configure, the path to this command will be used.
  --bindir      Print binary directory.
  --libdir      Print library directory.
  --datadir     Print data directory.

  --help, -h    Print usage information.
END
    exit 0
fi

########################################################################
# Configuration variables
########################################################################

INSTALL_TYPE="BLOB"

GLED_PREFIX="<none>"
GLED_BIN_DIR="<none>"
GLED_LIB_DIR="<none>"
GLED_DATA_DIR="<none>"

########################################################################
# Determine where the script was run from.
########################################################################

# work around readlink versions not having -f option
fullpath1=`readlink "$0"`
if [ $? -ne 0 ]; then
   fullpath1="$0"
fi
progdir=`dirname "$fullpath1"`
runningdir=`pwd`
if echo $progdir | grep "^/" > /dev/null 2>& 1 || \
   echo $progdir | grep "^~" > /dev/null 2>& 1; then
   # absolute path
   fullpath="$progdir"
else
   # relative path
   fullpath="$runningdir/$progdir"
fi
# work around readlink versions not having -f option
fullpath1=`readlink "$fullpath"`
if [ $? -ne 0 ]; then
   fullpath1="$fullpath"
fi
topdir=`dirname "$fullpath1"`

########################################################################
# Setup the default paths
########################################################################

if [ -n "$GLEDSYS" ]; then
    PREFIX="$GLEDSYS"
    BINDIR="$PREFIX/bin"
    LIBDIR="$PREFIX/lib"
    DATADIR="$PREFIX"
elif [ "$GLED_PREFIX" = "<none>" ]; then
    PREFIX="$topdir"
    BINDIR="$topdir/bin"
    LIBDIR="$topdir/lib"
    DATADIR="$topdir"
else
    PREFIX="$GLED_PREFIX"
    BINDIR="$GLED_BIN_DIR"
    LIBDIR="$GLED_LIB_DIR"
    DATADIR="$GLED_DATA_DIR"
fi

########################################################################
# Parse and process command-line options
########################################################################

out=""

while [ $# -gt 0 ]; do
  case "$1" in
  -*=*) optarg=`echo "$1" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
  *)    optarg= ;;
  esac

  case "$1" in
    --prefix)
      out="$out $PREFIX"
      ;;
    --bindir)
      out="$out $BINDIR"
      ;;
    --libdir)
      out="$out $LIBDIR"
      ;;
    --datadir)
      out="$out $DATADIR"
      ;;
    *)
      ### Give an error
      echo "Unknown argument \"$1\"!" 1>&2
      exit 1
      ;;
  esac
  shift
done

echo $out
