#!/bin/bash

namespace=$1

get_install_path() {
    find_path=`find / -name "auto_sed_namespace.sh" -type f | awk -F "auto_sed_namespace.sh" '{print $1}'`
    install_path="${find_path%%/}"
    echo "${install_path}"
    cd "${install_path}"
    chmod -R +x scripts/*
}

get_install_path

sleep 5

# sed all namespace 加在此处
sed -i "s/edoc2/$NAMESPACE/g" edoc2_namespace.yaml
sed -i "s/namespace: edoc2/namespace: $NAMESPACE/g" Template/cluster/configmap.yaml
sed -i "s/namespace: edoc2/namespace: $NAMESPACE/g" Template/cluster/rbac/edoc2-ns-reader-rbac.yaml
sed -i "s/namespace: edoc2/namespace: $NAMESPACE/g" Template/cluster/secret/edoc2-secret.yaml
sed -i "s/namespace: edoc2/namespace: $NAMESPACE/g" Template/cluster/app/*.yaml
sed -i "s/namespace: edoc2/namespace: $NAMESPACE/g" Template/cluster/apm/*.yaml
sed -i "s/namespace: edoc2/namespace: $NAMESPACE/g" Template/cluster/middleware/minio/*.yaml
sed -i "s/namespace: edoc2/namespace: $NAMESPACE/g" Template/cluster/middleware/rabbitmq/*.yaml
sed -i "s/namespace: edoc2/namespace: $NAMESPACE/g" Template/cluster/middleware/elasticsearch/*.yaml
sed -i "s/namespace: edoc2/namespace: $NAMESPACE/g" Template/cluster/middleware/redis/*.yaml
sed -i "s/namespace: edoc2/namespace: $NAMESPACE/g" Template/cluster/middleware/stateful_mysql/*.yaml
sed -i "s/namespace: edoc2/namespace: $NAMESPACE/g" Template/cluster/middleware/haproxy/*.yaml

sed -i "s/.edoc2.svc/.${NAMESPACE}.svc/g" Template/cluster/middleware/minio/*.yaml
sed -i "s/.edoc2.svc/.${NAMESPACE}.svc/g" Template/cluster/middleware/rabbitmq/*.yaml
sed -i "s/.edoc2.svc/.${NAMESPACE}.svc/g" Template/cluster/middleware/elasticsearch/*.yaml
sed -i "s/.edoc2.svc/.${NAMESPACE}.svc/g" Template/cluster/middleware/redis/*.yaml
sed -i "s/.edoc2.svc/.${NAMESPACE}.svc/g" Template/cluster/middleware/stateful_mysql/*.yaml
sed -i "s/.edoc2.svc/.${NAMESPACE}.svc/g" Template/cluster/middleware/haproxy/*.yaml

# sed namespace此处
sed -i "s/edoc2/$NAMESPACE/g" Template/standalone/edoc2_namespace.yaml
sed -i "s/namespace: edoc2/namespace: $NAMESPACE/g" Template/standalone/middleware/*.yaml
sed -i "s/namespace: edoc2/namespace: $NAMESPACE/g" Template/standalone/app/*.yaml
sed -i "s/namespace: edoc2/namespace: $NAMESPACE/g" Template/standalone/apm/*.yaml
sed -i "s/namespace: edoc2/namespace: $NAMESPACE/g" Template/standalone/configmap.yaml
sed -i "s/.edoc2.svc/.${NAMESPACE}.svc/g" Template/standalone/configmap.yaml
sed -i "s/namespace: edoc2/namespace: $NAMESPACE/g" Template/standalone/rbac/edoc2-ns-reader-rbac.yaml
sed -i "s/namespace: edoc2/namespace: $NAMESPACE/g" Template/standalone/secret/edoc2-secret.yaml
