#!/bin/bash

set -eu -o pipefail

readonly logfile="/home/yellowtent/platformdata/logs/box.log"

if [[ ${EUID} -ne 0 ]]; then
    echo "This script should be run as root." > /dev/stderr
    exit 1
fi

echo "This will re-create all the containers. Services will go down for a bit."

read -p "Do you want to proceed? (y/N) " -n 1 -r choice
echo

if [[ ! $choice =~ ^[Yy]$ ]]; then
    exit 1
fi

echo -n "Re-creating addon containers (this takes a while) ."
line_count=$(cat /home/yellowtent/platformdata/logs/box.log | wc -l)
sed -e 's/"version": ".*",/"version":"48.0.0",/' -i /home/yellowtent/platformdata/INFRA_VERSION
systemctl restart box

while ! tail -n "+${line_count}" "${logfile}" | grep -q "platform is ready"; do
    echo -n "."
    sleep 2
done

echo -e "\nDone.\nThe Cloudron dashboard will say 'Configuring (Queued)' for each app. The apps will come up in a short while."

