#!/bin/sh
##########################################################################################
##
## DDMwrap - Execute a DQ2 Client command inside a wrapper
##
## This script allows one to execute any DQ2Client command in the context of the python 2.6
## This allows a process to remain in the python 2.4 context, while running DQ2Client commands
##
## This script sets up the DQ2Client, then executes the command in that environment as an "exec"
##
## The callers process environment remains unchanged upon completion of the command
##
## Usage:
##
## Create a softlink to DQ2wrap for the command you want to run in the ALRB context
## This softlink "should" reside in the same directory as the ALRBwrap script
##
## This directory must then placed at the begining of the users PATH variable
##
## The softlink command is then executed in place of the "real" command
##
## chmod 755 $HOME/DDM/DDMwrap
## ln -s $HOME/DDM/DDMwrap $HOME/DDM/dq2-ls
## PATH=$HOME/DDM:$PATH
## dq2-ls
##
##
##########################################################################################


# DQ2 version to use from DDM
DQ2_Version=2.3.0

# Location of DDM products
DQ2_Base=/cvmfs/atlas.cern.ch/repo/sw/ddm


# Version of the DDMwrap script
DDM_Version=1.0-1


##########################################################################################
##
#
# Extract the strings, deliminated by :, which matches the given pattern
#
# Usage:
#
#	NewString=$(DDM_exPatt String Pattern)
#
# Where:
#
#	String		String to examine with : deliminiators
#	Pattern		The pattern to match between the :
#
# Example:
#
#	DQ2PYTHONPATH=$(DDM_exPatt $PYTHONPATH "/DQ2Client/")
#
##
##########################################################################################

function DDM_exPatt () {

  ArgList=$1
  ArgPatt=$2

  [[ -z $ArgPatt ]] && ArgPatt=' '

  # Need this to end the loop
  ArgList=$ArgList:


  # Start with a null list
  myList=""

  # End when out of arguments
  until [[ -z $ArgList ]]; do

    # Extract an argument ending in a :
    Arg=${ArgList%%:*}
   
    # See if it contains the pattern given
    echo $Arg | grep -q $ArgPatt

    # If match found, add to my list separated by a :
    [[ $? -eq 0 ]] && myList=$myList:$Arg

    # Remove the string we just compared
    ArgList=${ArgList#*:}

  done

  # Extract the leading : from the first argumennt added
  myList=${myList#*:}

  # Return what we found
  echo $myList

}

##
##########################################################################################



# Sanity check - make certain we are not in a loop

if [[ -n $DDMpid ]]; then
   echo "%DDMwrap infinite loop detected - exiting"
   exit 1
else
   export DDMpid=$$
fi


# Setup that Version of DQ2
source $DQ2_Base/$DQ2_Version/setup.sh


# Get the base of the command being executed
BaseCMD=$(basename $0)


##########################################################################################
##
## Check for any built-in commands such as ddm-version
##
## Build the command we will later "exec"
##
##########################################################################################


# Build a full command looking for some internal commands

case $BaseCMD in

  (ddm-version)

    FullCMD="echo $DDM_Version"
    ;;

  (ddm-python)

    FullCMD="python $*"
    ;;

  (ddm-pythonpath)

    FullCMD="echo $PYTHONPATH"
    ;;

  (ddm-ldlibrarypath)

    FullCMD="echo $LD_LIBRARY_PATH"
    ;;

  (ddm-dq2version)

    FullCMD="echo $DQ2_Version"
    ;;

  (ddm-dq2pythonpath)

    FullCMD="echo $(DDM_exPatt $PYTHONPATH '/DQ2Clients/')"
    ;;

  (*)

    FullCMD="$BaseCMD $*"
    ;;

esac



##########################################################################################
##
## Execute the command as an "exec"
##
## The environment we just set is destroyed when the command completes and returns to the caller
##
##########################################################################################

exec $FullCMD
