#!/bin/bash

install::description() {
    echo "Install Syndesis to a connected OpenShift cluster"
}

install::usage() {
    cat <<"EOT"
-s  --setup                   Install CRDs clusterwide. Use --grant if you want a specific user to be
                              able to install Syndesis. You have to run this option once as cluster admin.
-u  --grant <user>            Add permissions for the given user so that user can install the operator
                              in her projects. Must be run as cluster admin.
    --cluster                 Add the permission for all projects in the cluster
                              (only when used together with --grant)
-p  --project                 Install into this project. Delete this project
                              if it already exists. By default, install into the current project (without deleting)
    --operator-only           Only install the operator but no resource
                              connected cluster.
    --tag <tag>               Syndesis version/tag to install. If not given, then the latest
                              version from is installed
    --force                   Override an existing "Syndesis" if present
-w  --watch                   Wait until cluster is up
-f  --force-binary-download   By default if the binary cli is present in the expected path, it will
                              be used. With this option enabled, the binary will be removed and downloaded,
                              ensuring it is the latest version
-o  --open                    Open Syndesis in browser when installation is ready (implies --watch)
-y  --yes                     Assume 'yes' automatically when asking for deleting
                              a given project.
    --maven-mirror            Install Maven Mirror to be used with --maven-mirror when building.
    --custom-resource         Provide a custom resource to be installed by the operator
    --app-options             Operator parameters when installing the app.
                              Use quotes and start with a space before appending the options (example: " --addons jaeger").
EOT
}


install::run() {
    source "$(basedir)/commands/util/openshift_funcs"
    source "$(basedir)/commands/util/operator_funcs"

    release_tag="$(readopt --tag)"
    if [[ $(hasflag -f --force-binary-download) ]] || [[ -n "$release_tag" ]]; then
        if [[ -f ${OPERATOR_BINARY} ]]; then
            rm ${OPERATOR_BINARY}
        fi
    fi
    download_operator_binary || print_error_and_exit "unable to download the operator binary, exit"

    # syndesis-operator install --help prints the version in the --tag description
    operator_version=$($OPERATOR_BINARY install --help|grep '\-\-tag'|cut -d "\"" -f 2)
    echo "Syndesis version: ${operator_version}"

    local prep_only="false"
    if [[ $(hasflag -s --setup) ]]; then
        echo "Installing Syndesis CRD"
        result=$($OPERATOR_BINARY install cluster)
        check_error "$result"
        prep_only="true"
    fi

    local user_to_prepare="$(readopt -u --grant)"
    if [ -n  "$user_to_prepare" ]; then
        echo "Grant permission to create Syndesis to user $user_to_prepare"
        if [ $(hasflag --cluster) ]; then
            result=$($OPERATOR_BINARY grant --user $user_to_prepare --cluster)
        else
            result=$($OPERATOR_BINARY grant --user $user_to_prepare)

        fi
        check_error "$result"
        prep_only="true"
    fi

    if $prep_only; then
        return
    fi

    #Check the custom resource if one has been specified
    local customcr=$(readopt --custom-resource)
    if [ -n "${customcr}" ]; then
        if [ ! -f "${customcr}" ]; then
            check_error "ERROR: No custom resource exists at '${customcr}'"
        fi
    fi

    # If a project is given, create it new or recreate it
    local project=$(readopt --project -p)
    if [ -n "${project}" ]; then
        recreate_project $project "$(hasflag --yes -y)"
    fi

    # Setup oc
    setup_oc

    # Check for the proper setup
    set +e
    oc get syndesis >/dev/null 2>&1
    if [ $? -ne 0 ]; then
        check_error "ERROR: No CRD Syndesis installed or not enough permissions to read them. Please run --setup and/or --grant as cluster-admin. See 'syndesis install --help' for more information."
    fi

    set -e

    if [ $(hasflag --maven-mirror) ]; then
        install_maven_mirror
    fi

    # Deploy operator and wait until its up
    echo "Deploying Syndesis operator"
    result=$($OPERATOR_BINARY install operator)
    check_error "$result"

    wait_for_deployments 1 syndesis-operator

    local app_options=$(readopt --app-options)
    if [ $(hasflag --operator-only) ]; then
        echo "Deployed operator."
        exit 0
    else
        # Deploying syndesis app
        local customcr=$(readopt --custom-resource)

        if [ -n "${customcr}" ]; then
            echo "Deploying syndesis app with custom resource ${customcr}"
            app_options=$app_options" --custom-resource ${customcr}"
        else
            echo "Deploying syndesis app."
        fi

        result=$($OPERATOR_BINARY install app $app_options)
        check_error "$result"
    fi

    echo "Install finished."
    if [ $(hasflag --watch -w) ] || [ $(hasflag --open -o) ]; then
        wait_for_deployments 1 syndesis-server syndesis-ui syndesis-meta
    fi

    if [ $(hasflag --open -o) ]; then
        while ! (oc get routes | grep syndesis >/dev/null 2>&1); do
            echo "Sleeping 5s ..."
            sleep 5
        done
        open_url "https://$(oc get routes syndesis --template "{{.spec.host}}")"
    fi
}
