#!/bin/bash
# Info: Script to install/create/delete/update rook-ceph from file settings 
# Author: JesusPerezLorenzo  
# Release: 1.0 
# Date: 15-12-2023

USAGE="install-rook-ceph.sh full-path-settings-file [ -m controlplane (hostname -cp-) | worker]  [*install | update | makejoin | remove | fullremove]"

[ "$1" == "-h"  ] && echo "$USAGE"  && exit 1

[[ "$1" == *setting* ]] && [ -r  "$1" ]  && . $1 && shift 
[[ "$1" == env-* ]] && [ -r  "$1" ]  && . $1 && shift 
[ -r "env-rook-ceph" ] && . env-rook-ceph


has_rook_operator=$(kubectl get pods -n ${NAMESPACE} 2>/dev/null | grep operator)

INSTALL_NAME="root-cepth"

if [ ! -d "rook-ceph" ] ; then 
   echo "Error: rook-cepth path not found"
   exit 1
fi

_save_target() {
   [ -z "$TARGET_SAVE_PATH" ]  && return
   local file_path=$1
   mkdir -p "$TARGET_SAVE_PATH"
   if cp "$file_path" "$TARGET_SAVE_PATH" ; then 
      echo "$file_path saved in $TARGET_SAVE_PATH"
   fi
}
_kubectl() {
   local mode=$1
   local yaml=$2
   [ ! -r "$yaml" ]  && return
   case $mode in 
      "create") if ! kubectl create -f "$yaml" ; then
                  echo "Error: $INSTALL_NAME $yaml"
               fi
               ;;
      "apply") if ! kubectl apply -f "$yaml" ; then 
                  echo "Error: $INSTALL_NAME $yaml"
               fi
               ;;
   esac
   _save_target "$yaml"
}

cd rook-ceph || exit 1

_kubectl create crds.yaml
_kubectl apply common.yaml
_kubectl apply operator.yaml
_kubectl apply cluster.yaml
_kubectl apply object.yaml
_kubectl apply object-user.yaml
_kubectl apply pool.yaml 
_kubectl apply storageclass-csi.yaml
_kubectl apply storageclass-rdb.yaml 
_kubectl apply filesystem.yaml
_kubectl apply rgw-external.yaml
_kubectl apply dashboard-external-https.yaml
#_kubectl apply nfs.yaml
_kubectl apply toolbox.yaml
