#!/bin/sh
install_modules="se,cdp"
server_address="172.20.31.169"
server_user_name="test"
agent_name="test_install"
seagent_package=dbackup-seagent
cdpagent_package=dbackup-cdpagent
install_se_agent_package_rpm()
{
    #get current package version
    seagent_rpm_name=$(ls -l ./ | grep ${seagent_package} | awk -F $seagent_package '{print $2}')
    if [ -z "$seagent_rpm_name" ];then
        echo "the package seagent is not exist"
        return
    fi
    #get install flag
    rpm -qa | grep dbackup-seagent >/dev/null 2>&1
    if [ `echo $?` -ne 0 ]; then
        echo "new install seagent"
        rpm -ivh ${seagent_package}${seagent_rpm_name}
    else
        echo "seagent is already installed, upgrade it"
        rpm -Uvh ${seagent_package}${seagent_rpm_name}
    fi
}
install_cdp_agent_package_rpm()
{
    cdpgent_rpm_name=$(ls -l ./ | grep ${cdpagent_package} | awk -F $cdpagent_package '{print $2}')
    if [ -z "$cdpgent_rpm_name" ];then
        echo "the package seagent is not exist"
        return
    fi
    rpm -qa | grep dbackup-seagent >/dev/null 2>&1
    if [ `echo $?` -ne 0 ]; then
        echo "new install seagent"
        rpm -ivh ${cdpagent_package}${cdpgent_rpm_name}
    else
        echo "module seagent is already installed, upgrade it"
        rpm -Uvh ${cdpagent_package}${cdpgent_rpm_name}
    fi
}
if [[ $install_modules =~ "se" ]]; then
    install_se_agent_package_rpm
fi

 if [[ $install_modules =~ "cdp" ]]; then
    install_cdp_agent_package_rpm
fi

#config seagent
seagent_config -server_ip=${server_address} -user_name=${server_user_name} -client_name=${agent_name}

