#!/bin/bash

set -xe

CONTAINER_NAME=letter-werefox-cafe

MODE=$1
x=$(MODE=$MODE docker compose ps | grep $CONTAINER_NAME | wc -l)

if [ "$MODE" == "" ] || [ "$MODE" != "build" && "$MODE" != "dev" && "$MODE" != "start" ]; then
    echo "Please use 'dev', 'build', or 'start' as an argument."
    exit 1
fi

if [ $x -eq 1 ]; then
    MODE=$MODE docker compose rm -sf
fi

#if [ ! -d "$(pwd)/src/$APP_FOLDER" ]; then
#    MODE=$MODE docker compose run $CONTAINER_NAME sh -c "npm install --silent create-next-app && \
#        npx create-next-app /usr/src/app/$APP_FOLDER --ts --eslint && \
#        cd $APP_FOLDER && npm install tailwindcss autoprefixer &&  npx tailwindcss init -p && \
#        cd /usr/src/app && rm -rf node_modules package.json package-lock.json"
#fi


MODE=$MODE docker compose run $CONTAINER_NAME sh -c "npm install --silent"
MODE=$MODE docker compose run $CONTAINER_NAME sh -c "npx next telemetry disable"

if [ "$MODE" == "build" ]; then
    MODE=$MODE docker compose run $CONTAINER_NAME sh -c "npm run build && npm run export"
elif [ "$MODE" == "dev" ] || [ "$MODE" == "start" ]; then
    MODE=$MODE docker compose up -d
    MODE=$MODE docker compose logs -f
fi
