#!/bin/bash
set -e

echo "Launching OCS Inventory Backend post-installation script"

# if there is a backup directory, restore the configuration, else cp the configuration
if [ -d "/usr/share/ocsinventory-backend-backup" ]; then
    UPDATE=1
else
    UPDATE=0
fi

# venv and requirements
if [ ! -d "/usr/lib/ocsinventory-backend/venv" ]; then
    # generating secret for Django 
    echo "Generating Django secret key..."
    SECRET_KEY=$(python3 -c "import secrets; print(secrets.token_urlsafe(50))")
    sed -i "s/SECRET_KEY=.*/SECRET_KEY='${SECRET_KEY}'/" /usr/share/ocsinventory-backend/.env
    echo "Creating virtual environment..."
    python3 -m venv /usr/lib/ocsinventory-backend/venv
fi

echo "Activating virtual environment ..."
source /usr/lib/ocsinventory-backend/venv/bin/activate
echo "Installing requirements ..."
pip3 install -r /usr/share/ocsinventory-backend/requirements.txt

# Check if update
if [ $UPDATE -eq 1 ]; then
    echo "OCS Inventory Backend update detected"
    cp /usr/share/ocsinventory-backend-backup/.env /usr/share/ocsinventory-backend/.env
    echo "Running database migrations..."
    python3 /usr/share/ocsinventory-backend/manage.py migrate
fi

deactivate

if [ $UPDATE -eq 1 ]; then
    echo "Restoring Nginx and UWSGI configuration ..."
    # restore UWSGI configuration
    if [ -f /usr/share/ocsinventory-backend-backup/ocsinventory-backend.ini ]; then
        cp /usr/share/ocsinventory-backend-backup/ocsinventory-backend.ini /etc/uwsgi/apps-available/ocsinventory-backend.ini
    fi

    # restore NGINX configuration
    if [ -f /usr/share/ocsinventory-backend-backup/ocsinventory-backend ]; then
        cp /usr/share/ocsinventory-backend-backup/ocsinventory-backend /etc/nginx/sites-available/ocsinventory-backend
    fi
fi

if [ $UPDATE -eq 0 ]; then
    # enable NGINX configuration
    ln -s /etc/nginx/sites-available/ocsinventory-backend /etc/nginx/sites-enabled/
    # enable UWSGI configuration
    ln -s /etc/uwsgi/apps-available/ocsinventory-backend.ini /etc/uwsgi/apps-enabled/
fi

# change ownership and permissions on logs directory
chown -R www-data:www-data /usr/share/ocsinventory-backend/
chmod -R 755 /usr/share/ocsinventory-backend/logs

# restart services
echo "Restarting UWSGI and Nginx services..."
systemctl restart uwsgi
systemctl restart nginx

echo "OCS Inventory Backend successfully installed."

if [ $UPDATE -eq 0 ]; then
    echo "================================================================================================================================="
    echo "=                                                                                                                               ="
    echo "= Please run the script '/usr/share/ocsinventory-backend/tools/configure-ocsinventory-backend.sh' to configure the application. ="
    echo "=                                                                                                                               ="
    echo "================================================================================================================================="
else
    rm -rf /usr/share/ocsinventory-backend-backup
fi
