#!/usr/bin/env bash
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x

# Check if name is specified
if [[ $1 == config ]] || [[ $1 == config:* ]]; then
  if [[ -z $2 ]]; then
    echo "You must specify an app name"
    exit 1
  else
    APP="$2"
    ENV_FILE="$DOKKU_ROOT/$APP/ENV"
    ENV_FILE_TEMP="$DOKKU_ROOT/$APP/ENV.tmp"

    # Check if app exists with the same name
    if [ ! -d "$DOKKU_ROOT/$APP" ]; then
      echo "App $APP does not exist"
      exit 1
    fi

    [ -f $ENV_FILE ] || {
      # echo "-----> Creating $ENV_FILE"
      touch $ENV_FILE
    }
  fi
fi

config_styled_hash () {
  sed 's/^\(.*\)=/\1:=/g' | column -t -s '='
}

config_shell_hash() {
  while read var
  do
    KEY=`echo "$var" | cut -d"=" -f1`
    VALUE=`echo "$var" | cut -d"=" -f2-`
    echo "export $(printf "%q" "$KEY")=$(printf "%q" "$VALUE")"
  done
}

config_write() {
  ENV_TEMP="$1"
  echo -e "$ENV_TEMP" | sed '/^$/d' | sort > "$ENV_FILE_TEMP"
  if ! cmp -s "$ENV_FILE" "$ENV_FILE_TEMP"; then
    cp -f "$ENV_FILE_TEMP" "$ENV_FILE"
    dokku rebuild "$APP"
  fi
  rm -f "$ENV_FILE_TEMP"
}

case "$1" in
  config)
    APP="$2"

    for var in "$@"; do
      if [[ "$var" == "--shell" ]]; then
        : | pluginhook env-vars "$APP" | config_shell_hash
        exit 0
      fi
    done

    echo "=== $APP config vars ==="
    : | pluginhook env-vars "$APP" | config_styled_hash
  ;;

  config:get)
    if [[ -z $3 ]]; then
      echo "Usage: dokku config:get APP KEY"
      echo "Must specify KEY."
      exit 1
    fi

    KEY="$3"

    : | pluginhook env-vars "$APP" | grep "^$KEY=" | cut -d"=" -f2-
  ;;

  config:set)
    if [[ -z "${*:3}" ]]; then
      echo "Usage: dokku config:set APP KEY1=VALUE1 [KEY2=VALUE2 ...]"
      echo "Must specify KEY and VALUE to set."
      exit 1
    fi

    APP="$2"; APP_DIR="$DOKKU_ROOT/$APP"
    ENV_ADD=""
    ENV_TEMP=`cat "${ENV_FILE}"`
    RESTART_APP=false

    shift 2

    for var in "$@"; do
      if [[ "$var" != *"="* ]]; then
        echo "Usage: dokku config:set APP KEY1=VALUE1 [KEY2=VALUE2 ...]"
        echo "Must specify KEY and VALUE to set."
        exit 1
      fi
    done

    for var in "$@"; do
      KEY=`echo "$var" | cut -d"=" -f1`
      VALUE=`echo "$var" | cut -d"=" -f2-`

      if [[ $KEY =~ [a-zA-Z_][a-zA-Z0-9_]* ]]; then
        RESTART_APP=true
        ENV_TEMP=$(echo -e "${ENV_TEMP}" | sed "/^export $KEY=/ d")
        ENV_TEMP="${ENV_TEMP}\nexport $KEY=$VALUE"
        ENV_ADD=$(echo -e "${ENV_ADD}" | sed "/^$KEY=/ d")
        ENV_ADD="${ENV_ADD}\n${KEY}=${VALUE}"
      fi
    done

    if [[ $RESTART_APP ]]; then
      echo "-----> Setting config vars and restarting $APP"
      echo -e "$ENV_ADD" | config_styled_hash

      config_write "$ENV_TEMP"
    fi
  ;;

  config:unset)
    if [[ -z "$3" ]]; then
      echo "Usage: dokku config:unset APP KEY1 [KEY2 ...]"
      echo "Must specify KEY to unset."
      exit 1
    fi

    APP="$2"; APP_DIR="$DOKKU_ROOT/$APP"
    ENV_TEMP=`cat "${ENV_FILE}"`
    RESTART_APP=false
    shift 2

    for var in "$@"; do
      echo "-----> Unsetting $var and restarting $APP"
      ENV_TEMP=$(echo -e "${ENV_TEMP}" | sed "/^export $var=/ d")
      RESTART_APP=true
    done

    config_write "$ENV_TEMP"
  ;;

  help|config:help)
    cat && cat<<EOF
    config <app>                                    Display the config vars for an app
    config:get <app> KEY                            Display a config value for an app
    config:set <app> KEY1=VALUE1 [KEY2=VALUE2 ...]  Set one or more config vars
    config:unset <app> KEY1 [KEY2 ...]              Unset one or more config vars
EOF
  ;;

  *)
    exit $DOKKU_NOT_IMPLEMENTED_EXIT
    ;;

esac

