# #!/bin/bash
# Created em: Ter 11/Out/2011 hs 08:01
# Last Change: 2011 Out 11 08:02:24
# need snippet for 'usage'
# source: http://snipt.net/voyeg3r/tag/snippets
# header for shell script
snippet sh
	#!/bin/bash
	# Criado em: `system("date +%m-%d-%Y")`
	# Last Change: `system("date +%m-%d-%Y")`
	# vim:ft=sh:fdm=syntax:nu:
	# Instituicao: ${1:instituicao}
	# Proposito do script: ${2:descricao}
	# Autor: ${3:`g:snips_author`}
	# site: ${4:site}
snippet #!
	#!/bin/bash
# test if has dir
snippet ifdir
	[ -d ${1:namedir} ] || mkdir $1 ; cd $1
# source: http://wiki.bash-hackers.org/scripting/style
# if has needed programs in system
snippet has
	NEEDED_COMMANDS="${1:program}"
	missing_counter=0
	for needed_command in $NEEDED_COMMANDS; do
		if ! hash "$needed_command" >/dev/null 2>&1; then
			printf "Command not found in PATH: %s\n" "$needed_command" >&2
			((missing_counter++))
		fi
	done
	if ((missing_counter > 0)); then
		printf "Minimum %d commands are missing in PATH, aborting" "$missing_counter" >&2
		exit 1
	fi
	${2}
snippet existe
	# if which dont returns nothing exit
	NEEDED_COMMAND="$(which ${1:program})" || { echo "Instale primeiro o programa $1"; sleep 3; exit 1;}
snippet ifile
	if [ ! -e ${1:filename}  ]; then
		echo "  file $1 not present"
	fi
snippet if
	if [[ ${1:condition} ]]; then
		${2:#statements}
	fi
snippet elif
	elif [[ ${1:condition} ]]; then
		${2:#statements}
snippet for
	for (( ${2:i} = 0; $2 < ${1:count}; $2++ )); do
		${3:#statements}
	done
snippet wh
	while [[ ${1:condition} ]]; do
		${2:#statements}
	done
snippet until
	[[ ${1:condition} ]]; do
		${2:#statements}
	done
snippet case
	case ${1:word} in
		${2:pattern})
		${3};;
	esac
# ifroot - if root
snippet ir
	# testa se está logado como root
	if [ "$(id -u)" != "0" ]; then
		echo "  Need be root"
		sleep 2
		exit  1
	fi
snippet lock
	LOCK_FILE=/tmp/.lock
	(set -C; :  > $LOCK_FILE) 2> /dev/null
	if [  $? != "0" ];  then
	  echo "Lock File exists - exiting"
	   sleep 1
	   exit 1
	fi
	${1}
snippet usage
	usage()
	{
		echo "Usage: $(basename $${1:0}) ${2:argument}"
		echo "${3:explanation}"
	}
snippet lower
	${1:var}=${$1,,}
	${2}
snippet upper
	${1:var}=${$1^^}
	${2}
snippet array
	#ARRAY=( 'Debian Linux' 'Redhat Linux' Ubuntu Linux )
	# get number of elements in the array
	ELEMENTS=${#ARRAY[@]}
	for (( i=0;i<$ELEMENTS;i++)); do
		${1:echo} ${ARRAY[${i}]}
	done
	${2:out}
snippet alias
	alias ${1:name}='${2:action}'