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

case "$1" in
  domains)
    [[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1
    [[ ! -d "$DOKKU_ROOT/$2" ]] && echo "App $2 does not exist" && exit 1
    APP="$2"

    dokku domains:setup $APP
    echo "=== $APP Domain Names"
    cat "$DOKKU_ROOT/$APP/VHOST"
    ;;

  domains:setup)
    [[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1
    [[ ! -d "$DOKKU_ROOT/$2" ]] && echo "App $2 does not exist" && exit 1
    APP="$2"; VHOST_PATH="$DOKKU_ROOT/$APP/VHOST"

    if [[ ! -f $VHOST_PATH ]]; then
      if [[ -f "$DOKKU_ROOT/VHOST" ]];then
        VHOST=$(< "$DOKKU_ROOT/VHOST")
      else
        VHOST=$(< "$DOKKU_ROOT/HOSTNAME")
      fi
      if [[ "$VHOST" =~ ([0-9]{1,3}[\.]){3}[0-9]{1,3} ]];then
        echo "ip found as hostname. disabling vhost support"
        [[ ! $(grep -q NO_VHOST "$DOKKU_ROOT/$APP/ENV") ]] && echo "export NO_VHOST='1'" >> "$DOKKU_ROOT/$APP/ENV"
      else
        echo "-----> Creating new $VHOST_PATH..."
        SUBDOMAIN=${APP/%\.${VHOST}/}
        hostname=$(: | pluginhook nginx-hostname $APP $SUBDOMAIN $VHOST)
        if [[ ! -n $hostname ]]; then
          if [[ "$APP" == *.* ]] && [[ "$SUBDOMAIN" == "$APP" ]]; then
            hostname="${APP/\//-}"
          else
            hostname="${APP/\//-}.$VHOST"
          fi
        fi

        echo "$hostname" > $VHOST_PATH
      fi
    fi
    ;;

  domains:add)
    [[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1
    [[ ! -d "$DOKKU_ROOT/$2" ]] && echo "App $2 does not exist" && exit 1
    APP="$2"

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

    if [[ $(egrep ^"$3"$ "$DOKKU_ROOT/$APP/VHOST" > /dev/null 2>&1; echo $?) -eq 0 ]]; then
      echo "$3 is already defined for $APP"
      exit 1
    fi

    dokku domains:setup $APP
    echo "$3" >> "$DOKKU_ROOT/$APP/VHOST"
    pluginhook post-domains-update $APP
    echo "-----> Added $3 to $APP"

    ;;

  domains:clear)
    [[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1
    [[ ! -d "$DOKKU_ROOT/$2" ]] && echo "App $2 does not exist" && exit 1
    APP="$2"

    rm -f "$DOKKU_ROOT/$APP/VHOST"
    dokku domains:setup $APP
    pluginhook post-domains-update $APP
    echo "-----> Cleared domains in $APP"

    ;;

  domains:remove)
    [[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1
    [[ ! -d "$DOKKU_ROOT/$2" ]] && echo "App $2 does not exist" && exit 1
    APP="$2"

    if [[ -z $3 ]]; then
      echo "Usage: dokku $1 $2 DOMAIN"
      echo "Must specify DOMAIN."
      exit 1
    fi

    dokku domains:setup $APP
    sed -i "/^$3$/d" "$DOKKU_ROOT/$APP/VHOST"
    pluginhook post-domains-update $APP
    echo "-----> Removed $3 from $APP"

    ;;

  help | domains:help)
    cat && cat<<EOF
    domains <app>                                   List custom domains for app
    domains:add <app> DOMAIN                        Add a custom domain to app
    domains:clear <app>                             Clear all custom domains for app
    domains:remove <app> DOMAIN                     Remove a custom domain from app
EOF
    ;;

  *)
    exit $DOKKU_NOT_IMPLEMENTED_EXIT
    ;;

esac
