#! /bin/sh
# vim:sts=4 sw=4 ts=4 et ai foldmethod=marker

CheckLibcVersion() #{{{
{
    version=`ldd --version | head -n 1 | cut -d')' -f 2`
    major=`echo ${version} | cut -d. -f 1`
    minor=`echo ${version} | cut -d. -f 2`
    if [ $IgnoreLibs -eq 1 ] ; then
        return
    fi
    if [ $major -gt 2 ] ; then
        return
    fi
    if [ $major -eq 2 -a $minor -ge 16 ] ; then
        return
    fi
    Message "This version of the agent requires libc version 2.16 or higher"
    exit 1
} #}}}

PlatformPrerequisites() #{{{
{
    os_name=`uname -s`

    if [ "${os_name}" != "Linux" ]; then #{{{
        Message <<EOS

This Agent package is only intended for Linux.
EOS

        exit 1
    fi #}}}

    CheckLibcVersion
    platform=${os_name}
    platform_arch=`uname -sm | sed 's/ /-/'`
    platform_libs='/usr/lib'
    debian_libs='/usr/lib/i386-linux-gnu'

    if [ `uname -m` = 'x86_64' ] ; then #{{{
        platform_libs="${platform_libs}64"
        debian_libs='/usr/lib/x86_64-linux-gnu'
    fi #}}}

    detected=
    v46="${dn0}/${platform_arch}-stdc++v4_6"
    v36="${dn0}/${platform_arch}-stdc++v3_6"
    v35="${dn0}/${platform_arch}-stdc++v3_5"
    v2x="${dn0}/${platform_arch}-no_stdc++v3"

    # currently we are only building the agent using library version 4.6
    # if this directory does not exist, the installation package does not match the platform
    if ! [ -d "${v46}" ] ; then #{{{
        if [ `uname -m` = 'x86_64' ] ; then #{{{
            Message "Error: You are trying to install 32 bit agent on a 64 bit machine"
        else
            Message "Error: You are trying to install 64 bit agent on a 32 bit machine"
        fi #}}}
        exit 1
    fi #}}}

    if [ -f /usr/lib/libstdc++.so.6 -a -d "${v36}" ]; then #{{{
        distro="${platform_arch}-stdc++v3_6"
        detected=`ls -l /usr/lib/libstdc++.so.6 | cut -d\> -f2`
    fi #}}}

    if [ -z "${detected}" -a -f ${platform_libs}/libstdc++.so.6 -a -d "${v46}" ]; then #{{{
        distro="${platform_arch}-stdc++v4_6"
        detected=`ls -l ${platform_libs}/libstdc++.so.6 | cut -d\> -f2`
    fi #}}}

    if [ -z "${detected}" -a -f ${debian_libs}/libstdc++.so.6 -a -d "${v46}" ]; then #{{{
        distro="${platform_arch}-stdc++v4_6"
        detected=`ls -l ${debian_libs}/libstdc++.so.6 | cut -d\> -f2`
    fi #}}}

    if [ -z "${detected}" -a -f /usr/lib/libstdc++.so.5 -a -d "${v35}" ]; then #{{{
        libver=`ls -l /usr/lib/libstdc++.so.5 | cut -d\> -f2 | cut -d. -f3- | sed -e 's/\.//g'`

        if [ ${libver} -ge 503 ]; then #{{{
            distro="${platform_arch}-stdc++v3_5"
            detected=`ls -l /usr/lib/libstdc++.so.5 | cut -d\> -f2`
        fi #}}}
    fi #}}}

    if [ -z "${detected}" -a -f /usr/lib/libstdc++-libc6.2-2.so.3 -a -d "${v2x}" ]; then #{{{
        distro="${platform_arch}-no_stdc++v3"
        detected='/usr/lib/libstdc++-libc6.2-2.so.3'
    fi #}}}

    if [ -z "${detected}" ]; then #{{{
        Message "The Agent requires one of the following libraries:"
        [ -d "${v36}" ] && Message "    /usr/lib/libstdc++.so.6"
        [ -d "${v35}" ] && Message "    /usr/lib/libstdc++.so.5 (libstdc++.so.5.0.3 or higher)"
        [ -d "${v2x}" ] && Message "    /usr/lib/libstdc++-libc6.2-2.so.3"
        Message <<EOS

A requisite version cannot be found.
EOS
        exit 1
    fi #}}}

    if [ -z "${init_dir}" -o ! -d "${init_dir}" ]; then #{{{
        if [ -f /etc/rc.d/init.d/functions ]; then #{{{ Red Hat
            init_dir=/etc/rc.d/init.d
            rc_vvagent=rc.vvagent.r
        elif [ -f /etc/rc.status ]; then # SUSE
            init_dir=/etc/init.d
            rc_vvagent=rc.vvagent.s
        elif [ -f /lib/lsb/init-functions ]; then # Debian/Ubuntu
            init_dir=/etc/init.d
            rc_vvagent=rc.vvagent.d
        else # Other
            init_dir=/etc/rc.d
            rc_vvagent=rc.vvagent.g
        fi #}}}
    fi #}}}

    return 0
} #}}}

