#!/bin/sh

myreadlink()
{
    target="$("${READLINK}" "$1" 2>/dev/null)"
    if [ -z "$target" ]; then
        echo "$1"
    else
        if [ -n "${target##/*}" ]; then
            echo "$("${DIRNAME}" "$1")/${target}"
        else
            echo "${target}"
        fi
    fi
}

findcommand()
{
    command=$1
    path="$(which ${command} 2>/dev/null)"
    if [ -n "${path}" ]; then
        echo "${path}"
    else
        for path in /bin/${command} /usr/bin/${command} /dsl/bin/${command} /dsl/usr/bin/${command}; do
            if [ -x "${path}" ]; then
                echo "${path}"
                break
            fi
        done
    fi
}

findcommands()
{
    UNAME="$(findcommand uname)"
    DIRNAME="$(findcommand dirname)"
    BASENAME="$(findcommand basename)"
    HEAD="$(findcommand head)"
    READLINK="$(findcommand readlink)"
    LDD="$(findcommand ldd)"
    CUT="$(findcommand cut)"
}

findcommands

FORGE_TOOLS_OS="$(${UNAME})"
if [ "${FORGE_TOOLS_OS}" = Linux -a -n "${MMCS_SERVER_IP}" -o -n "${BG_IS_IO_NODE}" ]; then
    FORGE_TOOLS_ARCH=bgp
    VERSION=
elif [ "${FORGE_TOOLS_OS}" = Linux -a -n "${BGQDRV}" -o -f /bgsys/drivers/ppcfloor/bin/runjob -o -f "${BG_NODE_TYPE}" ]; then
    FORGE_TOOLS_ARCH=bgq
    VERSION=
else
    if [ "${FORGE_TOOLS_OS}" = Linux ] ; then
        if [ -f /etc/redhat-release ] ; then
            SED="$(findcommand sed)"
            FORGE_TOOLS_OS_DISTRIBUTION=Redhat
            FORGE_TOOLS_OS_VERSION="$("${HEAD}" --lines=1 /etc/redhat-release | "${SED}" 's/.*release \([0-9\.]\+\).*/\1/g' | "${CUT}" -d'.' -f1-2)"
        elif [ -f /etc/SuSE-release ] ; then
            SED="$(findcommand sed)"
            GREP="$(findcommand grep)"
            FORGE_TOOLS_OS_DISTRIBUTION=Suse
            FORGE_TOOLS_OS_VERSION="$("${HEAD}" --lines=1 /etc/SuSE-release  | "${SED}" 's/.* \([0-9\.]\+\).*/\1/g')"
            SUSE_VERSION="$("${GREP}" VERSION /etc/SuSE-release | "${SED}" 's/VERSION = \([0-9]\+\).*/\1/g')"
            SUSE_PATCHLEVEL="$("${GREP}" PATCHLEVEL /etc/SuSE-release | "${SED}" 's/PATCHLEVEL = \([0-9]\+\).*/\1/g')"
            if [ "${SUSE_VERSION}" = 11 ]; then
                if [ "${SUSE_PATCHLEVEL}" = 0 ]; then
                   FORGE_TOOLS_OS_VERSION=11
                else 
                    FORGE_TOOLS_OS_VERSION="11.${SUSE_PATCHLEVEL}"
                fi
            fi
        elif [ -x /usr/bin/lsb_release ] ; then
            LSB_RELEASE="$(findcommand lsb_release)"
            CUT="$(findcommand cut)"
            FORGE_TOOLS_OS_DISTRIBUTION=$("${LSB_RELEASE}" -i | "${CUT}" -f2)
            FORGE_TOOLS_OS_VERSION=$("${LSB_RELEASE}" -r | "${CUT}" -f2)
        fi
        ARCH="$(findcommand arch)"
        FORGE_TOOLS_ARCH="$("${ARCH}" 2>/dev/null || "${UNAME}" -m)"
    elif [ "${FORGE_TOOLS_OS}" = Darwin ] ; then
        SW_VERS="$(findcommand sw_vers)"
        FORGE_TOOLS_OS_DISTRIBUTION=MacOSX
        FORGE_TOOLS_OS_VERSION="$("${SW_VERS}" -productVersion)"
        FORGE_TOOLS_ARCH="$("${UNAME}" -m)"
    fi
    VERSION="${FORGE_TOOLS_OS_DISTRIBUTION}-${FORGE_TOOLS_OS_VERSION}-${FORGE_TOOLS_ARCH}"
fi
PROGRAM="$1"
if [ "$("${BASENAME}" "$("${DIRNAME}" "$0")")" != libexec -a "$("${BASENAME}" "${PROGRAM}")" = "${PROGRAM}" ]; then
    # If running from a flat directory structure then look in the current directory.
    FORGE_TOOLS_PATH="$("${DIRNAME}" "$0")"
else
    FORGE_TOOLS_PATH="$("${DIRNAME}" "$("${DIRNAME}" "$0")")"
fi

if [ -z "${PROGRAM}" ]; then
    echo "findarch: Missing argument." >&2
    exit 1
fi

shift
for ext in "${VERSION}" "${FORGE_TOOLS_ARCH}"; do
    filename="${FORGE_TOOLS_PATH}/${PROGRAM}.${ext}"
    if [ -f "${filename}" -o -h "${filename}" ]; then
        # Resolve symlinks (e.g. forge-backend.x86_64 may be symlinked to forge-backend
        # from the x86_64 install of Forge tools) so that the executed program gets the right
        # FORGE_TOOLS_PATH for the architecture.
        canonical="$(myreadlink "${filename}")"
        if [ -n "${canonical}" ]; then
            filename="${canonical}"
        fi
        exec "${filename}" "$@"
    fi
done
# Only require the user to provide a symlink for forge-backend.ARCH.  Assumes other
# binaries can be found by resolving that symlink.
if [ "${filename}" != libexec/forge-backend ]; then
    for ext in "${VERSION}" "${FORGE_TOOLS_ARCH}"; do
        filename="${FORGE_TOOLS_PATH}/libexec/forge-backend.${ext}"
        if [ -f "${filename}" -o -h "${filename}" ]; then
            # Resolve symlinks (e.g. forge-backend.x86_64 may be symlinked to forge-backend
            # from the x86_64 install of Forge tools) so that the executed program gets the right
            # FORGE_TOOLS_PATH for the architecture.
            canonical="$(myreadlink "${filename}")"
            if [ -n "${canonical}" ]; then
                filename="${canonical}"
            fi
            archRoot="$("${DIRNAME}" "$("${DIRNAME}" "${filename}")")"
            filename="${archRoot}/${PROGRAM}"
            exec "${filename}" "$@"
        fi
    done
fi
filename="${FORGE_TOOLS_PATH}/${PROGRAM}"
if ! type ldd >/dev/null 2>&1 || "${LDD}" "${filename}" >/dev/null 2>&1 || [ "$("${HEAD}" -c 2 "${filename}")" = '#!' ]; then
    shopt -s execfail 2>/dev/null
    exec "${filename}" "$@"
    status=$!
else
    status=127
fi
echo "No ${PROGRAM} found for the ${FORGE_TOOLS_ARCH} architecture." >&2
echo "See the user guide for instructions on installing Forge tools on heterogeneous systems." >&2
exit $status
