#!/bin/bash

if [ "$XIVO_VERSION_INSTALLED" \< "2019.11.00" ]; then
    echo "Removing nginx packages"
    apt-get purge -y nginx nginx-full nginx-common
fi

if [ "$XIVO_VERSION_INSTALLED" \< "2019.12.00" ]; then
    echo "Removing xivo-web-interface old packages"
    apt-get purge -y xivo-web-interface
fi

if [[ ! -d /var/log/nginx ]]; then
    mkdir /var/log/nginx
fi

if [[ ! -d /var/log/xivo-web-interface ]]; then
    mkdir /var/log/xivo-web-interface
    touch /var/log/xivo-web-interface/xivo.log
    touch /var/log/xivo-web-interface/error.log
    chown -R www-data:www-data /var/log/xivo-web-interface/*
fi

if [ "$XIVO_VERSION_INSTALLED" \< "2019.10.00" ]; then
    exit 0
fi

# This ensures that the webi is correctly upgraded
echo "Removing webi docker volume"
xivo-dcomp stop webi nginx
xivo-dcomp rm -f webi nginx
docker volume rm xivo_webivol

