#!/bin/bash
# info: update vesta after rpm update
# options: version
#
# The function is runs as rpm update trigger. It pulls shell script from vesta
# server and runs it.


#----------------------------------------------------------#
#                    Variable&Function                     #
#----------------------------------------------------------#

# Argument defenition
version=$1
updates=''

# Importing system enviroment
source /etc/profile.d/vesta.sh

# Includes
source $VESTA/conf/vesta.conf
source $VESTA/func/main.sh


#----------------------------------------------------------#
#                    Verifications                         #
#----------------------------------------------------------#

# Checking arg number
check_args '1' "$#" 'version'


#----------------------------------------------------------#
#                       Action                             #
#----------------------------------------------------------#

# Compare versions
if [ "$version" != "$VERSION" ]; then
    # Downloading version tree
    upd_host="yum.vestacp.com"
    wget -O "/tmp/versions" http://$V_UPD_HOST/upd_scripts/version_tree.txt \
        &>/dev/null

    # Checking download result
    if [ "$?" -ne "0" ]; then
        echo "Error: version tree update failed"
        log_event "$E_UPDATE" "$EVENT"
        exit $E_UPDATE
    fi

    # Deviding version
    v1=$(echo "$version" |cut -f 1 -d '.')
    v2=$(echo "$version" |cut -f 2 -d '.')
    v3=$(echo "$version" |cut -f 3 -d '.')
    V1=$(echo "$VERSION" |cut -f 1 -d '.')
    V2=$(echo "$VERSION" |cut -f 2 -d '.')
    V3=$(echo "$VERSION" |cut -f 3 -d '.')


    # Checking difference between versions
    # Too nested tests, sory about complexity
    if [ "$V1" -lt "$v1" ]; then
        for ver in $(seq $V1 $v1); do
            updates="$updates $(grep "^$ver." /tmp/versions|grep ":1$"|\
                cut -f 1 -d :)"
        done
    else
        if [ "$V2" -lt "$v2" ]; then
            for ver in $(seq $V2 $v2); do
                updates="$updates $(grep "^$v1.$ver." /tmp/versions |\
                    grep ":1$"|cut -f 1 -d :)"
            done
        else
            V4=$((V3 + 1))
            for ver in $(seq $V4 $v3); do
                updates="$updates $(grep "^$v1.$v2.$ver" /tmp/versions |\
                    grep ":1$"|cut -f 1 -d :)"
            done
        fi
    fi

    # Executing update scripts
    if [ ! -z "$updates" ]; then
        mkdir $BIN/updates &>/dev/null

        for update in $updates; do
            wget -O $BIN/updates/$update.sh \
                http://$V_UPD_HOST/upd_scripts/$update.sh &>/dev/null

            # Checking download result
            if [ "$?" -ne "0" ]; then
                echo "Error: version tree update failed"
                log_event "$E_UPDATE" "$EVENT"
                exit $E_UPDATE
            fi
            bash  $BIN/updates/$update.sh
        done
        rm -rf $BIN/updates
    fi

    # Updating config version
    sed -i "s/VERSION='$VERSION'/VERSION='$version'/g" $VESTA/conf/vesta.conf
fi


#----------------------------------------------------------#
#                       Vesta                              #
#----------------------------------------------------------#

# Logging
log_event "$OK" "$EVENT"

exit
