#!/bin/sh

# Create backup of previous kubeconfig
cp ~/.kube/config ~/.kube/config.bak

KUBECONFIG=""

for config in ~/.kube/config-*
do
    profile=$(echo $config | cut -d '-' -f 2)

    # De-duplicate profile names
    sed 's/default/'$profile'/g' $config > $config.tmp
    KUBECONFIG="$KUBECONFIG:$config.tmp"
done

KUBECONFIG=$(echo $KUBECONFIG | cut -c2-)

# Flatten configuration and write it to file
KUBECONFIG=$KUBECONFIG kubectl config view --flatten > ~/.kube/config

# Remove building blocks
rm ~/.kube/config-*.tmp
