#!/bin/bash -e
#
# $Id: /drqueue/remote/tags/0.64.3/bin/shlib 2000 2007-07-21T07:29:37.530137Z jorge  $
#


####
#
# Defaults
#

DRQ_SH_AUTOESCAPE=0
DRQ_SH_UNDERSCORE=1

MACHINE=`uname -m`
KERNEL=`uname -s`
Universal_Binaries_Short_Name='fat'

underscore_spaces () {
    ORIG_STRING="$1"
    echo $( echo $ORIG_STRING | tr ' ' '_' )
    return 0
}

underscore_slashes () {
    ORIG_STRING="$1"
    echo $( echo $ORIG_STRING | tr '/' '_' )
    return 0
}

underscore_spaces_and_slashes () {
    ORIG_STRING="$1"
    echo -n $( underscore_slashes "$( underscore_spaces "$ORIG_STRING" )" )
    return 0
}

escape_spaces_string () {
  ORIG_STRING="$1"
  if [ -z "$1" ]; then
      return 1
  else
      echo -n $( echo $ORIG_STRING | sed 's/ /\\ /g' )
  fi
  return 0
}

check_environment () {
  if [ -z ${DRQUEUE_ROOT} ]; then
    echo ERROR: DRQUEUE_ROOT not set in environment
    exit 1
  fi
  if [ -z ${DRQUEUE_MASTER} ]; then
    echo ERROR: DRQUEUE_MASTER not set in environment
    exit 1
  fi
}

get_env_drqueue_root () {
    check_environment
    if [ ! -z $DRQ_SH_AUTOESCAPE -o ! -z "$1" ]; then
      echo -n $( escape_spaces_string "$DRQUEUE_ROOT" )
    else
      echo -n $DRQUEUE_ROOT
    fi
    return 0
}

get_env_base_tool () {
    if [ ! -z $DRQ_SH_AUTOESCAPE -o ! -z "$1" ]; then
        echo -n $(escape_spaces_string "$1")
    else
        echo -n "$1"
    fi
}

get_env_kernel () {
    if [ ! -z "$DRQ_SH_UNDERSCORE" -o ! -z "$1" ]; then
        echo -n $( underscore_spaces_and_slashes "${KERNEL}" )
    else
        echo -n "${KERNEL}"
    fi
}

get_env_machine () {
    if [ ! -z "$DRQ_SH_UNDERSCORE" -o ! -z "$1" ]; then
        echo -n $( underscore_spaces_and_slashes "${MACHINE}" )
    else
        echo -n "${MACHINE}"
    fi
}

get_env_drqueue_bin () {
    if [ ! -z "$DRQUEUE_BIN" ]; then
        echo -n $DRQUEUE_BIN
    else
        echo -n $( get_env_drqueue_root )/bin 
    fi
}

wrapper_cmd () {
  CMD_TO_WRAP=$1
  _conv_to_underscores=$2
  unset WRAP_CMD
  _full_unwrapped_tool_path="$( get_env_drqueue_bin )/$CMD_TO_WRAP"
  if [ ! -x "$_full_unwrapped_tool_path" ]; then
    echo ERROR: Command "$_full_unwrapped_tool_path" does not exist. Cannot be wrapped.
    return 1
  fi

  _full_wrapped_path_without_arch=$( get_env_drqueue_bin )/$( get_env_base_tool "$BASETOOL" ).$( get_env_kernel )
  #
  # Universal Binaries workaround
  #
  if [ ${KERNEL} == "Darwin" -a -x /usr/bin/lipo -a -f ${_full_wrapped_path_without_arch}.${Universal_Binaries_Short_Name} ]
  then
    _full_wrapped_path=${_full_wrapped_path_without_arch}.${Universal_Binaries_Short_Name}
  else
    _full_wrapped_path=$( get_env_drqueue_bin )/$( get_env_base_tool "$BASETOOL" ).$( get_env_kernel ).$( get_env_machine )
  fi
  WRAP_CMD="$_full_wrapped_path"

  if [ ! -x "${WRAP_CMD}" ]; then
    echo ERROR: the wrapper "$( get_env_base_tool )" for your platform \(${WRAP_CMD}\) does not exist.
    echo Please check your installation. If this problem keeps showing up, please report it.
    return 1
  fi
  return 0
}