PlatformSetup() #{{{
{
    binsh=/bin/bash
    binawk=`which awk`

    run_levels="2 3 5"
    rc_root=/etc
    cp_args=-d
    start_level=95
    stop_level=05
    LD_LIBRARY_PATH="${dn0}/${distro}"

    if [ -d /opt ]; then
        default_dir=/opt/BUAgent
    elif [ -d /usr/local ]; then
        default_dir=/usr/local/BUAgent
    else
        default_dir=/usr/BUAgent
    fi

    export binsh binawk os_name platform distro cp_args
} #}}}

TestRunning() #{{{
{
    ps -e | grep -w "${1}" | grep -v grep | wc -l
} #}}}

GetFreeSpace() #{{{
{
    df -kP "${1}" | tail -1 | awk '{ print $4; }'
} #}}}

GetSpace() #{{{
{
    du -k "${1}" | tail -1 | cut -f1
} #}}}

InstallPlatformFiles() #{{{
{
  for file in "${source_dir}/bmr/"*; do
    InstallFile "${file}"
  done
  return
} #}}}

RegisterRCScript() #{{{
{
    if [ -x /sbin/chkconfig ]; then #{{{
        /sbin/chkconfig --add vvagent 2> /dev/null > /dev/null
    elif [ -x /usr/sbin/update-rc.d ]; then
        /usr/sbin/update-rc.d vvagent defaults > /dev/null 2> /dev/null
    else
        for level in ${run_levels}; do #{{{
            if [ -d ${rc_root}/rc${level}.d ]; then #{{{
                sf=${rc_root}/rc${level}.d/S95vvagent
                kf=${rc_root}/rc${level}.d/K05vvagent
            elif [ -d ${rc_root}/rc.d/rc${level}.d ]; then
                sf=${rc_root}/rc.d/rc${level}.d/S95vvagent
                kf=${rc_root}/rc.d/rc${level}.d/K05vvagent
            fi #}}}

            if [ ! -h ${sf} ]; then #{{{
                if [ -f ${sf} ]; then #{{{
                    LogMessage "Cannot create ${sf}; already exists as a file."
                else
                    ln -s ${init_dir}/vvagent ${sf}
                fi #}}}
            fi #}}}

            if [ ! -h ${kf} ]; then #{{{
                if [ -f ${kf} ]; then #{{{
                    LogMessage "Cannot create ${kf}; already exists as a file."
                else
                    ln -s ${init_dir}/vvagent ${kf}
                fi #}}}
            fi #}}}
        done #}}}
    fi #}}}
} #}}}

CheckValidInstallDir() #{{{
{
    for mount in `mount | awk '{print $3}'`
    do
        if [ "${mount}" = "${1}" ]
        then
            echo
            echo "Invalid installation directory. The installation directory cannot be a mount point."
            echo
            return 0
        fi    
    done
    return 1
} #}}}
