#!/usr/bin/env bash

HTPASSWD_FILE="./htpass"
USERNAME="consoledeveloper-basic"
USERPASS="developer"
HTPASSWD_SECRET="htpasswd-consoledeveloper-basic-sec1"

OC_USERS_LIST="$(oc get users)"
if echo "${OC_USERS_LIST}" | grep -q "${USERNAME}"; then
    echo -e "\n\033[0;32m \xE2\x9C\x94 User consoledeveloper-basic already exists \033[0m\n"
    exit;
fi
htpasswd -cb $HTPASSWD_FILE $USERNAME $USERPASS

oc get secret $HTPASSWD_SECRET -n openshift-config &> /dev/null

oc create secret generic ${HTPASSWD_SECRET} --from-file=htpasswd=${HTPASSWD_FILE} -n openshift-config

oc apply -f - <<EOF
apiVersion: config.openshift.io/v1
kind: OAuth
metadata:
  name: cluster
spec:
  identityProviders:
  - name: consoledeveloper-basic
    challenge: true
    login: true
    mappingMethod: claim
    type: HTPasswd
    htpasswd:
      fileData:
        name: ${HTPASSWD_SECRET}
EOF

sleep 10s
oc create clusterrolebinding ${USERNAME}_role --clusterrole=basic-user --user=${USERNAME}
#!oc create clusterrolebinding ${USERNAME}_role2 --clusterrole=view --user=${USERNAME}
sleep 15s
echo -e "\n\e[1;35m User consoledeveloper-basic created with the password developer. Type the below\e[0m \n"
echo -e "\n\e[1;32m oc login -u\e[3m \e[1;36mconsoledeveloper-basic\e[0m \e[1;32m-p\e[3m \e[1;36mdeveloper\e[0m \n"
