#!/usr/bin/env bash

function gitops_suite_app_install_help() {
  [[ -n "$1" ]] && echo_warning "$1"
  reset_colors
  cat << EOM
Usage:
  mas gitops_suite_app_install [options]
Where ${COLOR_YELLOW}specified${TEXT_RESET} each option may also be defined by setting the appropriate environment variable.
When no options are specified on the command line, interactive-mode will be enabled by default.

GitOps Configuration:
  -d, --dir ${COLOR_YELLOW}GITOPS_WORKING_DIR${TEXT_RESET}                         Directory for GitOps repository
  -a, --account-id ${COLOR_YELLOW}ACCOUNT_ID${TEXT_RESET}                          Account name that the cluster belongs to
  -c, --cluster-id ${COLOR_YELLOW}CLUSTER_ID${TEXT_RESET}                          Cluster ID

Maximo Application Suite:
  -m, --mas-instance-id ${COLOR_YELLOW}MAS_INSTANCE_ID${TEXT_RESET}                IBM Suite Maximo Application Suite Instance ID
  --mas-app-id ${COLOR_YELLOW}MAS_APP_ID${TEXT_RESET}                              IBM Suite Maximo Application Suite Application ID
  --mas-app-channel ${COLOR_YELLOW}MAS_APP_CHANNEL${TEXT_RESET}                    IBM Suite Maximo Application Suite Application Channel
  --mas-app-install-plan ${COLOR_YELLOW}MAS_APP_INSTALL_PLAN{TEXT_RESET}           IBM Suite Maximo Application Suite Application Install Plan ('Automatic' or 'Manual'. Default is 'Automatic')
  --mas-app-catalog-source ${COLOR_YELLOW}MAS_APP_CATALOG_SOURCE${TEXT_RESET}      IBM Suite Maximo Application Suite Application Catalog Source
  --mas-app-api-version ${COLOR_YELLOW}MAS_APP_API_VERSION${TEXT_RESET}            IBM Suite Maximo Application Suite Application API Version
  --mas-app-kind ${COLOR_YELLOW}MAS_APP_KIND${TEXT_RESET}                          IBM Suite Maximo Application Suite Application Kind
  --mas-app-spec-yaml ${COLOR_YELLOW}MAS_APP_SPEC_YAML${TEXT_RESET}                Yaml file location containing IBM Suite Maximo Application Suite Application Spec
  --mas-manual-cert-mgmt ${COLOR_YELLOW}MAS_MANUAL_CERT_MGMT${TEXT_RESET}          MAS Manual Cert Management
  --mas-edition ${COLOR_YELLOW}MAS_EDITION${TEXT_RESET}                            MAS EDITION
  --mas-manual-certs-yaml ${COLOR_YELLOW}MAS_MANUAL_CERTS_YAML${TEXT_RESET}        YAML file location containing combined MAS Manual Certs of core and all other apps
  --internal-cert-authority ${COLOR_YELLOW}INTERNAL_CERT_AUTHORITY${TEXT_RESET}    The certificate authority to use for provisoning certificates for all the apps

  Below is a sample yaml file template representing manual_certs dictionary ( key: << value base64 encoded content of cert file >>) 
  where key is <<app>>_<<filename . replaced by _>> name and value will be base64 encoded of the respective ca/tls file, 
  supported app are core, assist, iot, monitor, optimizer, predict,visualinspection
    manual_certs:
      core_tls_crt: <<base64 enc of core tls.crt>>
      core_tls_key: <<base64 enc of core tls.key>>
      core_ca_crt: <<base64 enc of core ca.crt>>
      assist_tls_crt: <<base64 enc of assist tls.crt>>
      assist_tls_key: <<base64 enc of assist tls.key>>
      assist_ca_crt: <<base64 enc of assist ca.crt>>
      <<app>>_tls_crt: <<base64 enc of <<app>> tls.crt>>
      <<app>>_tls_key: <<base64 enc of <<app>> tls.key>>
      <<app>>_ca_crt: <<base64 enc of <<app>> ca.crt>>

Visual Inspection:
  --gpu-request-quota ${COLOR_YELLOW}GPU_REQUEST_QUOTA${TEXT_RESET}              The gpu request quota to set for this VisualInspection App install

Secrets Manager:
      --secrets-path ${COLOR_YELLOW}SECRETS_PATH${TEXT_RESET}                    Secrets Manager path
      --secrets-key-seperator ${COLOR_YELLOW}SECRETS_KEY_SEPERATOR${TEXT_RESET}  Secrets Manager key seperator string

Automatic GitHub Push:
  -P, --github-push ${COLOR_YELLOW}GITHUB_PUSH${TEXT_RESET}        Enable automatic push to GitHub
  -H, --github-host ${COLOR_YELLOW}GITHUB_HOST${TEXT_RESET}        GitHub Hostname for your GitOps repository
  -O, --github-org  ${COLOR_YELLOW}GITHUB_ORG${TEXT_RESET}         Github org for your GitOps repository
  -R, --github-repo ${COLOR_YELLOW}GITHUB_REPO${TEXT_RESET}        Github repo for your GitOps repository
  -B, --git-branch ${COLOR_YELLOW}GIT_BRANCH${TEXT_RESET}          Git branch to commit to of your GitOps repository
  -M, --git-commit-msg ${COLOR_YELLOW}GIT_COMMIT_MSG${TEXT_RESET}  Git commit message to use when committing to of your GitOps repository
  -S , --github-ssh  ${COLOR_YELLOW}GIT_SSH${TEXT_RESET}           Git ssh key path

Other Commands:
  -h, --help                                      Show this help message
EOM
  [[ -n "$1" ]] && exit 1 || exit 0
}

