#!/bin/sh

yarn_install() {
    NPM_TOKEN=$(env | grep NPM_TOKEN | sed 's/^NPM_TOKEN=//')
    NPM_TOKEN=$NPM_TOKEN yarn install --frozen-lockfile
}

web() {
    sleep 20

    if [ ! -d "/src/node_modules" ]; then
        yarn_install
    fi

    bundle exec rake db:create
    bundle exec rake db:migrate
    bundle exec rails s -b 0.0.0.0
}

sidekiq() {
    sleep 20
    bundle exec sidekiq
}

case $1 in
    web)
        web
        ;;

    sidekiq)
        sidekiq
        ;;

    *)
        exec "$@"
        ;;
esac