#!/usr/bin/env bash

set -Eeuo pipefail

isRollback=false

parseParams() {
    while :; do
        case "${1-}" in
        --rollback) isRollback=true ;;
        -?*) ./lib/die.sh "Unknown option: $1" ;;
        *) break ;;
        esac
        shift
    done
}

runChecks() {
    ./lib/msg.sh "Checking permissions..."
    ./lib/check-permissions.sh "create CustomResourceDefinition"
    ./lib/check-permissions.sh "delete CustomResourceDefinition"
    ./lib/check-permissions.sh "create Namespace"
    ./lib/check-permissions.sh "delete Namespace"
    ./lib/check-permissions.sh "create ServiceAccount"
    ./lib/check-permissions.sh "delete ServiceAccount"
    ./lib/check-permissions.sh "create ClusterRole"
    ./lib/check-permissions.sh "patch ClusterRole"
    ./lib/check-permissions.sh "create ClusterRoleBinding"
    ./lib/check-permissions.sh "patch ClusterRoleBinding"
    ./lib/check-permissions.sh "create Role"
    ./lib/check-permissions.sh "patch Role"
    ./lib/check-permissions.sh "create RoleBinding"
    ./lib/check-permissions.sh "patch RoleBinding"
    ./lib/check-permissions.sh "create Service"
    ./lib/check-permissions.sh "patch Service"
    ./lib/check-permissions.sh "create Deployment"
    ./lib/check-permissions.sh "patch Deployment"
    ./lib/check-permissions.sh "create MutatingWebhookConfiguration"
    ./lib/check-permissions.sh "patch MutatingWebhookConfiguration"
    ./lib/check-permissions.sh "create ValidatingWebhookConfiguration"
    ./lib/check-permissions.sh "patch ValidatingWebhookConfiguration"
}

checkCurrentNginx() {
    nginxGetCommand="kubectl get deployment nginx-ingress-controller -n ingress-nginx -o yaml > lib/tmp/current-nginx-controller.yaml"
    ./lib/msg.sh "reading current nginx deployment: $nginxGetCommand"
    ./lib/msg.sh "backing up current nginx-ingress-controller at lib/tmp/current-nginx-controller.yaml"
    if ! eval "$nginxGetCommand"; then
        ./lib/die.sh "current nginx deployment not found"
    fi
}

nginxUpgrade() {
    upgradeCommand="kubectl apply -f nginx/ingress-nginx-update.yaml"
    ./lib/msg.sh "Upgrading nginx: $upgradeCommand"
    eval "$upgradeCommand"

    waitCommand="kubectl rollout status -n ingress-nginx deployment/nginx-ingress-controller --timeout=5m"
    ./lib/msg.sh "Waiting for the nginx-ingress-controller deployment to be ready: $waitCommand"
    eval "$waitCommand"
}

main() {
    parseParams "$@"

    ./lib/msg.sh ""
    ./lib/msg.sh "-------- Nginx upgrade ---------"

    if [[ $isRollback == true ]]; then
        ./lib/msg.sh "No automatic rollback available for downgrading the nginx-ingress-controller."
        return
    fi

    ./lib/msg.sh "This step upgrades nginx using the manifest provided in the installation guide."
    ./lib/msg.sh "Note that this step can not be rolled back."

    read -p "Upgrade nginx? [y/n] " -n 1 -r
    printf '\n'
    if [[ ! $REPLY =~ ^[Yy]$ ]]; then
        return
    fi

    runChecks
    checkCurrentNginx
    nginxUpgrade
    ./lib/msg.sh "Nginx upgrade completed."
}

main "$@"