function gitops_suite_app_install_noninteractive() {
  GITOPS_WORKING_DIR=$PWD/working-dir
  SECRETS_KEY_SEPERATOR="/"
  GIT_COMMIT_MSG="gitops-suite-app-install commit"

  # TODO: will need to add explicit args to pipeline when we start using this code to deploy to MCSP
  export REGION=${REGION:-${SM_AWS_REGION}}
  export CLUSTER_URL=${CLUSTER_URL:-"https://kubernetes.default.svc"} # use an in-cluster argocd worker (which fvtsaas does)

  export MAS_APP_INSTALL_PLAN=${MAS_APP_INSTALL_PLAN:-"Automatic"}

  while [[ $# -gt 0 ]]
  do
    key="$1"
    shift
    case $key in
      # GitOps Configuration
      -d|--dir)
        export GITOPS_WORKING_DIR=$1 && shift
        ;;
      -g|--gitops-version)
        echo "${COLOR_YELLOW}WARNING: the -g|--gitops-version parameter is deprecated; it has no effect and will be removed in a future release${COLOR_RESET}"
        shift
        ;;
      -a|--account-id)
        export ACCOUNT_ID=$1 && shift
        ;;
      -c|--cluster-id)
        export CLUSTER_ID=$1 && shift
        ;;
      -m|--mas-instance-id)
        export MAS_INSTANCE_ID=$1 && shift
        ;;
      -r|--region)
        export REGION=$1 && shift
        ;;
      -u|--cluster-url)
        export CLUSTER_URL=$1 && shift
        ;;
      --mas-manual-cert-mgmt)
        export MAS_MANUAL_CERT_MGMT=$1 && shift
        ;;
      --mas-edition)
        export MAS_EDITION=$1 && shift
        ;;
      --mas-manual-certs-yaml)
        export MAS_MANUAL_CERTS_YAML=$1 && shift
        ;;

      # Secrets Manager
      --secrets-path)
        export SECRETS_PATH=$1 && shift
        ;;
      --secrets-key-seperator)
        export SECRETS_KEY_SEPERATOR=$1 && shift
        ;;

      # MAS
      --mas-app-id)
        export MAS_APP_ID=$1 && shift
        ;;
      --mas-app-channel)
        export MAS_APP_CHANNEL=$1 && shift
        ;;
      --mas-app-install-plan)
        export MAS_APP_INSTALL_PLAN=$1 && shift
        ;;
      --mas-app-catalog-source)
        export MAS_APP_CATALOG_SOURCE=$1 && shift
        ;;
      --mas-app-api-version)
        export MAS_APP_API_VERSION=$1 && shift
        ;;
      --mas-app-kind)
        export MAS_APP_KIND=$1 && shift
        ;;
      --mas-app-spec-yaml)
        export MAS_APP_SPEC_YAML=$1 && shift
        ;;

      # MAS VisualInspection
      --gpu-request-quota)
        export GPU_REQUEST_QUOTA=$1 && shift
        ;;

      # Automatic GitHub Push
      -P|--github-push)
        export GITHUB_PUSH=true
        ;;
      -H|--github-host)
        export GITHUB_HOST=$1 && shift
        ;;
      -O|--github-org)
        export GITHUB_ORG=$1 && shift
        ;;
      -R|--github-repo)
        export GITHUB_REPO=$1 && shift
        ;;
      -B|--git-branch)
        export GIT_BRANCH=$1 && shift
        ;;
      -M|--git-commit-msg)
        export GIT_COMMIT_MSG=$1 && shift
        ;;
      -S|--github-ssh)
        export GIT_SSH=$1 && shift
        ;;

      --internal-cert-authority)
        export INTERNAL_CERT_AUTHORITY=$1 && shift
        ;;

      # Other Commands
      -h|--help)
        gitops_suite_app_install_help
        ;;
      *)
        # unknown option
        echo -e "${COLOR_RED}Usage Error: Unsupported option \"${key}\"${COLOR_RESET}\n"
        gitops_suite_app_install_help "Usage Error: Unsupported option \"${key}\" "
        exit 1
        ;;
      esac
  done

  [[ -z "$ACCOUNT_ID" ]] && gitops_suite_app_install_help "ACCOUNT_ID is not set"
  [[ -z "$CLUSTER_ID" ]] && gitops_suite_app_install_help "CLUSTER_ID is not set"
  [[ -z "$REGION" ]] && gitops_suite_app_install_help "REGION is not set"


  [[ -z "$MAS_INSTANCE_ID" ]] && gitops_suite_app_install_help "MAS_INSTANCE_ID is not set"
  [[ -z "$MAS_APP_ID" ]] && gitops_suite_app_install_help "MAS_APP_ID is not set"
  [[ -z "$MAS_APP_CHANNEL" ]] && gitops_suite_app_install_help "MAS_APP_CHANNEL is not set"
  [[ -z "$MAS_APP_CATALOG_SOURCE" ]] && gitops_suite_app_install_help "MAS_APP_CATALOG_SOURCE is not set"
  [[ -z "$MAS_APP_API_VERSION" ]] && gitops_suite_app_install_help "MAS_APP_API_VERSION is not set"
  [[ -z "$MAS_APP_KIND" ]] && gitops_suite_app_install_help "MAS_APP_KIND is not set"


  if [[ "$GITHUB_PUSH" == "true" ]]; then
    [[ -z "$GITHUB_HOST" ]] && gitops_suite_app_install_help "GITHUB_HOST is not set"
    [[ -z "$GITHUB_ORG" ]] && gitops_suite_app_install_help "GITHUB_ORG is not set"
    [[ -z "$GITHUB_REPO" ]] && gitops_suite_app_install_help "GITHUB_REPO is not set"
    [[ -z "$GIT_BRANCH" ]] && gitops_suite_app_install_help "GIT_BRANCH is not set"
  fi

  if [[ -z "$MAS_APP_SPEC_YAML" ]]; then
    if [[ "${MAS_APP_ID}" == "iot" ]]; then
      [[ "${DEFAULT_BLOCK_STORAGE_CLASS}" == "" ]] && gitops_suite_app_install_help "DEFAULT_BLOCK_STORAGE_CLASS is not set"
    fi
    if [[ "${MAS_APP_ID}" == "visualinspection" ]]; then
      [[ "${DEFAULT_FILE_STORAGE_CLASS}" == "" ]] && gitops_suite_app_install_help "DEFAULT_FILE_STORAGE_CLASS is not set"
    fi
  fi

}

