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

BuildVersionCompare() #{{{
{
    n_major=`echo ${1} | cut -d. -f1` # existing versions
    n_minor=`echo ${1} | cut -d. -f2 | cut -c1`

    h_major=`echo ${2} | cut -d. -f1` # have versions
    h_minor=`echo ${2} | cut -d. -f2 | cut -c1`

    [ ${h_major} -lt ${n_major} ] && return 1
    [ ${h_minor} -lt ${n_minor} ] && return 1

    if [ "${3}" = "-with-build" ]; then
        n_build=`echo ${1} | cut -d. -f3`
        h_build=`echo ${2} | cut -d. -f3`

        [ ${h_build} -lt ${n_build} ] && return 1
    fi

    return 0
} #}}}

VerifyInstallation() #{{{
{
    if [ ! -d "${install_dir}" ]; then #{{{
        FailInstall ${INSTALL_FAILED} "${install_dir} does not exist. `InstallName` can only be installed into an existing location."
    fi #}}}

    if [ ! -w "${install_dir}" ]; then #{{{
        FailInstall ${INSTALL_FAILED} "${install_dir} is not writeable."
    fi #}}}

    if [ ! -w "${install_dir}/Global.vvc" ]; then #{{{
        FailInstall ${INSTALL_FAILED} "${install_dir} does not contain an Agent installation. `InstallName` can only be installed into an existing location."
    fi #}}}

    installed_version=`${binawk} '/BuildVersion/ { print $3; }' "${install_dir}/Global.vvc"`

    BuildVersionCompare ${dist_version} ${installed_version}
    if [ ${?} -ne 0 ]; then #{{{
        FailInstall ${INSTALL_FAILED} "The Agent installed in ${install_dir} is older than `InstallName` and must be upgraded prior to installation."
    fi #}}}
} #}}}

PrepareInstallation() #{{{
{
    MakeRollbackDirectory

    rollback=1
} #}}}

pluginInstall=1
