#!/usr/bin/with-contenv bash

# used https://github.com/linuxserver/docker-plex as a template

POPULATE_EXAMPLES=false

echo "-------------------------------------"
echo -e "Setting up config directory based on DATA_DIR env: ${DATA_DIR}\n"

# make config folder if it does not exist
if [ ! -d "${DATA_DIR}" ]; then
  echo "Directory does not exist! Creating..."
  POPULATE_EXAMPLES=true
  mkdir -p "${DATA_DIR}"
else
  if [ "$(ls -A ${DATA_DIR})" ]; then
       echo "Directory is not empty, not creating examples."
    else
      POPULATE_EXAMPLES=true
    fi
fi

# add example configs
if [ "$POPULATE_EXAMPLES" = true ]; then
  echo "Directory is empty, adding examples..."
  cp -r /app/config/. "${DATA_DIR}"/
fi

# permissions
echo "chown'ing directory to ensure correct permissions."
chown -R abc:abc "${DATA_DIR}"
echo "Done!"
echo -e "-------------------------------------\n"