function gitops_suite_app_install() {
  # Take the first parameter off (it will be create-gitops)
  shift
  if [[ $# -gt 0 ]]; then
    gitops_suite_app_install_noninteractive "$@"
  else
    echo "Not supported yet"
    exit 1
    gitops_suite_app_install_interactive
  fi

  # catch errors
  set -o pipefail
  trap 'echo "[ERROR] Error occurred at $BASH_SOURCE, line $LINENO, exited with $?"; exit 1' ERR

  mkdir -p ${GITOPS_WORKING_DIR}
  GITOPS_INSTANCE_DIR=${GITOPS_WORKING_DIR}/${GITHUB_REPO}/${ACCOUNT_ID}/${CLUSTER_ID}/${MAS_INSTANCE_ID}

  export MAS_APP_NAMESPACE="mas-${MAS_INSTANCE_ID}-${MAS_APP_ID}"

  CURRENT_DIR=$PWD
  TEMP_DIR=$CURRENT_DIR/tmp-suite-app-install
  rm -rf $TEMP_DIR
  mkdir -p $TEMP_DIR

  echo
  reset_colors
  echo_h2 "Review Settings"

  echo "${TEXT_DIM}"
  echo_h2 "Target" "    "
  echo_reset_dim "Account ID ............................ ${COLOR_MAGENTA}${ACCOUNT_ID}"
  echo_reset_dim "Region ................................ ${COLOR_MAGENTA}${REGION}"
  echo_reset_dim "Cluster ID ............................ ${COLOR_MAGENTA}${CLUSTER_ID}"
  echo_reset_dim "Instance Config Directory ............. ${COLOR_MAGENTA}${GITOPS_INSTANCE_DIR}"
  reset_colors

  echo "${TEXT_DIM}"
  echo_h2 "Secrets Manager" "    "
  echo_reset_dim "Secrets Path .......................... ${COLOR_MAGENTA}${SECRETS_PATH}"
  reset_colors

  echo "${TEXT_DIM}"
  if [[ "$GITHUB_PUSH" == "true" ]]; then
    echo_h2 "GitOps Target" "    "
    echo_reset_dim "Automatic Push ........................ ${COLOR_GREEN}Enabled"
    echo_reset_dim "Working Directory ..................... ${COLOR_MAGENTA}${GITOPS_WORKING_DIR}"
    echo_reset_dim "Host .................................. ${COLOR_MAGENTA}${GITHUB_HOST}"
    echo_reset_dim "Organization .......................... ${COLOR_MAGENTA}${GITHUB_ORG}"
    echo_reset_dim "Repository ............................ ${COLOR_MAGENTA}${GITHUB_REPO}"
    echo_reset_dim "Branch ................................ ${COLOR_MAGENTA}${GIT_BRANCH}"
  else
    echo_h2 "GitOps Target" "    "
    echo_reset_dim "Automatic Push ........................ ${COLOR_RED}Disabled"
    echo_reset_dim "Working Directory ..................... ${COLOR_MAGENTA}${GITOPS_WORKING_DIR}"
  fi
  reset_colors

  echo "${TEXT_DIM}"
  echo_h2 "IBM Maximo Application Suite" "    "
  echo_reset_dim "Instance ID ........................... ${COLOR_MAGENTA}${MAS_INSTANCE_ID}"
  echo_reset_dim "Application ID ........................ ${COLOR_MAGENTA}${MAS_APP_ID}"
  echo_reset_dim "Application Channel ................... ${COLOR_MAGENTA}${MAS_APP_CHANNEL}"
  echo_reset_dim "Application Install Plan .............. ${COLOR_MAGENTA}${MAS_APP_INSTALL_PLAN}"
  echo_reset_dim "Application Catalog Source ............ ${COLOR_MAGENTA}${MAS_APP_CATALOG_SOURCE}"
  echo_reset_dim "Application API Version ............... ${COLOR_MAGENTA}${MAS_APP_API_VERSION}"
  echo_reset_dim "Application Kind ...................... ${COLOR_MAGENTA}${MAS_APP_KIND}"
  echo_reset_dim "Application Spec Yaml file ............ ${COLOR_MAGENTA}${MAS_APP_SPEC_YAML}"
  echo_reset_dim "MAS Edition ........................... ${COLOR_MAGENTA}${MAS_EDITION}"
  
  if [[ -n "$MAS_APP_SPEC_YAML" ]] && [[ -s "$MAS_APP_SPEC_YAML" ]]; then
    echo_reset_dim "Using Default Application Spec ...... ${COLOR_MAGENTA}False"
  else
    echo_reset_dim "Using Default Application Spec ...... ${COLOR_MAGENTA}True"
  fi
  echo_reset_dim "Default File Storage Class ............ ${COLOR_MAGENTA}${DEFAULT_FILE_STORAGE_CLASS}"
  echo_reset_dim "Default Block Storage Class ........... ${COLOR_MAGENTA}${DEFAULT_BLOCK_STORAGE_CLASS}"
  echo_reset_dim "MAS Manual Cert Mgt ................... ${COLOR_MAGENTA}${MAS_MANUAL_CERT_MGMT}"
  echo_reset_dim "MAS Manual Certs YAML ................. ${COLOR_MAGENTA}${MAS_MANUAL_CERTS_YAML}"
  
  if [[ "${MAS_APP_ID}" == "visualinspection" ]]; then
    echo_reset_dim "GPU Request Quota ..................... ${COLOR_MAGENTA}${GPU_REQUEST_QUOTA}"
  fi
  
  reset_colors


  # Set up Suite secrets
  # ---------------------------------------------------------------------------
  echo
  echo_h2 "Configuring Suite secrets"
  AVP_TYPE=aws  # Support for IBM will be added later
  sm_login


  # Define cluster-level secrets used
  # ---------------------------------------------------------------------------
  # Note that this cluster-level secret is set up by gitops-cluster


  # Get the cluster-level secrets used
  # ---------------------------------------------------------------------------
  # Note that this cluster-level secret is set up by gitops-mongo
  export SECRET_KEY_IBM_ENTITLEMENT=${ACCOUNT_ID}${SECRETS_KEY_SEPERATOR}${CLUSTER_ID}${SECRETS_KEY_SEPERATOR}ibm_entitlement#image_pull_secret_b64




  # Instance-level secrets to use
  # ---------------------------------------------------------------------------


  # Instance-level secrets to create
  # ---------------------------------------------------------------------------
  if [ -z $GIT_SSH ]; then
    export GIT_SSH="false"
  fi


  # Set and Validate App Names
  # ---------------------------------------------------------------------------
  INSTANCE_ROOT_APP="instance.${CLUSTER_ID}.${MAS_INSTANCE_ID}"
  MASAPP_APP="${MAS_APP_ID}.${CLUSTER_ID}.${MAS_INSTANCE_ID}"

  validate_app_name "${INSTANCE_ROOT_APP}"
  validate_app_name "${MASAPP_APP}"




  # Clone github target repo
  # ---------------------------------------------------------------------------
  if [ "$GITHUB_PUSH" == "true" ]; then
    echo
    echo_h2 "Cloning GitHub repo $GITHUB_ORG $GITHUB_REPO"
    clone_target_git_repo $GITHUB_HOST $GITHUB_ORG $GITHUB_REPO $GIT_BRANCH $GITOPS_WORKING_DIR $GIT_SSH
  fi
  mkdir -p ${GITOPS_INSTANCE_DIR}

  # Getting app spec, either default or provided
  # ---------------------------------------------------------------------------
  if [[ -n "$MAS_APP_SPEC_YAML" ]] && [[ -s "$MAS_APP_SPEC_YAML" ]]; then
    echo
    echo_h2 "Using application spec provided for $MAS_APP_ID at $MAS_APP_SPEC_YAML"
    export MAS_APP_SPEC=$(cat ${MAS_APP_SPEC_YAML} | yq '.' --output-format yaml)
  else
    echo
    echo_h2 "Using default application spec for $MAS_APP_ID"
    jinjanate_commmon $CLI_DIR/templates/gitops/app-spec-defaults/${MAS_APP_ID}.yaml.j2 ${TEMP_DIR}/${MAS_APP_ID}-app-spec.yaml
    export MAS_APP_SPEC=$(cat ${TEMP_DIR}/${MAS_APP_ID}-app-spec.yaml | yq '.' --output-format yaml)
  fi
  if [[ -n "$INTERNAL_CERT_AUTHORITY" ]]; then
    export MAS_APP_SPEC=$(echo "$MAS_APP_SPEC" | yq '.mas_app_spec.internalCertificateAuthority = env(INTERNAL_CERT_AUTHORITY)' --output-format yaml)
  fi
  echo_reset_dim "MAS_APP_SPEC ............................ ${COLOR_MAGENTA}${MAS_APP_SPEC}"
  reset_colors



  # Look for generated(<type>) directives on storage class names
  # These are specified in different places in app spec yaml, so we need a special case for each
  # ---------------------------------------------------------------------------
  export STORAGE_CLASS_DEFINITIONS
  if [[ "${MAS_APP_ID}" == "visualinspection" ]]; then
    MVI_STORAGE_CLASS=$(echo "${MAS_APP_SPEC}" | yq '.mas_app_spec.settings.storage.storageClassName // ""')
    if [[ -n "${MVI_STORAGE_CLASS}" ]]; then
      STORAGE_CLASS_DEFINITIONS=$(generate_storage_class_def "${MVI_STORAGE_CLASS}" "${CLUSTER_ID}" "${MAS_INSTANCE_ID}" ${MAS_APP_ID} "main" "${STORAGE_CLASS_DEFINITIONS}") || exit 1
      MVI_STORAGE_CLASS=$(generate_storage_class_name "${MVI_STORAGE_CLASS}" "${CLUSTER_ID}" "${MAS_INSTANCE_ID}" ${MAS_APP_ID} "main" )
      MAS_APP_SPEC=$(echo "${MAS_APP_SPEC}" | yq '.mas_app_spec.settings.storage.storageClassName = "'${MVI_STORAGE_CLASS}'"')
    fi
  fi


  # Set certs only if manual cert is True (to create k8s secret in gitops) for app $MAS_APP_ID
  # ---------------------------------------------------------------------------
  if [ "${MAS_MANUAL_CERT_MGMT}" == "true" ] ; then
    if [[ -n "$MAS_MANUAL_CERTS_YAML" ]] && [[ -s "$MAS_MANUAL_CERTS_YAML" ]]; then

      declare -A tls_secret_name
      tls_secret_name['iot']="${MAS_INSTANCE_ID}-public-tls"
      tls_secret_name['add']="${MAS_INSTANCE_ID}-add-cert-public"
      tls_secret_name['assist']="public-assist-tls"
      tls_secret_name['monitor']="${MAS_INSTANCE_ID}-public-tls"
      tls_secret_name['optimizer']="${MAS_INSTANCE_ID}-cert-optimizer-public"
      tls_secret_name['predict']="${MAS_INSTANCE_ID}-public-predict-tls"
      tls_secret_name['visualinspection']="public-visualinspection-tls"

      if [[ -n "${tls_secret_name[$MAS_APP_ID]}" ]]; then
        echo
        echo_h2 "Read manual cert files for app ${MAS_APP_ID} from file ${MAS_MANUAL_CERTS_YAML}, and set to gitops template"

        tls_crt_filter_query=.manual_certs.${MAS_APP_ID}_tls_crt
        tls_key_filter_query=.manual_certs.${MAS_APP_ID}_tls_key
        ca_crt_filter_query=.manual_certs.${MAS_APP_ID}_ca_crt

        export PUBLIC_TLS_SECRET_NAME=${tls_secret_name[$MAS_APP_ID]}
        export TLS_CERT=$(yq eval "$tls_crt_filter_query" ${MAS_MANUAL_CERTS_YAML})
        export TLS_KEY=$(yq eval  "$tls_key_filter_query" ${MAS_MANUAL_CERTS_YAML})
        export CA_CERT=$(yq eval  "$ca_crt_filter_query"  ${MAS_MANUAL_CERTS_YAML})

        echo -e "\n - PUBLIC_TLS_SECRET_NAME ................ ${PUBLIC_TLS_SECRET_NAME}"
        echo -e "\n - CA_CERT CONTENT ....................... ${CA_CERT}"
        echo -e "\n - TLS_CERT CONTENT ...................... ${TLS_CERT}"
        echo -e "\n - TLS_KEY CONTENT ....................... ${TLS_KEY}"
      fi

    fi
  fi

  # Generate ArgoApps
  # ---------------------------------------------------------------------------
  echo
  echo_h2 "Generating Argo Project and Applications"

  echo "- IBM MAS Application Install"
  CONFIG_FILE=${GITOPS_INSTANCE_DIR}/ibm-mas-masapp-${MAS_APP_ID}-install.yaml
  jinjanate_commmon $CLI_DIR/templates/gitops/appset-configs/cluster/instance/masapp/ibm-mas-masapp-install.yaml.j2 ${CONFIG_FILE}


  echo_h2 "Generated configuration file (${CONFIG_FILE})"
  cat ${CONFIG_FILE}


  # Commit and push to github target repo
  # ---------------------------------------------------------------------------
  if [ "$GITHUB_PUSH" == "true" ]; then

    echo
    echo_h2 "Commit and push changes to GitHub repo $GITHUB_ORG $GITHUB_REPO"
    save_to_target_git_repo $GITHUB_HOST $GITHUB_ORG $GITHUB_REPO $GIT_BRANCH "${GITOPS_WORKING_DIR}/${GITHUB_REPO}" "${GIT_COMMIT_MSG}"
    remove_git_repo_clone $GITOPS_WORKING_DIR/$GITHUB_REPO
  fi

  rm -rf $TEMP_DIR

}
