#!/bin/bash

if [ -f .env ]; then
  source .env

  mkdir -p "$DIFY_ROOT_PATH"

  if [ -d "conf" ]; then
      find conf -type f | while read -r file; do
        dest="$DIFY_ROOT_PATH/${file#conf/}"
        if [ ! -e "$dest" ]; then
          mkdir -p "$(dirname "$dest")"
          cp "$file" "$dest"
        fi
      done
      echo "Conf files copied to $DIFY_ROOT_PATH."
    else
      echo "Warning: conf directory not found."
    fi

  env_source="envs/dify.env"
  if [ -f "$env_source" ]; then
    while IFS='=' read -r key value; do
      if [[ -z "$key" || "$key" =~ ^# ]]; then
        continue
      fi
      if ! grep -q "^$key=" .env; then
        echo "$key=$value" >> .env
      fi
    done < "$env_source"
  fi

  echo "Check Finish."

else
  echo "Error: .env file not found."
fi
