#!/bin/sh

setEnv() {
  user=$1
  USERDIR="/home/$user"
  if [ ! -d $USERDIR ]
  then
    echo "$(gettext'User') $user $(gettext 'missing from the system')" >&2
    echo "Пользователь $user отсутствует в системе" >&2
    exit 1
  fi
  export KUBECONFIGDIR=$USERDIR/.kube
  export KUBECONFIGFILE="$KUBECONFIGDIR/config"
}

setUserKubeconfigAccess() {
  if ! id -nG | grep wheel >/dev/null 2>&1
  then
    echo "$(gettext 'User') $gettext('not included in the group') wheel" >&2
    exit 1
  fi
  slist=; n=1; while [ $n -le 64 ]; do slist+=" $n"; let n=$n+1; done
  trap "while ! su - -c 'chmod 700 $KUBECONFIGDIR'; do :; done" $slist
  if [ "$(id -u)" -ne 0 ]
  then
    echo "$(gettext 'Enter password of user') root" >&2
  fi
  while ! su - -c "chmod 770 $KUBECONFIGDIR"; do :; done
}

unsetUserKubeconfigAccess() {
  if ! id -nG | grep wheel >/dev/null 2>&1
  then
    echo "$(gettext 'User') $gettext('not included in the group') wheel" >&2
    exit 1
  fi
  user=$1
  if [ "$(id -u)" -ne 0 ]
  then
    echo "$(gettext 'Enter password of user') root" >&2
  fi
  while ! su - -c "chown -R $user:k8s $KUBECONFIGDIR;chmod 700 $KUBECONFIGDIR"; do :; done
}

getClusterIP() {
  clusterURL=$(kubectl config  view -o json | jq '.clusters[0].cluster.server')
  if [ -z "$clusterURL" ]
  then
    echo "$(gettext 'Cluster is unavailable') ($(gettext 'configuration file') $KUBECONFIGFILE)" >&2
    exit 1
  fi
  clusterURL=${clusterURL:1:-1}
  ifs=$IFS IFS=:/
  set -- $clusterURL
  IFS=$ifs
  shift;while [ $# -gt 0 -a -z "$1" ]; do shift; done
  echo $1
}

getClusterName() {
  clusterName=$(kubectl config  view -o json | jq '.clusters[0].name')
  if [ -z "$clusterName" ]
  then
    echo "$(gettext 'Cluster is unavailable') ($(gettext 'configuration file') $KUBECONFIGFILE)" >&2
    exit 1
  fi
  echo $clusterName
}
