#!/bin/sh

mkdir -p ~/.n8n/nodes
if [ -n "$NPM_PACKAGES" ]; then
  cd ~/.n8n/nodes
  npm install $NPM_PACKAGES
  cd -
fi

if [ -n "$CUSTOM_UI" ]; then
  # extract /tmp/dist.tgz to /usr/local/lib/node_modules/n8n/packages/dits
  rm -rf /usr/local/lib/node_modules/n8n/packages/dits
  tar -xzf /tmp/dist.tgz -C /usr/local/lib/node_modules/n8n/packages
  rm /tmp/dist.tgz
fi

if [ "$#" -gt 0 ]; then
  # Got started with arguments
  exec n8n "$@"
else
  # Got started without arguments
  exec n8n
fi