
#----------------------------------------------------------------------------------------------

platform_os() {
	case "$OSTYPE" in
		linux*)   echo "linux" ;;
		solaris*) echo "solaris" ;;
		darwin*)  echo "macos" ;;
		bsd*)     echo "bsd" ;;
		msys*)    echo "windows" ;;
		*)        echo "?" ;;
	esac
}

platform_arch() {
	case "`uname -m`" in
		x86_64*)  echo "x64" ;;
		amd64*)   echo "x64" ;;
		i386*)    echo "x86" ;;
		i686*)    echo "x86" ;;
		aarch64*) echo "arm64v8" ;;
		armv7hl*) echo "arm32v7" ;;
		armv7l*)  echo "arm32v7" ;;
		armv6l*)  echo "arm32v6" ;;
		ppc64*)   echo "ppc" ;;
		s390*)    echo "s390_31" ;;
		s390x*)   echo "s390" ;;
		*)        echo "?" ;;
	esac
}

#----------------------------------------------------------------------------------------------

if [[ $(platform_os) == macos ]]; then

realpath() {
	[[ $1 = /* ]] && echo "$1" || echo "$PWD/${1#./}"
}

fi

#----------------------------------------------------------------------------------------------
#

runn() {
	if [[ $1 == @ ]]; then
		cmd="$(cat $2)"
		(( NOP == 1 || V >= 1 )) && { echo "$cmd"; }
		[[ $NOP == 1 ]] && { return; }
		if (( V < 2 )); then
			__runn_log=$(mktemp)
			{ eval "$cmd" > $__runn_log 2>&1 ; E=$?; } || true
			[[ $E != 0 && $LOG != 0 ]] && cat $__runn_log
			rm -f $__runn_log
			return $E
		else
			eval "$cmd"
		fi
	else
		(( NOP == 1 || V >= 1 )) && { echo "${@:1}"; }
		[[ $NOP == 1 ]] && { return; }
		if (( V < 2 )); then
			__runn_log=$(mktemp)
			{ eval "${@:1}" > $__runn_log 2>&1 ; E=$?; } || true
			[[ $E != 0 && $LOG != 0 ]] && cat $__runn_log
			rm -f $__runn_log
			return $E
		else
			eval "${@:1}"
		fi
	fi
}

#----------------------------------------------------------------------------------------------

if [[ $NO_ANSI != 1 ]]; then
	RED=$'\033[0;31m'
	GREEN=$'\033[0;32m'
	ORANGE=$'\033[0;33m'
	BLUE=$'\033[0;34m'
	PURPLE=$'\033[0;35m'
	CYAN=$'\033[0;36m'
	LIGHTGRAY=$'\033[0;37m'
	DARKGRAY=$'\033[1;30m'
	LIGHTRED=$'\033[1;31m'
	LIGHTGREEN=$'\033[1;32m'
	YELLOW=$'\033[1;33m'
	LIGHTBLUE=$'\033[1;34m'
	LIGHTPURPLE=$'\033[1;35m'
	LIGHTCYAN=$'\033[1;36m'
	WHITE=$'\033[1;37m'
	NOCOLOR=$'\033[0m'

# 	export BOLD=$(tput bold)
# 	export NOCOLOR=$(tput sgr0)
# 	export RED=$(tput setaf 1)
# 	export GREEN=$(tput setaf 2)
# 	export BRED=$(tput bold; tput setaf 1)
# 	export BGREEN=$(tput bold; tput setaf 2)
fi

#----------------------------------------------------------------------------------------------

eprint() {
	if [[ -n $PROGNAME ]]; then
		local pref="$(basename "$PROGNAME")"
	else
		local pref="Error"
	fi

	if [[ -t 2 ]]; then
		>&2 echo "${RED}${pref}: $@${NOCOLOR}"
	else
		>&2 echo "error: $@"
	fi
}

#----------------------------------------------------------------------------------------------

fixterm() {
	reset
	stty sane
	tput rs1
	clear
	echo -e "\033c\033[0m"
}

#----------------------------------------------------------------------------------------------

is_command() {
        command -v $1 &> /dev/null
}

#----------------------------------------------------------------------------------------------

get_profile_d() {
	local d
	if [[ `uname -s` == Darwin ]]; then
		d="$HOME/.profile.d"
	else
		d="/etc/profile.d"
	fi
	if [[ ! -d $d ]]; then
		mkdir -p "$d"
	fi
	echo "$d"
}

setup_profile_d() {
	local d
	local profile
	local sudo
	if [[ `uname -s` == Darwin ]]; then
		d="$HOME/.profile.d"
		profile="$HOME/.bash_profile"
		[[ ! -f $profile ]] && touch $profile
		sudo=
	else
		d="/etc/profile.d"
		profile="/etc/profile"
		sudo="$SUDO"
	fi
	if [[ ! -d $d ]]; then
		$sudo mkdir -p "$d"
	fi
	if ! grep -q 'profile.d' $profile 2>/dev/null; then
		tmp_profile=$(mktemp /tmp/profiled.XXXXXX)
		cp $profile $tmp_profile
		printf "\nif [ -d $d ]; then if [ -n `ls $d/*.sh 2> /dev/null` ]; then for __sh in $d/*.sh; do if [ -r \$__sh ]; then . \$__sh; fi; done; unset __sh; fi; fi\n" >> $tmp_profile
#		cat <<-'END' > $tmp_profile
#			if [ -d $d ]; then
#				if [ -n `ls $d/*.sh 2> /dev/null` ]; then
#					for __sh in $d/*.sh; do
#						if [ -r \$__sh ]; then
#							. \$__sh
#						fi
#					done
#					unset __sh
#				fi
#			fi
#			END
		$sudo mv $tmp_profile $profile
	fi
}

add_to_profile_d() {
	local files="$@"
	local d
	local sudo
	if [[ `uname -s` == Darwin ]]; then
		d="$HOME/.profile.d"
		sudo=
	else
		d="/etc/profile.d"
		sudo="$SUDO"
	fi
	if [[ ! -d $d ]]; then
		$sudo mkdir -p $d
	fi
	local f
	for f in $files; do
		$sudo cp $f $d/
	done
}

#----------------------------------------------------------------------------------------------

xinstall() {
	local packs="$@"

	if is_command apt-get; then
		export DEBIAN_FRONTEND=noninteractive
		[[ $NO_UPDATE != 1 ]] && runn $SUDO apt-get update -qq
		runn $SUDO apt-get install --fix-missing -y "$packs"
	elif is_command dnf; then
		runn $SUDO dnf install -y "$packs"
	elif is_command yum; then
		runn $SUDO yum install -y "$packs"
	elif is_command zypper; then
		runn $SUDO zypper install -y "$packs"
	elif is_command apk; then
		[[ $NO_UPDATE != 1 ]] && runn apk update
		runn $SUDO apk add "$packs"
	elif is_command pacman; then
		[[ $NO_UPDATE != 1 ]] && runn pacman --noprogressbar -Sy
		runn $SUDO pacman --noconfirm --noprogressbar -S "$packs"
	elif is_command brew; then
		[[ $NO_UPDATE == 1 ]] && export HOMEBREW_NO_AUTO_UPDATE=1
		runn "brew list \"$packs\" &>/dev/null || brew install \"$packs\""
	elif is_command pkg; then
		runn $SUDO pkg install -y "$packs"
	fi
}

#----------------------------------------------------------------------------------------------

trim() {
    local var="$*"
    # remove leading whitespace characters
    var="${var#"${var%%[![:space:]]*}"}"
    # remove trailing whitespace characters
    var="${var%"${var##*[![:space:]]}"}"
    printf '%s' "$var"
}

#----------------------------------------------------------------------------------------------

is_abspath() {
	case $1 in (/*) pathchk -- "$1";; (*) ! : ;; esac
}
