#!/bin/bash

set -xe

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

sudo MODE=$MODE docker-compose build

if [ ! -d "./src/info" ]; then
    sudo docker-compose run node npx create-next-app info
    sudo docker-compose run node npx tailwindcss init -p
    sudo chown -R $USER:$USER ./src
fi

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