#!/bin/sh

# Запуск 
#	update.sh - обновление до последней сборки
#	update.sh <set> - обновление до указанной сборки, номер сборки должен быть больше текущей

cd ${0%${0##*/}}.

SERVER=/etc/init.d/bgbilling
SCHEDULER=/etc/init.d/bgscheduler
DATALOADER=/etc/init.d/bgdataloader
TEE=/usr/bin/tee
DATE=/bin/date

snapshot=`find ./snapshots -type f -mtime 0 | tail -1`

if [ -z $snapshot ]; then
    echo "Actual snapshot not found!"
    exit 0
fi

echo "Found actual snapshot: $snapshot"

time=`${DATE} +%d_%H:%M:%S`

if [ -e ${DATALOADER} ]; then
    echo "# Stopping DataLoader"
    ${DATALOADER} stop
fi
echo "# Stopping Scheduler"
${SCHEDULER} stop
echo "# Stopping Server"
${SERVER} stop
echo "# Start Updating"
 ./bg_installer.sh update $1 | ${TEE} ./log_update_${time}
echo "# Starting Server"
${SERVER} start
echo "# Starting Scheduler"
${SCHEDULER} start
if [ -e ${DATALOADER} ]; then
    echo "# Starting DataLoader"
    ${DATALOADER} start
fi

if [ -e .bgclient_keystore_user -o -e .bgclient_keystore ]; then
    ./bgclient_javawebstart.sh
fi
