#!/bin/bash

dirboss=/opt/Boss

chown root:root *
chmod a+x *.sh.txt
chmod 664 *.jar *.gif *.png

if [ `ls /root/update/update-centrala-NEMAZAT.txt | wc -l` != 0 ] ; then
    echo `date`": CENTRALA" > update.log
    cp --remove-destination *.{ear,jar,gif,ico,png,txt} /root/download/001 2> fail
    rm /root/download/001/transform-getdown.*.txt 2> fail
    /root/download/001/upload-version-central.sh 2> fail
    cat fail >> update.log
    rm fail
else
    mkdir bak
    mkdir $dirboss/client/getdown
    mkdir $dirboss/client/getdown/bak
    mkdir $dirboss/client/getdown/bin
    mkdir $dirboss/client/getdown/img

    echo `date`": FILIALKA" > update.log

    echo `date`": GETDOWN prepare" >> update.log
    echo "Remove: /opt/Boss/client/getdown/bin/transform-getdown.sh" >> update.log
    mkdir $dirboss/client/getdown
    mkdir $dirboss/client/getdown/bin
    rm $dirboss/client/getdown/bin/transform-getdown.sh 2> fail
    cat fail >> update.log

    echo "Updating: /opt/Boss/client/getdown/bin/update-getdown.sh" >> update.log
    cp $dirboss/client/getdown/bin/update-getdown.sh bak 2> fail
    cp --remove-destination update-getdown.sh.txt $dirboss/client/getdown/bin/update-getdown.sh 2> fail
    cat fail >> update.log

    echo "Updating: /opt/Boss/client/getdown/img/bossicon32a.gif" >> update.log
    cp $dirboss/client/getdown/img/bossicon32a.gif bak 2> fail
    cp --remove-destination bossicon32a.gif $dirboss/client/getdown/img 2> fail
    cat fail >> update.log

    echo "Updating: /opt/Boss/client/getdown/img/splash.gif" >> update.log
    cp $dirboss/client/getdown/img/splash.gif bak 2> fail
    cp --remove-destination splash.gif $dirboss/client/getdown/img 2> fail
    cat fail >> update.log

    echo "Updating: /opt/Boss/client/lib/getdown.jar" >> update.log
    cp $dirboss/client/lib/getdown.jar bak 2> fail
    cp --remove-destination getdown.jar $dirboss/client/lib 2> fail
    cat fail >> update.log

    echo "Updating: /opt/Boss/bossicon32a.png" >> update.log
    cp $dirboss/bossicon32a.png bak 2> fail
    cp --remove-destination bossicon32a.png $dirboss/bossicon32a.png 2> fail
    cat fail >> update.log

    if [ `ls boss.jar | wc -l` = 0 ] ; then
        echo `date`": No boss.jar to update" >> update.log
    else
        echo "Updating: /opt/Boss/client/boss.jar" >> update.log
        cp $dirboss/client/boss.jar bak 2> fail
        cat fail >> update.log
        cp --remove-destination boss.jar $dirboss/client 2> fail
        cp --remove-destination boss.jar $dirboss/update/prepare/jar 2> fail
        cat fail >> update.log

        echo "Updating: /opt/Boss/client/launcher.jar" >> update.log
        cp $dirboss/client/launcher.jar bak 2> fail
        cat fail >> update.log
        cp --remove-destination launcher.jar $dirboss/client 2> fail
        cp --remove-destination launcher.jar $dirboss/update/prepare/jar 2> fail

        if [ `ls mob.jar | wc -l` = 0 ] ; then
            echo `date`": No mob.jar to update" >> update.log
        else
            echo "Updating: /opt/Boss/client/mob.jar" >> update.log
            cp $dirboss/client/mob.jar bak 2> fail
            cat fail >> update.log
            cp --remove-destination mob.jar $dirboss/client 2> fail
            cp --remove-destination mob.jar $dirboss/update/prepare/jar 2> fail
            cat fail >> update.log
        fi

        up=1
    fi

    if [ `ls BossME.jar | wc -l` = 0 ] ; then
        echo `date`": No BossME.jar to update" >> update.log
    else
        echo "Updating: /opt/Boss/client/lib/BossME.jar" >> update.log
        cp $dirboss/client/lib/BossME.jar bak 2> fail
        cat fail >> update.log
        cp --remove-destination BossME.jar $dirboss/client/lib 2> fail
        cat fail >> update.log
    fi

    if [ `ls boss.ear | wc -l` = 0 ] ; then
        echo `date`": No boss.ear to update" >> update.log
    else
        echo "Updating: /opt/Boss/server/boss.ear" >> update.log
        cp $dirboss/server/boss.ear bak 2> fail
        cat fail >> update.log
        cp --remove-destination boss.ear $dirboss/server 2> fail
        cp --remove-destination boss.ear $dirboss/update/prepare/ear 2> fail
        cat fail >> update.log
        up=1
    fi

    rm fail
    cd /opt/Boss
    chmod a+x *.sh

    if [ "$up" = 1 ]; then
        cd /opt/Boss/server
        rm boss-*.ear
        rm boss*_*.ear

        /opt/Boss/wrapper/bin/jboss stop
        /opt/Boss/wrapper/bin/wildfly stop
        cd /opt/Boss/client/getdown/bin/
        cp --remove-destination $dirboss/client/getdown.txt $dirboss/client/getdown/bak
        ./update-getdown.sh

        sleep 30
        reboot
    fi
fi
