#!/usr/bin/env bash

source "$(dirname $0)/../dokku_common"

case "$1" in
  apps)
    echo "=== My Apps"
    find $DOKKU_ROOT -maxdepth 1 -type d  \( ! -iname ".*" \)  -not -path $DOKKU_ROOT/tls | sed 's|^\./||g' | sed 's|'$DOKKU_ROOT'\/||' | tail -n +2 | sort
    ;;

  apps:list|list)
    verify_max_args 1 "$@"
    for app in $(ls -d $DOKKU_ROOT/*/ 2>/dev/null); do
      if [[ -f "$app/refs/heads/master" ]]; then
        basename "$app"
      fi
    done
    ;;

  apps:status|status)
    verify_app_name "$2"
    verify_max_args 2 "$@"

  	if docker port "$APP_NAME" "$APP_PORT" 1>/dev/null 2>/dev/null
  	then
  		echo "$APP is running."
  	else
  		echo "$APP is stopped."
  	fi
	;;

  apps:start|start)
    verify_app_name "$2"
    verify_max_args 2 "$@"
    
    docker start "$APP_NAME"
	;;

  apps:stop|stop)
    verify_app_name "$2"
    verify_max_args 2 "$@"
    
    docker stop "$APP_NAME"
	;;

  apps:disable|disable)
    verify_app_name "$2"
    verify_max_args 2 "$@"
    if [[ ! -f "$APP_DIR/DISABLED" ]]; then
      touch "$APP_DIR/DISABLED"
      deploy_app "$APP"
    else
      echo "$APP: Application is already disabled"
    fi
  ;;

  apps:enable|enable)
    verify_app_name "$2"
    verify_max_args 2 "$@"
    if [[ -f "$APP_DIR/DISABLED" ]]; then
      rm -f "$APP_DIR/DISABLED"
      deploy_app "$APP"
    else
      echo "$APP: Application is already enabled"
    fi
  ;;

  apps:restart|restart)
    verify_app_name "$2"
    verify_max_args 2 "$@"
    docker restart "$APP_NAME"
    ;;

  apps:top|top)
    verify_app_name "$2"
    verify_max_args 2 "$@"
    
    shift 2
    docker top "$APP_NAME"
    ;;

  apps:destroy|delete)
    verify_app_name "$2"

    if [[ ! -d "$APP_DIR" ]]; then
        echo "App does not exist"
        exit 1
    fi

    [[ "$APP" == "tls" ]] && fail "Unable to destroy tls directory"
    [[ "$APP" == "ssl" ]] && fail "Unable to destroy tls directory"

    info "Deleting application $APP..."
    pluginhook pre-delete $APP

    stop_and_remove_app_containers
    stop_and_remove_container $APP_PERSISTENT_NAMES
    remove_image "$IMAGE"

    pluginhook post-delete $APP
    info "Application deleted: $APP"
    ;;

  apps:create)
    

  help)
    cat && cat<<EOF
    list                 List app
    status <app>         Status of specific app
    start <app>	        Stop specific app
    stop <app>		        Stop specific app
    restart <app>        Restart specific app (not-redeploy)
    enable <app>         Re-enable specific app
    disable <app>        Disable specific app
    top <app> [args...]  Show running processes
EOF
    ;;

  *)
    exit $DOKKU_NOT_IMPLEMENTED_EXIT
    ;;
esac
