#!/bin/bash
ROOT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )/.." && pwd )
set -e

# If we're executing from /usr then we should update the root directory
# to our default installation path and we should be allowed to sudo.
if [ $ROOT_DIR == "/usr" ]; then
  SHOULD_SUDO="yes"
  ROOT_DIR=/opt/postal/app
else
  SHOULD_SUDO="no"
fi

# If we we can sudo and the current user isn't postal, then automatically
# run commands as the postal user
if [ $SHOULD_SUDO == "yes" ] && [ $USER != "postal" ]; then
    run() {
        HOME=/opt/postal
        eval "sudo -u postal $@"
    }
else
    run() {
        eval $@
    }
fi

# Enter the root directory
cd $ROOT_DIR

# Run the right command
case "$1" in
    start)
        run "bundle exec ruby script/update_process_concurrency.rb"
        run "procodile start"
        ;;

    stop)
        run "procodile stop -s --wait"
        ;;

    restart)
        run "bundle exec ruby script/update_process_concurrency.rb"
        run "procodile restart"
        ;;

    status)
        run "procodile status"
        ;;

    run)
        run "bundle exec ruby script/update_process_concurrency.rb"
        run "LOG_TO_STDOUT=1 procodile start -f --no-respawn --stop-when-none"
        ;;

    upgrade)
        if [ ! -f "public/assets/.prebuilt" ]; then
            echo 'Compiling Assets'
            run "bundle exec rake assets:precompile"
        fi
        echo 'Migrating database'
        run "bundle exec rake db:migrate"
        ;;

    console)
        run "bundle exec rails console"
        ;;

    initialize-config)
        run "bundle exec ruby script/generate_initial_config.rb"
        ;;

    initialize)
        echo 'Initializing database'
        run "bundle exec rake db:schema:load db:seed"
        if [ ! -f "public/assets/.prebuilt" ]; then
            echo 'Compiling Assets'
            run "bundle exec rake assets:precompile"
        fi
        ;;

    default-dkim-record)
        run "bundle exec ruby script/default_dkim_record.rb"
        ;;

    register-lets-encrypt)
        run "bundle exec ruby script/register_lets_encrypt.rb $2"
        ;;

    auto-upgrade)
        run "ruby script/auto_upgrade.rb $@"
        ;;

    make-user)
        run "bundle exec ruby script/make_user.rb"
        ;;

    test-app-smtp)
        run "bundle exec ruby script/test_app_smtp.rb $2"
        ;;

    bundle)
        if [ -n "$2" ]; then
          run "bundle install --path=$2 --without=development --jobs=4 --clean"
        else
          echo "usage: $0 bundle path/to/vendor/dir"
          exit 1
        fi
        ;;

    version)
        run "bundle exec ruby script/version.rb"
        ;;

    *)
        echo $"Usage: $0 [command]"
        echo
        echo "Frequently used commands:"
        echo -e " * \e[35mstart|stop|restart\e[0m - start/stop/restart Postal in background"
        echo -e " * \e[35mstatus\e[0m - show the current status of Postal"
        echo -e " * \e[35mrun\e[0m - run Postal in the foreground"
        echo -e " * \e[35mversion\e[0m - show the current Postal version"
        echo
        echo "Other commands:"
        echo -e " * \e[35mconsole\e[0m - open a Postal system console (debug only)"
        echo -e " * \e[35mauto-upgrade\e[0m - upgrade to the latest version of Postal"
        echo -e " * \e[35minitialize-config\e[0m - initialize a new config directory"
        echo -e " * \e[35minitialize\e[0m - generate assets and load the database for the first time"
        echo -e " * \e[35mdefault-dkim-record\e[0m - show the default DKIM DNS record"
        echo -e " * \e[35mregister-lets-encrypt\e[0m - register the generated Let's Encrypt key"
        echo -e " * \e[35mupgrade\e[0m - upgrade the Postal installation"
        echo -e " * \e[35mbundle\e[0m - download & install all required Ruby dependencies"
        echo -e " * \e[35mmake-user\e[0m - create a new admin user"
        echo
esac
