#!/bin/sh
# Helper to start Home Assistant Core inside the devcontainer

# Stop on errors
set -e

WD="${WORKSPACE_DIRECTORY:=/workspaces/frontend}"

if [ -z "${DEVCONTAINER}" ]; then
  echo "This task should only run inside a devcontainer, for local install HA Core in a venv."
  exit 1
fi

if [ -z $(which hass) ]; then
  echo "Installing Home Asstant core from dev."
  python3 -m pip install --upgrade \
    colorlog \
    git+https://github.com/home-assistant/home-assistant.git@dev
fi

if [ ! -d "${WD}/config" ]; then
  echo "Creating default configuration."
  mkdir -p "${WD}/config";
  hass --script ensure_config -c config
  echo "demo:

logger:
  default: info
  logs:
    homeassistant.components.frontend: debug
" >> "${WD}/config/configuration.yaml"

  if [ ! -z "${HASSIO}" ]; then
  echo "
# frontend:
#   development_repo: ${WD}

hassio:
  development_repo: ${WD}" >> "${WD}/config/configuration.yaml"
  else
  echo "
frontend:
  development_repo: ${WD}

# hassio:
#   development_repo: ${WD}" >> "${WD}/config/configuration.yaml"
  fi

  if [ ! -z "${CODESPACES}" ]; then
  echo "
http:
  use_x_forwarded_for: true
  trusted_proxies:
    - 127.0.0.1
" >> "${WD}/config/configuration.yaml"
  fi
fi

hass -c "${WD}/config"
