dirname()

# dirname() -- Returns directory name component of path
# Usage: dirname filename
#
# Example 1:
#
# echo $(dirname "/usr/bin/chak");
#
# Example 2:
#
# string="$(dirname "/usr/bin/chak")";
# echo $string;

dirname() {
	local dir="${1%${1##*/}}";
	[ "${dir:=./}" != "/" ] && dir="${dir%?}";
	echo "$dir";
}

yesno()

# yesno() -- prompt user to accept yes or no
# Usage: yesno [prompt msg]
#
# Example:
#
# while ! yesno "sila taip yes"; do
#	echo "yes ditaip";
# done
#

yesno() {
	[ $# -eq 1 ] || return 1;
	local _prompt="$1";
	local _ANS="";
	read -p "${_prompt} [yes/no]: " _ANS;
	while [ "$_ANS" != "yes" ] && [ "$_ANS" != "no" ]; do
		read -p "Please enter 'yes' or 'no': " _ANS;
	done;
	[ "$_ANS" = "yes" ] && return 0;
	return 1;
}

basename()

# basename() -- Returns filename component of path
# Usage: basename filename
#
# Example 1:
#
# echo $(basename "/usr/bin/chak");
#
# Example 2:
#
# string="$(basename "/usr/bin/chak.exe" ".exe")";
# echo $string;

basename() {
	local name="${1##*/}";
	local opt="$2";
	[ -n "$opt" ] && name="${name%.$opt}";
	echo "${name%$2}";
}

ucfirst()

# ucfirst() -- Make a string's first character uppercase
# Usage: ucfirst string
# depend: strtoupper(): http://www.ronggeng.net/index.php/2009/04/24/strtoupper/
#
# Example:
#
# ucfirst "test test test";
#

if [ -n "${BASH_VERSINFO[0]}" ] && [ ${BASH_VERSINFO[0]} -gt 3 ]; then
	# Updated: 20-Aug-2009 - for bash version 4
	ucfirst() {
		[ $# -eq 1 ] || return 1;
		echo ${1^};
		return 0;
	}
else
	ucfirst() {
		[ $# -eq 1 ] || return 1;
		! type -t strtoupper &>/dev/null && return 1;
		echo "$(strtoupper "${1:0:1}")${1:1:${#1}}";
		return 0;
	}
fi

ucwords()

# ucwords() -- Uppercase the first character of each word in a string
# Usage: ucwords string
# depend: strtoupper(): http://www.ronggeng.net/index.php/2009/04/24/strtoupper/
#
# Example:
#
# ucwords "test test test";
#

ucwords() {
	[ $# -eq 1 ] || return 1;
	! type -t strtoupper &>/dev/null && return 1;
	local _x _c _p _ret="" _str="$1";
	_p=0;
	for ((_x=0;_x<${#_str};_x++)); do
		_c=${_str:$_x:1};
		if [ "$_c" != " " ] && [ "$_p" = "0" ]; then
			_ret+="$(strtoupper "$_c")";
			_p=1;continue;
		fi;
		[ "$_c" = " " ] && _p=0;
		_ret+="$_c";
	done;
	if [ -n "${_ret:-}" ]; then
		echo "${_ret}";
		return 0;
	fi;
	return 1;
}

_execq()

# _execq() -- Execute command and silent output
# Usage: _execq command
#
# Example:
#
# if _execq "/bin/ps auwx"; then
#	echo "ps OK";
# fi
#

_exeq() {
	local cmd=$@;
	$cmd &>/dev/null;
	return $?;
}

rm_str()

# rm_str() -- Remove string/character from string
# Usage: rm_str string string
#
# Example 1:
#
# echo $(rm_str "pungkok hang" "pungkok");
#
# Example 2:
#
# string="$(rm_str "pungkok hang" "pungkok")";
# echo $string;

rm_str() {
	local _str="$1";
	local _chr="$2";
	[ -z "$_chr" ] && _chr='"';
	if [ -n "$_str" -a -n "$_chr" ]; then
		echo ${_str//$_chr};
		return 0;
	fi;
	return 1;
}

lcfirst()

# lcfirst() -- Make a string's first character lowercase
# Usage: lcfirst string
# depend: strtolower(): http://www.ronggeng.net/index.php/2009/04/24/strtolower/
#
# Example:
#
# lcfirst "test test test";
#

if [ -n "${BASH_VERSINFO[0]}" ] && [ ${BASH_VERSINFO[0]} -gt 3 ]; then
	# Updated: 20-Aug-2009 - for bash version 4
	lcfirst() {
		[ $# -eq 1 ] || return 1;
		echo ${1,};
		return 0;
	}
else
	lcfirst() {
		[ $# -eq 1 ] || return 1;
		! type -t strtolower &>/dev/null && return 1;
		echo "$(strtolower "${1:0:1}")${1:1:${#1}}";
		return 0;
	}
fi

str_repeat()

# str_repeat() -- Repeat a string
# Usage: str_repeat input multiplier
# depend: is_num(): http://www.ronggeng.net/index.php/2009/04/24/is_num/
#
# Example:
#
# str_repeat "test test test" 10;
#

str_repeat() {
	[ $# -eq 2 ] || return 1;
	! is_num "$2" && return 1;
	local _cnt _ret="";
	while ((_cnt < $2 )); do
		_ret+="$1";
		((_cnt++));
        done;
	echo "$_ret";
	return 0;
}

is_num()

# is_num() -- Check if input is number between 0-9
# Usage: is_num input
#
# Example:
#
# if is_num 200; then
#	echo "OK";
# fi
#

is_num() {
	[ $# -eq 1 ] || return 1;
	[[ $1 =~ ^([0-9]+)$ ]] && return 0;
	return 1;
}