#!/bin/bash
set -e

SHARE_ROOT='/usr/local/share'

#### TOOLS: CHECK_MY_GOODIES
CMG_NAME='check-mk-goodies'
CMG_GIT="https://github.com/Cellularhacker/${CMG_NAME}.git"
CMG_ROOT="${SHARE_ROOT}/tools"
CMG_HOME="${CMG_ROOT}/${CMG_NAME}"

### MARK: Check CMG_ROOT dir is exists or not.
if [ ! -d "${CMG_ROOT}" ]; then
	echo "Directory '${CMG_ROOT}' is not exists. Creating..." && \
	mkdir -p "${CMG_ROOT}"
fi

### MARK: Check that 'check-my-goodies' are already exists or not.
if [ -d "${CMG_HOME}" ]; then
	echo "Check and Update that '${CMG_NAME}' is having a new changes..." && \
	cd "${CMG_HOME}" && \
	git pull && \
	exit
fi

## For only it didn't cloned never.
cd "${CMG_ROOT}" && \
git clone "${CMG_GIT}" && \
echo '' >> /etc/profile 2>/dev/null && \
echo '### TOOLS: Check My Goodies' >> /etc/profile 2>/dev/null && \
echo "# ${CMG_GIT}" >> /etc/profile 2>/dev/null && \
echo 'export TOOLS_ROOT="/usr/local/share/tools"' >> /etc/profile 2>/dev/null && \
echo 'export PATH="${PATH}:${TOOLS_ROOT}"' >> /etc/profile 2>/dev/null && \
echo '' >> /etc/profile 2>/dev/null
