#!/bin/bash

set -xe

APP_FOLDER=werefox-obs-layouts
CONTAINER_NAME=werefox-obs-layouts

MODE=$1
x=$(sudo docker container ls | grep next-js | wc -l)
if [ $x -eq 1 ]; then
    sudo MODE=$MODE docker-compose rm -sf
fi

sudo MODE=$MODE docker-compose build
sudo MODE=$MODE docker-compose run $CONTAINER_NAME bash -c "cd $APP_FOLDER && npm install --silent"

if [ ! -d "./src/$APP_FOLDER" ]; then
    sudo MODE=$MODE docker-compose run $CONTAINER_NAME npx create-next-app $APP_FOLDER
    sudo MODE=$MODE docker-compose run $CONTAINER_NAME bash -c "cd $APP_FOLDER && npx tailwindcss init -p"
    sudo chown -R $USER:$USER ./src
fi

sudo MODE=$MODE docker-compose run $CONTAINER_NAME bash -c "cd $APP_FOLDER && npx next telemetry disable"

if [ $MODE == "dev" ] || [ $MODE == "start" ]; then
    sudo MODE=$MODE docker-compose up --build --force-recreate --remove-orphans -d
    sudo MODE=$MODE docker-compose logs -f
    elif [ $MODE == "build" ]; then
    sudo MODE=$MODE docker-compose run $CONTAINER_NAME bash -c "cd $APP_FOLDER && npm run build"
else
    echo "Please use 'dev', 'build', or 'start' as an argument."
    exit 1
fi
